spring事务管理
xml的配置方式
spring 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: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">
</beans>
配置事务管理器
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入dataSource-->
<property name="dataSource" ref="dataSource"></property>
</bean>
配置事务增强(增强的事务逻辑)
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!--做事务操作-->
<tx:attributes>
<!--设置进行事务操作的方法匹配规则(可以配置多个) method标签中添加propagation可以指定事务的隔离级别,不添加的话会有默认的隔离级别-->
<tx:method name="insert*" propagation=“REQUIRED”/>
<tx:method name="update*"/>
</tx:attributes>
</tx:advice>
配置切面(切面:通俗来说把增强应用到方法的过程叫做切面)
<aop:config>
<!--切入点-->
<aop:pointcut expression="execution(* com.zhangjinbang.service.TestService.*(..))" id="pointcutTest"/>
<!--切面-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcutTest">
</aop:config>
注解的方式
- 配置事务管理器
- 配置事务注解
- 在要使用事务的方法所在类上面添加注解@Transactional
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入dataSource-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--开启事务注解-->
<tx:annotation-driven transaction-manager="transactionManager"/>
@Transactional
public class TestService{
}
java配置类
package config;
import org.apache.commons.dbcp2.BasicDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@EnableTransactionManagement
@ComponentScan(basePackages = { "com.zhangjinbang.dao" }, excludeFilters = {
@Filter(type = FilterType.ANNOTATION, value = EnableWebMvc.class) })
public class RootConfig {
// 配置数据源
@Bean
public BasicDataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/jinbang?autoReconnect=true&autoReconnectForPools=true");
dataSource.setUsername("root");
dataSource.setPassword("root");
dataSource.setMaxIdle(5);
return dataSource;
}
// 配置事务管理
@Bean
public DataSourceTransactionManager dataSourceTransaction() {
return new DataSourceTransactionManager(this.dataSource());
}
// sqlSessionFactory
@Bean(name = "sqlSessionFactory")
public SqlSessionFactoryBean sqlSessionFactory() {
SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
// 加载全局配置文件
sqlSessionFactory
.setConfigLocation(new DefaultResourceLoader().getResource("classpath:config/mybatis-config.xml"));
// 配置数据源
sqlSessionFactory.setDataSource(this.dataSource());
// 数据源
return sqlSessionFactory;
}
// 配置mapper 扫描器
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
// 设置扫描包的路径,如果有多个,中间使用半角逗号隔开。
mapperScannerConfigurer.setBasePackage("com.zhangjinbang.dao");
mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
return mapperScannerConfigurer;
}
}