Spring声明式事务
1:回顾事务:
把一组事务当成一个事务来做,要么都成功 ,要么失败
事务在项目开发中,非常重要,涉及到数据一致的问题,不能马虎;
确保完整性和一致性;
事物的ACID原则
1.原子性
2.一致性
3.隔离性
(1)多个业务可能操作同一个资源,防止数据损坏
4.持有型
(1)事务一旦提交,无论系统发生什么问题,结果都不会发生影响,被持有化的写到储存器中;
2:声明式事务: AOP
包结构:
我测试的时候出现的问题:
https://blog.csdn.net/weixin_45723046/article/details/124106251?spm=1001.2014.3001.5502
Pojo
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private int id;
private String name;
private String pwd;
}
UserMapper
public interface UserMapper {
//查询全部用户
List<User> selectUser();
//添加一个用户;
Integer addUser(User user);
//删除一个用户;
int deleteUser(int id);
}
UserMapper.xml
<!--configuration核心配置-->
<mapper namespace="com.kuang.mapper.UserMapper">
<select id="selectUser" resultType="user">
select * from mybatis.user
</select>
<select id="addUser" parameterType="user">
insert into mybatis.user (id, name, pwd) values (#{id},#{name},#{pwd});
</select>
<delete id="deleteUser" parameterType="int">
delete from mybatis.user where id = #{id}
</delete>
</mapper>
UserMapperImpl
public class UserMapperImpl extends SqlSessionDaoSupport implements UserMapper {
@Override
public List<User> selectUser() {
UserMapper mapper = getSqlSession().getMapper(UserMapper.class);
User user = new User(5, "铁锤", "543223");
mapper.addUser(user);
mapper.deleteUser(4);
return mapper.selectUser();
}
@Override
public Integer addUser(User user) {
UserMapper mapper = getSqlSession().getMapper(UserMapper.class);
return mapper.addUser(user);
}
@Override
public int deleteUser(int id) {
UserMapper mapper = getSqlSession().getMapper(UserMapper.class);
return mapper.deleteUser(id);
}
}
mybatis-config.xml
<!--configuration核心配置-->
<configuration>
<!--别名-->
<typeAliases>
<package name="com.kuang.pojo" />
</typeAliases>
</configuration>
spring-dao.xml
<?xml version="1.0" encoding="utf-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd">
<!--DataSource 使用Spring的数据源替换MyBatis的配置 c3p0 dbcp-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!--sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--绑定MyBatis配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:com/kuang/mapper/*.xml"/>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!--只能使用构造器注入sqlSessionFactory,因为它没有set方法-->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<!--配置声明式事务-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--结合AOP 实现事务的织入-->
<!--配置事务 通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!--给哪些方法配置事务-->
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!--配置事务切入-->
<aop:config>
<!--切入点:pointcut-->
<aop:pointcut id="txPointcut" expression="execution(* com.kuang.mapper.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
</beans>
Applicationcontext.xml
<?xml version="1.0" encoding="utf-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<import resource="spring-dao.xml"/>
<bean id="userMapper" class="com.kuang.mapper.UserMapperImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>
测试:
public class MyTest {
@Test
public void test() throws IOException {
ApplicationContext context = new ClassPathXmlApplicationContext("applicateioncontext.xml");
UserMapper userMapper = context.getBean("userMapper", UserMapper.class);
for (User user : userMapper.selectUser()) {
System.out.println(user);
}
}
@Test
public void addUser(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicateioncontext.xml");
User user = new User(5, "张三", "123");
UserMapper userMapper = context.getBean("userMapper", UserMapper.class);
Integer integer = userMapper.addUser(user);
System.out.println(integer);
}
@Test
public void deleteUser(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicateioncontext.xml");
UserMapper userMapper = context.getBean("userMapper", UserMapper.class);
int i = userMapper.deleteUser(5);
System.out.println(i);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/71850.html