前言
提示:spring 实现事务有两种方式(XML和注解)。
一、注解实现过程
与XML实现的方式差不多,只是在使用事务时有一定的不同。
二、使用步骤
1.引入需要的包
代码如下(示例):
<!-- 引入JDBC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.6</version>
</dependency>
<!-- 连接池 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.8.0</version>
</dependency>
2.配置XMR注入
代码如下(示例):
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:property-placeholder location="classpath:common.properties"/>
<!--配置数据库连接信息,需要引入dbcp架包-->
<bean id="dataSource" class="${dataSource}">
<property name="driverClassName" value="${driverClass}"></property>
<property name="url" value="${jdbcUrl}"></property>
<property name="username" value="${user}"></property>
<property name="password" value="${password}"></property>
</bean>
<!-- 配置JdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置实体类 -->
<bean id="UserTmpl" class="org.example.mysql.service.tmpl.UserTmpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
<!-- 配置事务通知 -->
<!--<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!– 根据方法名指定事务的属性 –>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>-->
<!-- 配置事务切入点,以及把事务切入点和事务属性关联起来 -->
<!--<aop:config>
<aop:pointcut expression="execution( * org.example.mysql.service.tmpl.UserTmpl.*(..))" id="txPointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>-->
</beans>
注释掉的xml配置即为xml实现事务的区别
3.实现类
代码如下(示例):
@Transactional(readOnly = false,propagation = Propagation.REQUIRED,rollbackFor = {SQLException.class,RuntimeException.class})
@Override
public int updateUser(Users users,long userId) {
int update;
//修改数据
String sql = " update te_users set integral =integral+1 where user_id= ?";
Object[] params = new Object[]{userId};
update = jdbcTemplate.update(sql, params);
sql = " update te_users set balance =? where user_id= ?";
params = new Object[]{users.getToken(),userId};
jdbcTemplate.update(sql, params);
return update;
}
主要是对@Transactional注解的使用,涉及事务的传播行为和隔离级别