springboot设置手动事务实战

1.注入

@Resource
private DataSourceTransactionManager dataSourceTransactionManager;
@Resource
private TransactionDefinition transactionDefinition;
 @PostMapping("/findB00List")
    @CrossOrigin
    public Result findB00List(String ... b00) throws SQLException, IOException {
        for (int i = 0; i < b00.length; i++) {
            System.out.println(b00[i]);
        }
        //开启手动事务
        TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
        //获取现在的时间
        LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dict::getName,"dda");
        List list = dictService.list(wrapper);
        Dict dict = new Dict();
        String s1= "dadadada\n"+
         "dadafawfawfaw\n";
        dict.setVarchar1(s1);
        dict.setVarchar2(s1);
        dict.setVarchar3(s1);
        dict.setName("侧睡事务");
        dictService.save(dict);

        //提交或者回滚事务
        // dataSourceTransactionManager.rollback(transactionStatus);
        dataSourceTransactionManager.commit(transactionStatus);

你可能感兴趣的:(spring,boot,spring,java)