参考: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); //转入
}
}