給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...
	}
		
}

歡迎拍磚討論......

 

 

 

 

 

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