Spring框架JdbcTemplate數(shù)據(jù)庫事務(wù)管理完全注解方式
Spring JdbcTemplate事務(wù)注解
配置類方式配置
在之前的操作中,相關(guān)的配置還是寫在了 xml 配置文件中。現(xiàn)在,使用配置類的方式進行配置。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="com.pingguo.spring5"></context:component-scan>
<!--引入外部屬性文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置連接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${prop.driverClass}"></property>
<property name="url" value="${prop.url}"></property>
<property name="username" value="${prop.username}"></property>
<property name="password" value="${prop.password}"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--注入datasource-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入數(shù)據(jù)源-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--開啟事務(wù)注釋-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>完全注解方式
一、創(chuàng)建配置類
把 xml 里的配置在配置類里用注解方式實現(xiàn)。
package com.pingguo.spring5.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
@Configuration // 聲明配置類
@ComponentScan(basePackages = "com.pingguo.spring5") // 開啟注解掃描
@EnableTransactionManagement // 開啟事務(wù)
public class TxConfig {
// 創(chuàng)建數(shù)據(jù)庫連接池
@Bean
public DruidDataSource getDruidDataSource() {
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
druidDataSource.setUrl("jdbc:mysql://223.31.222.111:3306/shop");
druidDataSource.setUsername("root");
druidDataSource.setPassword("123456");
return druidDataSource;
}
// 創(chuàng)建 JdbcTemplate 對象
@Bean
public JdbcTemplate getJdbcTemplate(DataSource dataSource) {
JdbcTemplate jdbcTemplate = new JdbcTemplate();
// 注入 dataSource
jdbcTemplate.setDataSource(dataSource);
return jdbcTemplate;
}
// 創(chuàng)建事務(wù)管理器的對象
@Bean
public DataSourceTransactionManager getDataSourceTransactionManager(DataSource dataSource) {
DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
transactionManager.setDataSource(dataSource);
return transactionManager;
}
}二、測試注解方式的事務(wù)管理
修改下測試方法,使用 AnnotationConfigApplicationContext 來讀取配置類。
public class TestTrans {
@Test
public void testJdbc() {
ApplicationContext context =
new AnnotationConfigApplicationContext(TxConfig.class);
UserService userService = context.getBean("userService", UserService.class);
userService.accountMoney();
}
}執(zhí)行一下:
八月 08, 2021 8:49:35 上午 com.alibaba.druid.pool.DruidDataSource info
信息: {dataSource-1} inited
Process finished with exit code 0查看數(shù)據(jù)表數(shù)據(jù)的修改情況。

成功。
以上就是Spring框架JdbcTemplate數(shù)據(jù)庫事務(wù)管理完全注解方式的詳細內(nèi)容,更多關(guān)于Spring JdbcTemplate事務(wù)注解的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Mybatis?Plus?QueryWrapper復(fù)合用法詳解
這篇文章主要介紹了Mybatis?Plus?QueryWrapper復(fù)合用法詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教。2022-01-01
深入了解SparkSQL中數(shù)據(jù)的加載與保存
這篇文章主要為大家詳細介紹了SparkSQL中數(shù)據(jù)的加載與保存的相關(guān)知識,文中的示例代碼講解詳細,具有一定的學(xué)習(xí)價值,感興趣的小伙伴可以了解下2023-11-11
詳談Java中net.sf.json包關(guān)于JSON與對象互轉(zhuǎn)的坑
下面小編就為大家分享一篇Java中net.sf.json包關(guān)于JSON與對象互轉(zhuǎn)的坑,具有很好的參考價值,希望對大家有所幫助2017-12-12
SpringBoot實現(xiàn)動態(tài)多線程并發(fā)定時任務(wù)
這篇文章主要為大家詳細介紹了SpringBoot實現(xiàn)動態(tài)多線程并發(fā)定時任務(wù),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-05-05

