Spring的事务管理
有关事务的概念可以参考:MySQL事务。
Spring当中事务管理的API接口:
- 参考文章:Spring事物管理简介。
PlatformTransactionManager
事务管理器。TransactionDefinition
事务定义信息(隔离,传播、超时、只读)。TransactionStatus
事务具体运行状态。
Spring对事务的管理的方式:
- 基于XML配置形式实现。
- 基于注解形式实现。
基于XML形式的事务管理
- 思想:其实就是AOP模式的思想实现。
- 这里仅说明XML文件的写法,不做具体的测试。
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.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="dataSource" class="com.mchange.v2.c3p0.DriverManagerDataSource">
<!--配置连接数据库的核心配置4个参数-->
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="user" value="root"/>
<property name="password" value="123456"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"/>
</bean>
<!--第一步:配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入数据源DataSource-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--第二步:配置事务增强-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!--做事务处理-->
<tx:attributes>
<!--
设定进行事务操作方法的匹配规则
add*指定增强的方法名 表示add开头的都可以
propagation:事务传播行为
isolation:指定隔离级别。。。
-->
<tx:method name="add*" propagation="REQUIRED" isolation="DEFAULT"/>
</tx:attributes>
</tx:advice>
<!--第三步:配置切面-->
<aop:config>
<!--切入点-->
<aop:pointcut id="pointcut" expression="execution(* com.tulun.dao.UserDao.*(..))"/>
<!--切面-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
</aop:config>
</beans>
基于注解形式的事务管理器
- 这里仅说明注解形式的写法,不做具体的测试。
- 1、开启事务注解
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.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="dataSource" class="com.mchange.v2.c3p0.DriverManagerDataSource">
<!--配置连接数据库的核心配置4个参数-->
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="user" value="root"/>
<property name="password" value="123456"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mybatis"/>
</bean>
<!--基于注解形式的事务管理器-->
<!--第一步:配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入数据源DataSource-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--第二步:配置事务注解:开启事务注解-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
- 2、配置注解(
@Transactional
)
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Transactional;
@Transactional
public class UserDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Transactional
public User getUserById(Long id){
//查询SQL
String sql = "select * from user where id = ?";
User user = jdbcTemplate.queryForObject(sql, new Object[]{id}, new UserMapper());
//使用jdbcTemplate模板来获取数据库数据
return user;
}
}
@Transactional
表示是事务,该注解可以添加在类上或者是方法上,添加在类上即该类的所有方法都使用事务,添加在方法上即当前的方法是支持事务的。