spring事务管理的配置方式

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>

注解的方式

  1. 配置事务管理器
  2. 配置事务注解
  3. 在要使用事务的方法所在类上面添加注解@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;
	}

}

转载于:https://my.oschina.net/liujin521/blog/1550555

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值