栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

2021.10.15 Spring中的事务 案例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2021.10.15 Spring中的事务 案例

目录

1.编程式事务管理(手动编写代码完成事务管理):

2.声明式事务管理(不需要手动编写代码,配置):

1)Xml配置(aop)的方式完成事务管理

 2)注解配置(aop)的方式完成事务管理

3.pox.xml中的相关依赖


创建一个数据表,有a,b两个数据,a,b各有2000元

        

若a向b转100,则现在a为1900,b为2100

此时在Spring事务管理

 创建com.mz.mapper.UserMapper接口

public interface UserMapper {
    public void decrease(Integer id, Double money);//转出
    public void increase(Integer id, Double money);//转入
}

 创建com.mz.mapper.impl.UserMapperImpl

public class UserServiceImpl implements UserService {
    private UserMapper userMapper;

    public void setUserMapper(UserMapper userMapper){
        this.userMapper = userMapper;
    }

    @Override
    public void transfer(Integer fromId, Integer toId, Double money) {
        userMapper.decrease(fromId,money);//转出
        userMapper.increase(toId,money);//转入
    }
}

创建com.mz.service.UserService接口

public interface UserService {
    //转账
    public void transfer(Integer fromId,Integer toId,Double money);
}

创建com.mz.service.impl.UserSServcieImpl类实现上面接口

public class UserServiceImpl implements UserService {
    private UserMapper userMapper;

    public void setUserMapper(UserMapper userMapper){
        this.userMapper = userMapper;
    }

    @Override
    public void transfer(Integer fromId, Integer toId, Double money) {
        userMapper.decrease(fromId,money);//转出
        userMapper.increase(toId,money);//转入
    }
}

applicationContext.xml配置文件


 

    
    
    
    
        
        
        
        
    





    
        
    
    
        
    

创建测试类实现

public class test {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService =
                (UserService)applicationContext.getBean("userService");

        userService.transfer(1,2,100d);
    }
}

此时出现结果为:

 a为1900,b为2100,现在是正常的流程,但如果在a给b转账的时候出现意外

就如这种状况,此时运行程序,会出现:

你会发现,转出的方法执行,转入的未执行,即a减少100元,但b没有变化 ,因此我们需要一个方法解决这种错误。

这时候的话就需要给它搭配一个事务。在Spring中给我们提供两种方式

1.编程式事务管理(手动编写代码完成事务管理):

com.mz.service.impl.UserServiceimpl类

public class UserServiceImpl  implements UserService {
    private UserMapper userMapper;

    public void setUserMapper(UserMapper userMapper){
        this.userMapper = userMapper;
    }

    //事务模板对象
    private TransactionTemplate transactionTemplate;

    public void setTransactionTemplate(TransactionTemplate transactionTemplate){
        this.transactionTemplate = transactionTemplate;
    }

//    @Override
//    public void transfer(Integer fromId, Integer toId, Double money) {
//        userMapper.decrease(fromId,money);//转出
//        int i = 1/0;
//        userMapper.increase(toId,money);//转入
//    }

    @Override
    public void transfer(final Integer fromId, final Integer toId, final Double money) {
        //手动编程式事务(了解)
        transactionTemplate.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                userMapper.decrease(fromId, money);//转出
                //int i = 1 / 0;
                userMapper.increase(toId, money);//转入
            }
        });
    }
}

applicationContext.xml配置文件


 

    
    
    
    
        
        
        
        
    


    
    
        
    
    
    
        
    


    
        
    
    
        
        
    

 此时,a为1800,b为2100

若正常运行,,a为1700,b为2200。

若发生异常,,此时,方法都不执行。

2.声明式事务管理(不需要手动编写代码,配置):

1)Xml配置(aop)的方式完成事务管理

com.mz.service.impl.UserServiceImpl

public class UserServiceImpl  implements UserService {
    private UserMapper userMapper;

    public void setUserMapper(UserMapper userMapper){
        this.userMapper = userMapper;
    }


    @Override
    public void transfer(Integer fromId, Integer toId, Double money) {
        userMapper.decrease(fromId,money);//转出
        //int i = 1/0;
        userMapper.increase(toId,money);//转入
    }
}

 applicationContext.xml配置文件


 

    
    
    
    
        
        
        
        
    


    
    
        
    

    
    
        
            
            
        
    

    
    
        
        
    







    
        
    
    
        

    

 此时a为1700,b为2200

正常运行,则,a为1600,b为2300。

异常运行,则,a,b不变,方法不执行。

 2)注解配置(aop)的方式完成事务管理

com.mz.service.impl.UserServletImpl

@Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED,readonly = true)
public class UserServiceImpl  implements UserService {
    private UserMapper userMapper;

    public void setUserMapper(UserMapper userMapper){
        this.userMapper = userMapper;
    }


    @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED,readonly = false)
    @Override
    public void transfer(Integer fromId, Integer toId, Double money) {
        userMapper.decrease(fromId,money);//转出
        int i = 1/0;
        userMapper.increase(toId,money);//转入
    }


}

 applictionContext.xml配置



    
    
        
        
        
        
    


    
    
        
    
    
    

    


















    
        
    
    
        

    

 此时a为1600,b为2300

正常运行,则,a为1500,b为2400。

出现异常的话则,a,b方法不执行。

3.pox.xml中的相关依赖
        
            org.springframework
            spring-context
            5.3.10
        

        
            org.springframework
            spring-jdbc
            5.3.10
        

        
            org.springframework
            spring-tx
            5.3.10
        

        
            org.aspectj
            aspectjweaver
            1.9.6
        

        
            mysql
            mysql-connector-java
            8.0.26
        

        
            com.alibaba
            druid
            1.2.6
        

        
            junit
            junit
            4.13.2
        

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/328591.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号