以前学ssh ssm都有事务管理service层通过applicationContext.xml配置,所有service方法都加上事务操作;
用来保证一致性,即service方法里的多个dao操作,要么同时成功,要么同时失败;
springboot下的话 一个@Transactional即可搞定;
我们这里搞一个实例,转账实例,A用户转账给B用户xx元
设计如下:
Account类
1 package com.hik.entity; 2 3 import javax.persistence.Column; 4 import javax.persistence.Entity; 5 import javax.persistence.GeneratedValue; 6 import javax.persistence.Id; 7 import javax.persistence.Table; 8 9 /** 10 * 账户实体 11 * @author jed 12 * 13 */ 14 @Entity 15 @Table(name="t_account") 16 public class Account { 17 18 @Id 19 @GeneratedValue 20 private Integer id; 21 22 @Column(length=50) 23 private String userName; 24 25 private float balance; 26 27 public Integer getId() { 28 return id; 29 } 30 31 public void setId(Integer id) { 32 this.id = id; 33 } 34 35 public String getUserName() { 36 return userName; 37 } 38 39 public void setUserName(String userName) { 40 this.userName = userName; 41 } 42 43 public float getBalance() { 44 return balance; 45 } 46 47 public void setBalance(float balance) { 48 this.balance = balance; 49 } 50 51 52 }