Spring注解 TX聲明式事務(wù)實(shí)現(xiàn)過程解析
環(huán)境搭建導(dǎo)入
maven依賴
<!--spring提供的數(shù)據(jù)庫操作工具--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.0.2.RELEASE</version> </dependency> <!--c3p0 數(shù)據(jù)庫連接池--> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.5</version> </dependency> <!--mysql連接器--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.48</version> </dependency>
配置數(shù)據(jù)庫相關(guān)信息
@Configuration
@ComponentScan("com.spring.tx")
public class TxConfig {
/**
* 配置數(shù)據(jù)源
*/
@Bean
public DataSource dataSource() throws PropertyVetoException {
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
comboPooledDataSource.setUser("root");
comboPooledDataSource.setPassword("root");
comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver");
comboPooledDataSource.setJdbcUrl("jdbc:mysql:///test");
return comboPooledDataSource;
}
@Bean
public JdbcTemplate jdbcTemplate() throws PropertyVetoException {
//Spring對(duì)配置類做了特殊處理,多次調(diào)用給容器中加組件的方法,其實(shí)是從容器中找組件,并不會(huì)重新添加
return new JdbcTemplate(dataSource());
}
}
添加數(shù)據(jù)訪問層、業(yè)務(wù)層
@Repository
public class UserDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public void saveUser(String name, Integer age) {
String sql = "insert into user(name, age) values(?, ?)";
jdbcTemplate.update(sql, name, age);
}
}
@Service
public class UserService {
@Autowired
private UserDao userDao;
public void saveUser(){
String name = "jack11";
Integer age = 19;
userDao.saveUser(name, age);
}
}
添加測(cè)試類
public class TxTest {
@Test
public void test(){
ApplicationContext context = new AnnotationConfigApplicationContext(TxConfig.class);
UserService userService = (UserService) context.getBean("userService");
userService.saveUser();
}
}
事務(wù)問題
此時(shí)基本環(huán)境已經(jīng)搭建好了,點(diǎn)擊運(yùn)行,數(shù)據(jù)可以成功插入但是還沒有配置事務(wù),沒有事務(wù)回滾會(huì)造成某些情況下數(shù)據(jù)出錯(cuò)。在Spring注解中,可以在需要添加事務(wù)的方法或類上加@Transactional,并且開啟事務(wù)管理功能,即@EnableTransactionManagement,代碼如下:
修改UserService 的 saveUser 方法
@Transactional
public void saveUser(){
String name = "jack11";
Integer age = 19;
userDao.saveUser(name, age);
//模擬異常
int i = 1 / 0;
}
在配置類加上@EnableTransactionManagement
@Configuration
@ComponentScan("com.spring.tx")
@EnableTransactionManagement
public class TxConfig {
//省略數(shù)據(jù)源、jdbcTemplate的配置
}
再次運(yùn)行測(cè)試方法,會(huì)發(fā)現(xiàn)報(bào)錯(cuò)了,但不是我們模擬的異常報(bào)錯(cuò),控制臺(tái)提示找不到bean
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.transaction.PlatformTransactionManager' available
我們還需要注冊(cè)一個(gè)事務(wù)管理器來管理事務(wù),PlatformTransactionManager有很多實(shí)現(xiàn)類,在Spring 中 JdbcTemplate、Mybatis應(yīng)該使用 DataSourceTransactionManager

在配置類中再注冊(cè)一個(gè)組件,運(yùn)行,事務(wù)生效
/**
* 注冊(cè)事務(wù)管理器
*/
@Bean
public PlatformTransactionManager platformTransactionManager() throws PropertyVetoException {
return new DataSourceTransactionManager(dataSource());
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
深入解析Java的設(shè)計(jì)模式編程中的模板方法模式
這篇文章主要介紹了深入解析Java的設(shè)計(jì)模式編程中的模板方法模式, 模版方法模式由一個(gè)抽象類和一個(gè)(或一組)實(shí)現(xiàn)類通過繼承結(jié)構(gòu)組成,需要的朋友可以參考下2016-02-02
Eureka源碼閱讀之環(huán)境搭建及工程結(jié)構(gòu)
這篇文章主要為大家介紹了Eureka源碼閱讀之環(huán)境搭建的工程結(jié)構(gòu)及調(diào)試需知詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪<BR>2022-10-10
Java實(shí)現(xiàn)按年月打印日歷功能【基于Calendar】
這篇文章主要介紹了Java實(shí)現(xiàn)按年月打印日歷功能,涉及java基于Calendar進(jìn)行日期運(yùn)算的相關(guān)操作技巧,需要的朋友可以參考下2018-03-03
Mybatis使用collection標(biāo)簽進(jìn)行樹形結(jié)構(gòu)數(shù)據(jù)查詢時(shí)攜帶外部參數(shù)查詢
這篇文章主要介紹了Mybatis使用collection標(biāo)簽進(jìn)行樹形結(jié)構(gòu)數(shù)據(jù)查詢時(shí)攜帶外部參數(shù)查詢,需要的朋友可以參考下2023-10-10
Java中ArrayList和LinkedList的遍歷與性能分析
這篇文章主要給大家介紹了ArrayList和LinkedList這兩種list的五種循環(huán)遍歷方式,各種方式的性能測(cè)試對(duì)比,根據(jù)ArrayList和LinkedList的源碼實(shí)現(xiàn)分析性能結(jié)果,總結(jié)結(jié)論。相信對(duì)大家的理解和學(xué)習(xí)具有一定的參考價(jià)值,有需要的朋友們下面跟著小編一起來學(xué)習(xí)學(xué)習(xí)吧。2016-12-12
圖解Java經(jīng)典算法快速排序的原理與實(shí)現(xiàn)
快速排序是基于二分的思想,對(duì)冒泡排序的一種改進(jìn)。主要思想是確立一個(gè)基數(shù),將小于基數(shù)的數(shù)放到基數(shù)左邊,大于基數(shù)的數(shù)字放到基數(shù)的右邊,然后在對(duì)這兩部分進(jìn)一步排序,從而實(shí)現(xiàn)對(duì)數(shù)組的排序2022-09-09

