基于Springboot+MybatisPlus实现多数据源+多数据源的事务管理 2020-12-06

  • 一、多数据源的使用

  • 首先编写配置文件

spring:
  aop:
    proxy-target-class: true
    auto: true
  datasource:
    druid:
      db1:
        type: com.alibaba.druid.pool.DruidDataSource            
        driver-class-name: com.mysql.jdbc.Driver              
        url: jdbc:mysql:数据库连接地址
        username: 账号
        password: 密码
        initialSize: 5
        minIdle: 5
        maxActive: 20
      db2:
        type: com.alibaba.druid.pool.DruidDataSource            
        driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver            
        url: jdbc:sqlserver:数据库链接地址
        username: 账号
        password: 密码
        initialSize: 5
        minIdle: 5
        maxActive: 20
  • 然后创建数据源配置类



import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

/**
 * 创建读取数据源配置到数据库连接池
 *
 * @author 创作人:陈定雄
 * @date 2019/10/26 0:24
 */
@Configuration
public class DataSourceConfig {

    @Bean
    @Primary
    public DataSource dataSource(@Value("${spring.datasource.druid.db1.username}") String username,
                                 @Value("${spring.datasource.druid.db1.password}") String password,
                                 @Value("${spring.datasource.druid.db1.url}") String url,
                                 @Value("${spring.datasource.druid.db1.driver-class-name}") String driverClassName) {
        return createDataSource(username, password, url, driverClassName);
    }

    @Bean
    public DataSource dataSource2(@Value("${spring.datasource.druid.db2.username}") String username,
                                  @Value("${spring.datasource.druid.db2.password}") String password,
                                  @Value("${spring.datasource.druid.db2.url}") String url,
                                  @Value("${spring.datasource.druid.db2.driver-class-name}") String driverClassName) {
        return createDataSource(username, password, url, driverClassName);
    }

    private DataSource createDataSource(String username, String password, String url, String driverClassName) {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setJdbcUrl(url);
        dataSource.setDriverClassName(driverClassName);
        return dataSource;
    }
}


  • 然后创建mybatisPlus配置类



import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * MybatisConfig配置类
 *
  * @author 创作人:陈定雄
 * @date 2019/10/26 0:31
 */
@Configuration
@ConditionalOnClass(value = {PaginationInterceptor.class})
@MapperScan("mapper包路径1(使用.分级)")
@MapperScan("mapper路径2")
public class MybatisPlusConfig {

    //设置分页的bean=====================================================================================================
    @Bean
    public PaginationInterceptor paginationInterceptor() {

        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        // 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求  默认false
        // paginationInterceptor.setOverflow(false);
        // 设置最大单页限制数量,默认 500 条,-1 不受限制
        // paginationInterceptor.setLimit(500);
        // 开启 count 的 join 优化,只针对部分 left join
        paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
        return paginationInterceptor;
    }


    @Configuration
    @MapperScan(basePackages = "mapper包路径2(使用.分级)", sqlSessionTemplateRef = "sqlSessionTemplate")
    public class Db1 {
        @Bean
        @Primary
        public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource dataSource) throws Exception {
            //mybatis应该使用  SqlSessionFactoryBean
            //SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();

            //mybatis-plus应该使用  MybatisSqlSessionFactoryBean  否则无法使用baseMapper
            MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
            sqlSessionFactory.setDataSource(dataSource);
            sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                    .getResources("classpath:mapper包路径2(使用 / 分级)/*.xml"));
            //添加分页功能================================================================================================
            sqlSessionFactory.setPlugins(new Interceptor[]{paginationInterceptor()});
            return sqlSessionFactory.getObject();
        }

        @Bean
        @Primary
        public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
            return new SqlSessionTemplate(sqlSessionFactory);
        }

        @Bean
        @Primary
        public DataSourceTransactionManager dataSourceTransactionManager(@Qualifier("dataSource") DataSource dataSource) {
            return new DataSourceTransactionManager(dataSource);
        }
    }

    @Configuration
    @MapperScan(basePackages = "mapper包路径1(使用.分级)", sqlSessionTemplateRef = "sqlSessionTemplate2")
    public class Db2 {

        @Bean
        public SqlSessionFactory sqlSessionFactory2(@Qualifier("dataSource2") DataSource dataSource) throws Exception {
            //mybatis应该使用  SqlSessionFactoryBean
            //SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();

            //mybatis-plus应该使用  MybatisSqlSessionFactoryBean  否则无法使用baseMapper
            MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
            sqlSessionFactory.setDataSource(dataSource);
            sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                    .getResources("classpath:mapper包路径1(使用 / 分级)/*.xml"));
            //添加分页功能================================================================================================
            sqlSessionFactory.setPlugins(new Interceptor[]{paginationInterceptor()});
            return sqlSessionFactory.getObject();
        }
        @Bean
        public SqlSessionTemplate sqlSessionTemplate2(@Qualifier("sqlSessionFactory2") SqlSessionFactory sqlSessionFactory) throws Exception {
            return new SqlSessionTemplate(sqlSessionFactory);
        }

        @Bean
        public DataSourceTransactionManager dataSourceTransactionManager2(@Qualifier("dataSource2") DataSource dataSource) {
            return new DataSourceTransactionManager(dataSource);
        }
    }
}


  • 以上步骤做完就可以在调用不同的xml的时候使用指定的不同的数据源了


二、多数据源之间数据需要同步 增删改查 时怎么进行事务的管理

  • 创建一个自定义注解用于:事务管理器数组


import java.lang.annotation.*;

/**
 * 

* 多数据源事务注解 *

* * @author 创作人:陈定雄 * @since 2020/11/22 0022 16:55 */ @Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented public @interface MultiDataSourceTransactional { /** * 事务管理器数组 */ String[] transactionManagers() default {"dataSourceTransactionManager", "dataSourceTransactionManager2"}; }
  • 创建一个类用切面拦截重新定义事务的开始与结束


import javafx.util.Pair;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import java.util.Stack;

/**
 * 

* *

* * @author 陈定雄 * @since 2020/11/22 0022 16:56 */ @Component @Aspect public class MultiDataSourceTransactionAspect { /** * 线程本地变量:为什么使用栈?※为了达到后进先出的效果※ */ private static final ThreadLocal>> THREAD_LOCAL = new ThreadLocal<>(); /** * 用于获取事务管理器 */ @Autowired private ApplicationContext applicationContext; /** * 事务声明 */ private DefaultTransactionDefinition def = new DefaultTransactionDefinition(); { // 非只读模式 def.setReadOnly(false); // 事务隔离级别:采用数据库的 def.setIsolationLevel(TransactionDefinition.ISOLATION_DEFAULT); // 事务传播行为 def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); } /** * 切面 */ @Pointcut("@annotation(本类的包路径(使用.分隔开))") public void pointcut() { } /** * 声明事务 * * @param transactional 注解 */ @Before("pointcut() && @annotation(transactional)") public void before(MultiDataSourceTransactional transactional) { System.out.println("===============事务开始==============="); // 根据设置的事务名称按顺序声明,并放到ThreadLocal里 String[] transactionManagerNames = transactional.transactionManagers(); Stack> pairStack = new Stack<>(); for (String transactionManagerName : transactionManagerNames) { // System.out.println("事务名称:"+ transactionManagerName); DataSourceTransactionManager transactionManager = applicationContext.getBean(transactionManagerName, DataSourceTransactionManager.class); TransactionStatus transactionStatus = transactionManager.getTransaction(def); pairStack.push(new Pair(transactionManager, transactionStatus)); } THREAD_LOCAL.set(pairStack); } /** * 提交事务 */ @AfterReturning("pointcut()") public void afterReturning() { // ※栈顶弹出(后进先出) Stack> pairStack = THREAD_LOCAL.get(); while (!pairStack.empty()) { Pair pair = pairStack.pop(); pair.getKey().commit(pair.getValue()); // System.out.println("提交事务:"+ pair.getValue()); } THREAD_LOCAL.remove(); System.out.println("===============事务正常结束==============="); } /** * 回滚事务 */ @AfterThrowing(value = "pointcut()") public void afterThrowing() { // ※栈顶弹出(后进先出) Stack> pairStack = THREAD_LOCAL.get(); while (!pairStack.empty()) { Pair pair = pairStack.pop(); pair.getKey().rollback(pair.getValue()); // System.out.println("回滚事务:"+ pair.getValue()); } THREAD_LOCAL.remove(); System.out.println("===============事务异常,已回滚==============="); } }
  • 然后如果需要使用事务,使用之前自定义的注解加在需要的地方就可以i开启事务了

![

你可能感兴趣的:(基于Springboot+MybatisPlus实现多数据源+多数据源的事务管理 2020-12-06)