在Spring 中使用注解配置 Transaction (事务) ,如果会使用XML配置方式,那么就会发现注解更加方便高效。
XML进行必要配置
- 配置事务管理器
<bean id="cPDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/spring_db?useUnicode=true&characterEncoding=UTF-8" />
<property name="user" value="root" />
<property name="password" value="root" />
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入DataSource -->
<property name="dataSource" ref="cPDataSource"></property>
</bean>
- 开启事务注解
<!-- 开启事务注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
上面两步,配置基本完成,下面就是添加事务
在Spring 中添加事务
在Spring 中向一个类中添加事务也是非常方便的,只要在类上添加@Transactional
注解就可以可。
示例代码
<?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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="cPDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/spring_db?useUnicode=true&characterEncoding=UTF-8" />
<property name="user" value="root" />
<property name="password" value="root" />
</bean>
<!-- 1.配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入DataSource -->
<property name="dataSource" ref="cPDataSource"></property>
</bean>
<!-- 2. 开启事务注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="cPDataSource" />
</bean>
<bean id="userService" class="com.cfox.spring.UserService">
<property name="userDao" ref="userDao"/>
</bean>
<bean id="userDao" class="com.cfox.spring.UserDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
</beans>
@Transactional
public class UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void updateUserMoney() {
userDao.updateMoney("王五", 1000);
int a = 10 / 0;
userDao.updateMoney("李四", -1000);
}
}
public class UserDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void updateMoney(String name, int money) {
String sql = "UPDATE user_acc SET salary=salary+? WHERE user_name=?";
int row = jdbcTemplate.update(sql, money, name);
System.out.println(row);
}
}
网友评论