Spring-事務管理(概念和環境搭建)
阿新 • • 發佈:2020-08-19
1.什麼是事務?
(1)事務是資料庫操作最基本單元,邏輯上一組操作,要麼都成功,如果有一個失敗所有操作都失敗
(2)典型場景:銀行轉賬
2.事務特性(ACID)
(1)原子性:要麼都執行,要麼都不執行
(2)一致性:操作前後總量不變
(3)隔離性:
(4)永續性:
3.搭建事務操作基本環境
(1)銀行轉賬例子環境
1.建立資料庫表,新增記錄
2.建立service,搭建dao,完成物件建立和注入關係
(1)service注入dao,在dao注入JdbcTemplate,在JdbcTemplate注入dataSource
package com.orzjiangxiaoyu.spring.service;import com.orzjiangxiaoyu.spring.dao.BankDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * @author orz * @create 2020-08-18 16:32 */ @Service public class BankService { @Autowired private BankDao bankDao; }
packagecom.orzjiangxiaoyu.spring.dao; /** * @author orz * @create 2020-08-18 16:32 */ public interface BankDao { }
package com.orzjiangxiaoyu.spring.dao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository;/** * @author orz * @create 2020-08-18 16:32 */ @Repository public class BankDaoImpl implements BankDao { @Autowired private JdbcTemplate jdbcTemplate; }
(2)在dao裡面建立兩個方法:少錢方法、多錢方法
package com.orzjiangxiaoyu.spring.dao; /** * @author orz * @create 2020-08-18 16:32 */ public interface BankDao { public void addMoney(); public void reduceMoney(); }
package com.orzjiangxiaoyu.spring.dao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; /** * @author orz * @create 2020-08-18 16:32 */ @Repository public class BankDaoImpl implements BankDao { //AA轉賬100給BB @Autowired private JdbcTemplate jdbcTemplate; //多錢的方法 //BB+100 @Override public void addMoney() { String sql="update user_table set balance=balance+? where user=?"; int update = jdbcTemplate.update(sql, 100, "BB"); System.out.println(update); } //少錢的方法 //AA-100 @Override public void reduceMoney() { String sql="update user_table set balance=balance-? where user=?"; int update = jdbcTemplate.update(sql, 100, "AA"); System.out.println(update); } }
(3)在service裡面建立轉賬方法
package com.orzjiangxiaoyu.spring.service; import com.orzjiangxiaoyu.spring.dao.BankDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * @author orz * @create 2020-08-18 16:32 */ @Service public class BankService { @Autowired private BankDao bankDao; //轉賬方法 public void accountMoney() { //AA減100 bankDao.reduceMoney(); //BB加100 bankDao.addMoney(); } }
(5)測試
package com.orzjiangxiaoyu.spring.testdemo; import com.orzjiangxiaoyu.spring.service.BankService; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author orz * @create 2020-08-18 16:49 */ public class Test1 { @Test public void test1() { ApplicationContext context=new ClassPathXmlApplicationContext("jdbcbean.xml"); BankService bankService = context.getBean("bankService", BankService.class); bankService.accountMoney(); } }
上面程式碼如果正常執行沒有問題的,但是如果程式碼出現異常則會出現問題
使用事務進行解決