SpringBoot事务管理
[size=medium][b]1. SpringBoot之事物管理@Transactional[/b][/size] [size=smaill][b]1) 正常业务[/b][/size] [code="java"] 新建项目SpringDataTransaction src/main/java/com/andrew/entity/Account.java package com.andrew.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "t_account") public class Account { @Id @GeneratedValue private Integer id; @Column(length = 50) private String userName; private float balance; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public float getBalance() { return balance; } public void setBalance(float balance) { this.balance = balance; } } HelloWorldApplication.java右键Run As选择Spring Boot App 数据库生产t_account表 insert into t_account values(1, 700, 'zhangsan'); insert into t_account values(2, 300, 'lisi'); src/main/java/com/andrew/dao/AccountDao.java package com.andrew.dao; import org.springframework.data.jpa.repository.JpaRepository; import com.andrew.entity.Account; public interface AccountDao extends JpaRepository { } src/main/java/com/andrew/service/AccountService.java package com.andrew.service; public interface AccountService { public void transferAccounts(int fromUser,int toUser,float account); } src/main/java/com/andrew/service/impl/AccountServiceImpl.java package com.andrew.service.impl; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.andrew.dao.AccountDao; import com.andrew.entity.Account; import com.andrew.service.AccountService; @Service("accountService") public class AccountServiceImpl implements AccountService { @Resource private AccountDao accountDao; public void transferAccounts(int fromUser, int toUser, float account) { Account fromAccount = accountDao.getOne(fromUser); fromAccount.setBalance(fromAccount.getBalance() - account); accountDao.save(fromAccount); Account toAccount = accountDao.getOne(toUser); toAccount.setBalance(toAccount.getBalance() + account); accountDao.save(toAccount); } } src/main/java/com/andrew/controller/AccountController.java package com.andrew.controller; import javax.annotation.Resource; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.andrew.service.AccountService; @RestController @RequestMapping("/account") public class AccountController { @Resource private AccountService accoutService; @RequestMapping("/transfer") public String transferAccount(){ try { accoutService.transferAccounts(1, 2, 200); return "OK"; } catch(Exception e) { return "NO"; } } } src/main/resources/application.properties server.port=8000 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/springboot spring.datasource.username=root spring.datasource.password=root spring.jpa.hibernate.ddl-auto=update spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect spring.jpa.show-sql=true pom.xml org.springframework.boot spring-boot-configuration-processor true org.springframework.boot spring-boot-starter-freemarker mysql mysql-connector-java runtime org.springframework.boot spring-boot-starter-data-jpa http://localhost:8000/account/transfer 运行结果: YES 1 700 zhangsan 2 300 lisi [/code] [size=smaill][b]2) 增加异常[/b][/size] [code="java"] src/main/java/com/andrew/service/impl/AccountServiceImpl.java package com.andrew.service.impl; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.andrew.dao.AccountDao; import com.andrew.entity.Account; import com.andrew.service.AccountService; @Service("accountService") public class AccountServiceImpl implements AccountService { @Resource private AccountDao accountDao; public void transferAccounts(int fromUser, int toUser, float account) { Account fromAccount = accountDao.getOne(fromUser); fromAccount.setBalance(fromAccount.getBalance() - account); accountDao.save(fromAccount); Account toAccount = accountDao.getOne(toUser); toAccount.setBalance(toAccount.getBalance() + account); accountDao.save(toAccount); } } http://localhost:8000/account/transfer 运行结果: NO 1 500 zhangsan 2 300 lisi [/code] [size=smaill][b]3) 增加异常,增加事务[/b][/size] [code="java"] src/main/java/com/andrew/service/impl/AccountServiceImpl.java package com.andrew.service.impl; import javax.annotation.Resource; import javax.transaction.Transactional; import org.springframework.stereotype.Service; import com.andrew.dao.AccountDao; import com.andrew.entity.Account; import com.andrew.service.AccountService; @Service("accountService") public class AccountServiceImpl implements AccountService { @Resource private AccountDao accountDao; @Transactional public void transferAccounts(int fromUser, int toUser, float account) { Account fromAccount = accountDao.getOne(fromUser); fromAccount.setBalance(fromAccount.getBalance() - account); accountDao.save(fromAccount); Account toAccount = accountDao.getOne(toUser); toAccount.setBalance(toAccount.getBalance() + account); accountDao.save(toAccount); } } http://localhost:8000/account/transfer 运行结果: NO 1 700 zhangsan 2 300 lisi [/code]