国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

Spring注解 TX聲明式事務(wù)實(shí)現(xiàn)過程解析

瀏覽:71日期:2023-09-12 18:49:35

環(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對配置類做了特殊處理,多次調(diào)用給容器中加組件的方法,其實(shí)是從容器中找組件,并不會重新添加 return new JdbcTemplate(dataSource()); }}

添加數(shù)據(jù)訪問層、業(yè)務(wù)層

@Repositorypublic 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); }}

@Servicepublic class UserService { @Autowired private UserDao userDao; public void saveUser(){ String name = 'jack11'; Integer age = 19; userDao.saveUser(name, age); }}

添加測試類

public class TxTest { @Test public void test(){ ApplicationContext context = new AnnotationConfigApplicationContext(TxConfig.class); UserService userService = (UserService) context.getBean('userService'); userService.saveUser(); }}

事務(wù)問題

此時基本環(huán)境已經(jīng)搭建好了,點(diǎn)擊運(yùn)行,數(shù)據(jù)可以成功插入但是還沒有配置事務(wù),沒有事務(wù)回滾會造成某些情況下數(shù)據(jù)出錯。在Spring注解中,可以在需要添加事務(wù)的方法或類上加@Transactional,并且開啟事務(wù)管理功能,即@EnableTransactionManagement,代碼如下:

修改UserService 的 saveUser 方法

@Transactionalpublic void saveUser(){ String name = 'jack11'; Integer age = 19; userDao.saveUser(name, age); //模擬異常 int i = 1 / 0;}

在配置類加上@EnableTransactionManagement

@Configuration@ComponentScan('com.spring.tx')@EnableTransactionManagementpublic class TxConfig {//省略數(shù)據(jù)源、jdbcTemplate的配置}

再次運(yùn)行測試方法,會發(fā)現(xiàn)報(bào)錯了,但不是我們模擬的異常報(bào)錯,控制臺提示找不到bean

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ’org.springframework.transaction.PlatformTransactionManager’ available

我們還需要注冊一個事務(wù)管理器來管理事務(wù),PlatformTransactionManager有很多實(shí)現(xiàn)類,在Spring 中 JdbcTemplate、Mybatis應(yīng)該使用 DataSourceTransactionManager

Spring注解 TX聲明式事務(wù)實(shí)現(xiàn)過程解析

在配置類中再注冊一個組件,運(yùn)行,事務(wù)生效

/** * 注冊事務(wù)管理器 */@Beanpublic PlatformTransactionManager platformTransactionManager() throws PropertyVetoException { return new DataSourceTransactionManager(dataSource());}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 楚雄市| 南和县| 南投市| 嘉兴市| 南川市| 桂阳县| 南宁市| 大埔区| 项城市| 搜索| 舞阳县| 东乌珠穆沁旗| 临汾市| 九江县| 密山市| 施秉县| 静安区| 莎车县| 长汀县| 开平市| 永仁县| 阜平县| 鄢陵县| 城市| 吐鲁番市| 镶黄旗| 丰宁| 无棣县| 泽普县| 班玛县| 陇南市| 宾阳县| 安吉县| 松阳县| 绥阳县| 潞城市| 内乡县| 宁蒗| 潞西市| 西青区| 赤水市|