網(wǎng)盤做網(wǎng)站服務器網(wǎng)絡推廣營銷方案100例
SpringBoot之自定義Jackson反序列化日期類型轉換配置類
文章目錄
- SpringBoot之自定義Jackson反序列化日期類型轉換配置類
- 1. SpringBoot版本
- 2. 統(tǒng)一事務管理配置類
- 3. 主啟動類加入開啟事務的注解
統(tǒng)一事務管理配置
1. SpringBoot版本
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.5.RELEASE</version></parent>
<dependencies><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.9.7</version></dependency></dependencies>
2. 統(tǒng)一事務管理配置類
package com.yuan.webframework.config;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.interceptor.DefaultTransactionAttribute;
import org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource;
import org.springframework.transaction.interceptor.TransactionInterceptor;/*** <p>* Description: 統(tǒng)一事務管理配置* </p>** @author jinshengyuan* @since 2022/8/5 16:35*/
@Aspect
@Configuration
public class TransactionManagerConfig {//切點表達式private static final String AOP_POINT_EXPRESSION = "execution(* com.zx.*.**.service.impl.*.*(..)) or execution(* org.snaker.engine..*.*(..))";@AutowiredPlatformTransactionManager transactionManager;//注入平臺(Mybatis)事務管理器@Beanpublic TransactionInterceptor txAdvice(){//增刪改DefaultTransactionAttribute txRequired = new DefaultTransactionAttribute();txRequired.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);txRequired.rollbackOn(new Throwable());txRequired.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);//除了指定前綴開頭的以外,其他方法也支持事務DefaultTransactionAttribute txRequiredAll = new DefaultTransactionAttribute();txRequiredAll.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);txRequiredAll.rollbackOn(new Throwable());txRequiredAll.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);//查DefaultTransactionAttribute txRequiredReadOnly = new DefaultTransactionAttribute();txRequiredReadOnly.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);txRequiredReadOnly.setReadOnly(true);NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource();//切入點切入以下的方法為事務方法source.addTransactionalMethod("add*",txRequired);source.addTransactionalMethod("save*",txRequired);source.addTransactionalMethod("insert*",txRequired);source.addTransactionalMethod("update*",txRequired);source.addTransactionalMethod("modify*",txRequired);source.addTransactionalMethod("delete*",txRequired);source.addTransactionalMethod("change*",txRequired);source.addTransactionalMethod("move*",txRequired);source.addTransactionalMethod("remove*",txRequired);source.addTransactionalMethod("submit*",txRequired);source.addTransactionalMethod("distribute*",txRequired);source.addTransactionalMethod("cancel*",txRequired);source.addTransactionalMethod("batch*",txRequired);source.addTransactionalMethod("sync*",txRequired);source.addTransactionalMethod("set*",txRequired);source.addTransactionalMethod("*",txRequiredAll);//切入點切入的以下的方法為只讀事務方法source.addTransactionalMethod("get*",txRequiredReadOnly);source.addTransactionalMethod("query*",txRequiredReadOnly);source.addTransactionalMethod("select*",txRequiredReadOnly);source.addTransactionalMethod("count*",txRequiredReadOnly);source.addTransactionalMethod("find*",txRequiredReadOnly);source.addTransactionalMethod("search*",txRequiredReadOnly);source.addTransactionalMethod("is*",txRequiredReadOnly);return new TransactionInterceptor(transactionManager,source);}//事務切入點@Beanpublic Advisor txAdviceAdvisor(){AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();pointcut.setExpression(AOP_POINT_EXPRESSION);return new DefaultPointcutAdvisor(pointcut,txAdvice());}}
3. 主啟動類加入開啟事務的注解
如主啟動類中加入
@EnableTransactionManagement
注解,如下
package com.yuan;import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;@Slf4j
@EnableTransactionManagement
@SpringBootApplication
public class MyApplication {public static void main(String[] args) throws UnknownHostException {SpringApplication.run(MyApplication.class, args);}
}