spring注解 事务转账案例

spring注解 事务转账案例

---domain层 创建Account对象 略;
---dao层 
@Component("accountDao")
public class AccountDaoImpl implements AccountDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
   //姓名 获取 ACCount对象
    @Override
    public Account findAccountByName(String sourceName) {
        List<Account> list = jdbcTemplate.query("select * from account where name = ? ", new BeanPropertyRowMapper<Account>(Account.class), sourceName);
       return list.get(0);
    }
	//修改的方法
    @Override
    public void updateAccount(Account account) {
        jdbcTemplate.update("update account set money = ? where id = ? ",account.getMoney(),account.getId());
    }
}
--- service层
@Service("accountService")
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;
  /******
     * 注入AccountDao依赖
     * @param
     * @return
     */
    @Override
    @Transactional(readOnly = false,propagation =Propagation.REQUIRED)
    public void transfer(String sourceName, String targeName, Float money) {
        //1.根据名称查询两个账户
        Account source = accountDao.findAccountByName(sourceName);
        Account target = accountDao.findAccountByName(targeName);
        //2.修改两个账户的金额
        source.setMoney(source.getMoney()-money);//转出账户减钱
        target.setMoney(target.getMoney()+money);//转入账户加钱
        //3.更新两个账户
        accountDao.updateAccount(source);
        //事务异常发生点。
        int i =10/0;
        accountDao.updateAccount(target);
    }
}
--- SpringConfig 配置类
@Configuration
@ComponentScan("com.hupu")
@EnableTransactionManagement
public class SpringConfig {
    //创建jdbcTemplate 对象放进容器内
    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource){
        JdbcTemplate jdbcTemplate =new  JdbcTemplate(dataSource);
        return jdbcTemplate;
    }
    //创建dataSource 数据源 对象放进容器内
    @Bean
    public DataSource dataSource(){
        ComboPooledDataSource dataSource =null;
        try {
            dataSource = new ComboPooledDataSource();
            dataSource.setDriverClass("com.mysql.jdbc.Driver");
            dataSource.setPassword("root");
            dataSource.setUser("root");
            dataSource.setJdbcUrl("jdbc:mysql:///hupu55");
        } catch (Exception e) {
            e.printStackTrace();
        }
       return dataSource;
    }
    
    //创建transactionManager 事务管理器  对象放进容器内
    @Bean
    public DataSourceTransactionManager transactionManager(DataSource dataSource){
        DataSourceTransactionManager dsm =new  DataSourceTransactionManager();
        dsm.setDataSource(dataSource);

        return dsm;

    }
}


--- 记得导入依赖
 <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        
  ---test测试类  
@RunWith(SpringRunner.class)
@ContextConfiguration(classes =SpringConfig.class )
public class Springtest02 {

    @Autowired
    private  AccountService accountService;
    @Test
    public void test02() {
        accountService.transfer("aaa","bbb",100f);

    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值