给Springboot的增删改查加上事务功能

在DB操作时事务功能可以保证数据的一致性,所以一般我们在项目中DB操作一般都是用事务,springboot工程的增删改查加上事务功能,比较简单,网上收到一大推,整理一下,以便日后参考,懒人计划....

使用步骤:

一,引入依赖 JAR 包;

<!--依赖Jar包-->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-aop</artifactId>
	<!-- <version>自定义用哪个版本<version> -->
</dependency>

二,定义切面类,对象实例纳入spring容器中

package com.xx.yy.zz.aop;

import org.aspectj.lang.annotation.Aspect;
import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.intercepter.DefaultTransactionAttribute;
import org.springframework.transaction.intercepter.NameMathTransactionAttributeSource;
import org.springframework.transaction.intercepter.TransactionInterceptor;

@Aspect
@Configuration
public class TransactionAdviceConfig {
	
	private static final String AOP_POINTCUT_EXPRESSION="execution(* com.**.service.*.*(..))";
	
	@Autowired
	private PlatformTransactionManager transactionManager;
	
	//指定符合表达式的包下各类的哪些方法应用事务功能
	@Bean
	public TransactionInterceptor txAdvice(){
		DefaultTransactionAttribute txAttr_REQUIRED = new DefaultTransactionAttribute();
		txAttr_REQUIRED.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
		DefaultTransactionAttribute txAttr_REQUIRED_READONLY = new DefaultTransactionAttribute();
		txAttr_REQUIRED_READONLY.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
		txAttr_REQUIRED_READONLY.setReadOnly(true);
		NameMathTransactionAttributeSource source = new NameMathTransactionAttributeSource();
		source.addTransactionalMethod("save*",txAttr_REQUIRED);
		source.addTransactionalMethod("insert*",txAttr_REQUIRED);
		source.addTransactionalMethod("delete*",txAttr_REQUIRED);
		source.addTransactionalMethod("update*",txAttr_REQUIRED);
		source.addTransactionalMethod("remove*",txAttr_REQUIRED);
		source.addTransactionalMethod("get*",txAttr_REQUIRED_READONLY);
		source.addTransactionalMethod("query*",txAttr_REQUIRED_READONLY);
		source.addTransactionalMethod("select*",txAttr_REQUIRED_READONLY);
		return new TransactionInterceptor(transactionManager,source);
		
	}	
	
	//定义切入点使用什么意见操作,具体意见参见txAdvice()方法定义
	@Bean
	public Advisor txAdviceAdvisor(){
		AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
		pointcut.setExpression(AOP_POINTCUT_EXPRESSION);
		return new DefaultPointcutAdvisor(pointcut,txAdvice());
	}
	
}

三,事务注解 @Transactional  的使用

1,service接口定义

//文件名:XxxService.java
package com.xx.yy.zz.service;
import org.springframework.transaction.annotation.Transactional;
//more import...

//接口类
public interface XxxService {
	
	public Integer deleteById(Integer recId);
	
}

2,service接口实现类定义

//当然可以在实现类中使用注解方式加以应用事务到方法中;
//这种主要是应用那些方法名称定义不符合上面txAdvice()方法中定义的方法
//当然方法名称定义符合上面txAdvice()方法定义的也可以使用该注解
//接口实现类
//文件名:XxxServiceImpl.java
package com.xx.yy.zz.service.impl;
import com.xx.yy.zz.service.XxxService;
//more import...

public class XxxServiceImpl implements XxxService {
		
	//加注解方式应用事务功能
	@Transactional 
	@Override
	public Integer deleteById(Integer recId) {
		//...code...
	}
		
}

欢迎拍砖讨论......

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章