第10講. SpringBoot事物管理
阿新 • • 發佈:2018-11-16
1.新建一個數據庫,db_bank.編碼集:utf8
2,複製9.3的專案,重新命名為SpringDataTransaction,這一步主要是為了不影響之前的程式碼, 更新一下專案:右鍵專案名->maven->Update Projec...,
3,新建一個實體,Account,指定表明為:t_account 加上註解。float不需要長度註解, 修改配置檔案,啟動專案,檢視是否生成表,如果生成了表t_account 則成功。為了不影響程式碼 刪除 t_book 表,。
package com.cruise.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; } } |
配置檔案:
server: port: 8888 context-path: / spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/db_bank password: root username: root jpa: hibernate: ddl-auto: update show-sql: true |
4, 新建一個AccountDao,繼承介面JpaRepository Integer 為主鍵的型別
package com.cruise.dao; import org.springframework.data.jpa.repository.JpaRepository; import com.cruise.entity.Account; public interface AccountDao extends JpaRepository{ } |
5, 新建com.cruise.service包,新建AccountService介面,寫一個轉賬介面
package com.cruise.service; public interface AccountService { public void transferAccounts(int fromUser,int toUser,float account); } |
6,寫Service實現類,AccountServiceImpl,實現AccountService介面,加註解,注入dao介面,
從 fromAccount 轉賬到 toAccount ,fromAccount 減少account , toAccount增加 account
package com.cruise.service.impl; import javax.annotation.Resource; import javax.transaction.Transactional; import org.springframework.stereotype.Service; import com.cruise.dao.AccountDao; import com.cruise.entity.Account; import com.cruise.service.AccountService; /** * * @author pengc * */ @Service("accountService") public class AccountServiceImpl implements AccountService { @Resource private AccountDao accountDao; public void transferAccounts(int fromUser, int toUser, float account) { Account fromAccount = accountDao.findById(fromUser).get(); fromAccount.setBalance(fromAccount.getBalance()-account); accountDao.save(fromAccount); Account toAccount = accountDao.findById(toUser).get(); toAccount.setBalance(toAccount.getBalance()+account); accountDao.save(toAccount); } } |
7,AccountController ,使用RestController註解,注入service介面。刪除book相關的程式碼,以防有影響。
使用try{...}catch(Exception e){...}來判斷是否執行成功,如果成功返回OK,如果失敗返回NO
package com.cruise.controller; import javax.annotation.Resource; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.cruise.service.AccountService; @RestController @RequestMapping("/account") public class AccountController { @Resource private AccountService accountService; @RequestMapping("/transfer") public String transferAccount(){ try{ accountService.transferAccounts(1, 2, 200); return "OK"; }catch(Exception e){ return "NO"; } } } |
8,測試,
9,在service中設定異常程式碼,測試,
10,在service實現類上新增事物註解,測試,注意導哪個包