SpringBoot中事务控制

参考:https://www.cnblogs.com/wxc-xiaohuang/p/9471971.html

整合了Mybatis,并实现事务控制

 

pom依赖

application.properties配置文件。

1 spring.datasource.url=jdbc:mysql://localhost:3306/springboot_demo
2 spring.datasource.username=root
3 spring.datasource.password=root
4 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
1 mybatis.typeAliasesPackage: cn.yideng.*.entity
2 mybatis.mapperLocations: classpath:mapper/*.xml

创建数据库:

DROP TABLE IF EXISTS tbl_account;
CREATE TABLE tbl_account (
  id int(11) NOT NULL AUTO_INCREMENT,
  name varchar(20) NOT NULL,
  balance float,
  PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;


insert into tbl_account(id,name,balance) values(1, 'andy','200');
insert into tbl_account(id,name,balance) values(2, 'lucy','300');

实体类:

public class Account {

    private int id; 
    private String name;
    private float balance;

    public Account() {

    }
    // 省略setter / getter
}

dao:

public interface AccountDao {

    public void moveIn(@Param("id") int id, @Param("money") float money); // 转入

    public void moveOut(@Param("id") int id, @Param("money") float money); // 转出
}

service:

1 public interface AccountService {    
2     //转账
3     public void transfer(int outter,int inner,Integer money);
4 
5 }

实现:

@Service
public class AccountServiceImpl implements AccountService{

    @Autowired
    private AccountDao accountDao;

    public void transfer(int outter, int inner, Integer money) {

        accountDao.moveOut(outter, money); //转出
        accountDao.moveIn(inner, money); //转入

    }
}

控制器:

@RestController
@RequestMapping(value = "/account")
public class AccountController {

    @Autowired
    private AccountService accountService;


    @RequestMapping("/transfer")
    public String test(){
        try {
            // andy 给lucy转账50元
            accountService.transfer(1, 2, 50);
            return "转账成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "转账失败";
        }
    }
}

Mybatis中配置文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >

<mapper namespace="cn.yideng.tx.dao.AccountDao">

    <!-- 转入 -->
    <update id="moveIn" >
        update tbl_account 
        set balance = balance + #{money }
        where id= #{id,jdbcType=INTEGER}
    </update>

    <!-- 转出 -->
    <update id="moveOut" >
        update tbl_account 
        set balance = balance - #{money }
        where id= #{id,jdbcType=INTEGER}
    </update>

</mapper>

 

启动类:

在启动类中添加对mapper包扫描@MapperScan

 

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableAutoConfiguration
@MapperScan("cn.yideng.*.dao")
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    } 
}

修改为:

@Service
public class AccountServiceImpl implements AccountService{

    @Autowired
    private AccountDao accountDao;

    public void transfer(int outter, int inner, Integer money) {

        accountDao.moveOut(outter, money); //转出
        int i = 1/0;  // 抛出异常
        accountDao.moveIn(inner, money); //转入

    }
}

转账出现问题:

@Service
public class AccountServiceImpl implements AccountService{

    @Autowired
    private AccountDao accountDao;

    @Transactional
    public void transfer(int outter, int inner, Integer money) {

        accountDao.moveOut(outter, money); //转出
        int i = 1/0;  // 抛出异常
        accountDao.moveIn(inner, money); //转入

    }
}

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值