14.spring声明事务

1.要开启 Spring 的事务处理功能,在 Spring 的配置文件中创建一个 DataSourceTransactionManager 对象:

<!--配置声明式事务-->
<bean id="transationManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>

<!--结合AOP实现事务的织入-->
<!--配置事务通知-->
<tx:advice id="txAdvice" transaction-manager="transationManager">
    <tx:attributes>
        <!--给那些方法配置事务-->
        <!--propagation 配置事务的传播性-->
        <tx:method name="addUser" propagation="REQUIRED"/>
        <tx:method name="delUser" propagation="REQUIRED"/>
        <tx:method name="*" propagation="REQUIRED"/>
    </tx:attributes>
</tx:advice>

<!--配置事务的切入-->
<aop:config>
    <aop:pointcut id="pointCut" expression="execution(* com.zuo.dao.UserMapperImpl.*.*(..))"/>

    <aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"/>
</aop:config>

 

事务的管理有两种

  第一种:交由容器管理事务

  (1)配置事务

public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper {

    @Override
    public List<User> UserList() {

        UserMapper mapper = getSqlSession().getMapper(UserMapper.class);

        User addUser = new User(5, "xiaoKUN", "123456");

        mapper.addUser(addUser);

        mapper.delUser(5);

        return mapper.UserList();

    }

    @Override
    public int addUser(User user) {
        return getSqlSession().getMapper(UserMapper.class).addUser(user);
    }

    @Override
    public int delUser(int id) {
        return getSqlSession().getMapper(UserMapper.class).delUser(id);
    }
}

  (2)在spring-dao.xml中配置声明式事务

<!--配置声明式事务-->
<bean id="transationManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>

<!--结合AOP实现事务的织入-->
<!--配置事务通知-->
<tx:advice id="txAdvice" transaction-manager="transationManager">
    <tx:attributes>
        <!--给那些方法配置事务-->
        <!--propagation 配置事务的传播性-->
        <tx:method name="addUser" propagation="REQUIRED"/>
        <tx:method name="delUser" propagation="REQUIRED"/>
        <tx:method name="*" propagation="REQUIRED"/>
    </tx:attributes>
</tx:advice>

<!--配置事务的切入-->
<aop:config>
    <aop:pointcut id="pointCut" expression="execution(* com.zuo.dao.UserMapperImpl.*.*(..))"/>

    <aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"/>
</aop:config>

  (3)测试

public class MyTest {

    @Test
    public void test(){

       ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        UserMapper mapper = context.getBean("userMapper2", UserMapper.class);

        List<User> userList = mapper.UserList();

        for (User user : userList) {
            System.out.println(user);
        }

    }

}

  第二种:编程式的事务管理

    定义显式的捕获异常和业务的回滚操作

posted on   人无远虑必有近忧  阅读(20)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示