spring配合jdbc处理事务
spring中实现aop的配置方式很多,在这里配置事务的时候推荐使用:
1.tx前缀的事务标签和aop前缀的标签结合,将切面(事务管理器)织入到切入点上
2.注解进行事务配置
例如1:
spring结合jdbc,事务配置在service层指定方法上,使用tx标签结合aop标签
//使用jdbc实现dao层接口
public class JDBCAccountDaoImpl implements AccountDao{
private JdbcTemplate jdbcTemplate;
//get/set方法
//实现接口中的抽象方法
.....
}
spring_jdbc.xml配置文件主要内容:
//配置连接池对象
jdbc_service1.xml配置文件主要内容:
//配置
<bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg index="0" ref="dataSource"></constructor-arg>
</bean>
<bean id="accountDao" class="com.briup.tran.jdbc.JDBCAccountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
<!-- 配置service层对象 目标对象-->
<bean name="service" class="com.briup.tran.service.AccountServiceImpl">
<!-- 注入dao层对象 -->
<property name="accountDao" ref="accountDao"></property>
</bean>
<!-- 配置jdbc的事务管理器 (实际上,事务管理器就是一个切面) -->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据源,spring的jdbc事务管理器在管理事务时,依赖于JDBC的事务管理机制 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事务拦截器 ,transaction-manager:表示通知织入哪个切面-->
<tx:advice id="transactionInterceptor" transaction-manager="transactionManager">
<tx:attributes>
<!-- *代表所有的方法 -->
<tx:method name="*" propagation="REQUIRED" rollback-for="Throwable"/>
</tx:attributes>
</tx:advice>
<!-- 配置aop -->
<aop:config>
<!-- 配置切入点, 之后可以在事务中在对方法进行细化 -->
<aop:pointcut expression="execution(public * com.briup.tran.service.*.*(..))" id="myPointCut"/>
<!-- 配置事务拦截器在哪一个切入点上起作用 -->
<aop:advisor advice-ref="transactionInterceptor" pointcut-ref="myPointCut"/>
</aop:config>
//测试方法
String path[] = {"com/briup/tran/jdbc/spring_jdbc.xml",
"com/briup/tran/service/jdbc_service1.xml"};
ApplicationContext container =
new ClassPathXmlApplicationContext(path);
AccountService service = (AccountService)container.getBean("service");
service.add(new Account(1,"zs1",1000));
例如2:
spring结合jdbc,事务配置在service层指定方法上,使用注解进行事务配置
和例子1中只有俩处不同
第一处:
service接口的实现类的上面使用@Transactional:
@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Throwable.class)
public class AccountServiceImpl implements AccountService{
.....
}
第二处:
jdbc_service2.xml配置文件主要内容:
<!-- 配置service层对象 目标对象-->
<bean name="service" class="com.briup.tran.service.AccountServiceImpl">
<!-- 注入dao层对象 -->
<property name="accountDao" ref="accountDao"></property>
</bean>
<!-- 配置jdbc的事务管理器 (切面类)-->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 通知spring我们在目标对象中做了事务的注解,并指明使用哪一个事务管理器 -->
<!-- 加入上这个标签后 去目标对象去加入相应的注解就可以了 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
例如3:
spring结合mybatis,事务配置在service层指定方法上,使用tx标签结合aop标签
AccountMapper.xml配置文件主要内容:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- com.briup.tran.dao.AccountDao是我们定义接口的全限定名字 这样就可以使用接口调用映射的SQL语句了 这个名字一定要和接口对应上-->
<mapper namespace="com.briup.tran.dao.AccountDao">
<insert id="save" parameterType="Account">
insert into
t_account(id,name,balance)
values(#{id},#{name},#{balance})
</insert>
<update id="update" parameterType="Account">
update t_account
set
name=#{name},
balance=#{balance}
where id=#{id}
</update>
<delete id="delete" parameterType="Account">
delete from t_account where id=#{id}
</delete>
</mapper>
spring_mybatis.xml配置文件主要内容:
<!-- 配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@localhost:1521:XE</value>
</property>
<property name="username">
<value>briup</value>
</property>
<property name="password">
<value>briup</value>
</property>
</bean>
<!-- 配置sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage" value="com.briup.tran"></property>
<property name="configurationProperties">
<props>
<prop key="cacheEnabled">true</prop>
</props>
</property>
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:com/briup/tran/mybatis/AccountMapper.xml" />
</bean>
<!-- 自动扫描映射接口所在的包 -->
<!-- 将来可以通过接口的名字首字母小写作为beanName,从spring容器中拿出自动生成的该接口的实现类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.briup.tran.dao" />
</bean>
mybatis_service1.xml配置文件主要内容:
<!-- 配置service层对象 目标对象-->
<bean name="service" class="com.briup.tran.service.AccountServiceImpl">
<!-- 注入dao层对象 -->
<property name="accountDao" ref="accountDao"></property>
</bean>
<!-- 配置jdbc的事务管理器 (切面类) 适用于mybatis-->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事务拦截器 -->
<tx:advice id="transactionInterceptor" transaction-manager="transactionManager">
<tx:attributes>
<!-- *代表所有的方法
表示与事务属性关联的方法业务(方法名),对切入点进行细化。通配符(*)可以用来指定一批关联到相同的事务属性的方法。
如:'get*'、'find*'、'on*Event'等等-->
<tx:method name="*" propagation="REQUIRED" rollback-for="Throwable"/>
</tx:attributes>
</tx:advice>
<!-- 配置aop -->
<aop:config>
<!-- 配置切入点 -->
<aop:pointcut expression="execution(public * com.briup.tran.service.*.*(..))" id="myPointCut"/>
<!-- 配置事务拦截器在哪一个切入点上起作用 -->
<aop:advisor advice-ref="transactionInterceptor" pointcut-ref="myPointCut"/>
</aop:config>
测试方法:
.....
例如4:
spring结合mybatis,事务配置在service层指定方法上,使用注解进行事务配置
和例子3中只有俩处不同
第一处:
service接口的实现类的上面使用@Transactional:
@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Throwable.class)
public class AccountServiceImpl implements AccountService{
.....
}
第二处:
mybatis_service2.xml配置文件主要内容:
<!-- 配置service层对象 目标对象-->
<bean name="service" class="com.briup.tran.service.AccountServiceImpl">
<!-- 注入dao层对象 -->
<property name="accountDao" ref="accountDao"></property>
</bean>
<!-- 配置jdbc的事务管理器 (切面类) 适用于mybatis-->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 通知spring我们在目标对象中做了事务的注解,并指明使用哪一个事务管理器 -->
<!-- 加入上这个标签后 去目标对象去加入相应的注解就可以了 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?