ITeye博客

现有web系统替换成Spring Boot2框架 之3 配置数据库驱动,事务控制

sanry · 2019-02-20 15:06 · 编程语言 · Markdown

3.1 配置mysql驱动

 <dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

</dependency>

 

3.2 配置数据源

具体配置参数后面补充application.properties添加如下基本配置:

 

spring.datasource.url=jdbc:mysql://127.0.0.1/databasename

spring.datasource.username=uname

spring.datasource.password=123

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.datasource.hikari.autoCommit=false

 

3.3 pom.xml添加依赖:

 

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-jdbc</artifactId>

</dependency>

 

<!--springBoot 的aop-->

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-aop</artifactId>

</dependency>

 

3.4 事务管理

以*Service结尾的java类以update、add、del等开头的方法上面增加控制事务的提交与回滚,配置如下:

 

 

import java.util.Collections;

 

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.RollbackRuleAttribute;

import org.springframework.transaction.interceptor.RuleBasedTransactionAttribute;

import org.springframework.transaction.interceptor.TransactionInterceptor;

 

@Aspect

@Configuration

public class TransactionAdviceConfig {

 

private static final String AOP_POINTCUT_EXPRESSION = "execution(* bai.*..*Service.*(..)) || execution(* bai.*..*.*Service.*(..))";

@Autowired

private PlatformTransactionManager transactionManager;

@Bean

public TransactionInterceptor txAdvice() {

RuleBasedTransactionAttribute txAttr_REQUIRED =new RuleBasedTransactionAttribute();

txAttr_REQUIRED.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);

txAttr_REQUIRED.setRollbackRules(Collections.singletonList(new RollbackRuleAttribute(Exception.class)) );


DefaultTransactionAttribute txAttr_REQUIRED_READONLY = new DefaultTransactionAttribute();

txAttr_REQUIRED_READONLY.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);

        txAttr_REQUIRED_READONLY.setReadOnly(true);

        NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource();

        source.addTransactionalMethod("save*", txAttr_REQUIRED);

        source.addTransactionalMethod("del*", txAttr_REQUIRED);

        source.addTransactionalMethod("update*", txAttr_REQUIRED);

        source.addTransactionalMethod("exec*", txAttr_REQUIRED);

        source.addTransactionalMethod("set*", txAttr_REQUIRED);

        source.addTransactionalMethod("get*", txAttr_REQUIRED);

        source.addTransactionalMethod("remove*", txAttr_REQUIRED);

        source.addTransactionalMethod("save*", txAttr_REQUIRED);

        source.addTransactionalMethod("add*", txAttr_REQUIRED);

        source.addTransactionalMethod("create*", txAttr_REQUIRED);

        source.addTransactionalMethod("noTrans*", txAttr_REQUIRED);

        //read only

        source.addTransactionalMethod("query*", txAttr_REQUIRED_READONLY);

        source.addTransactionalMethod("find*", txAttr_REQUIRED_READONLY);

        source.addTransactionalMethod("list*", txAttr_REQUIRED_READONLY);

        source.addTransactionalMethod("count*", txAttr_REQUIRED_READONLY);

        source.addTransactionalMethod("is*", txAttr_REQUIRED_READONLY);

        return new TransactionInterceptor(transactionManager, source);

}


@Bean

public Advisor txAdviceAdvisor() {

        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();

        pointcut.setExpression(AOP_POINTCUT_EXPRESSION);

        return new DefaultPointcutAdvisor(pointcut, txAdvice());

    }

}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。