spring boot hikari mybatis-plus多数据源

一 数据库连接的配置参数:

spring:
  datasource:
    local:
      jdbc-url: jdbc:mysql://localhost:3306/local?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false
      username: root
      password: root
      maximum-pool-size: 5
    big-data-pool:
      jdbc-url: jdbc:mysql://localhost:3306/bigDataPool?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false
      username: root
      password: root
      maximum-pool-size: 3
    hikari:
      minimum-idle: 3
      maximum-pool-size: 10
      auto-commit: true
      idle-timeout: 30000
      pool-name: DatebookHikariCP
      max-lifetime: 1800000
      connection-timeout: 30000
      connection-test-query: SELECT 1

二 将配置信息注入

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

/**
 * @description:
 * @author ryr
 * @Date: 2019-12-06 16:23
 */
@EnableTransactionManagement
@Configuration
public class DataSources {

    @Bean("localDataSource")
    @Primary
    @ConfigurationProperties(prefix="spring.datasource.local")
    public DataSource localDataSource() {
        return DataSourceBuilder.create().type(HikariDataSource.class).build();
    }

    @Bean("poolDataSource")
    @Qualifier("bigDataPool")
    @ConfigurationProperties(prefix="spring.datasource.big-data-pool")
    public DataSource poolDataSource() {
        return DataSourceBuilder.create().type(HikariDataSource.class).build();
    }
}

三 动态数据源配置

刚开始使用的是SqlSessionFactoryBean结果导致mybatis-plus的内置的增删改查方法不能用,换为

MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();后正常

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
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 org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

/**
 * @description:
 * @author: ryr
 * @date: 2019-12-06 16:13
 */
@Configuration
@ConditionalOnClass({ SqlSessionFactory.class, SqlSessionFactoryBean.class })
public class DataSourceConfig {

    @Configuration
    @MapperScan(basePackages = {"com.ryr.mapper"}, sqlSessionFactoryRef = "localSessionFactory")
    public static class LocalDataSource {
        private final DataSource localDataSource;

        public LocalDataSource(@Qualifier("localDataSource") DataSource localDataSource) {
            this.localDataSource = localDataSource;
        }
        @Bean
        @Primary //主数据库
        public SqlSessionFactory localSessionFactory() throws Exception {
            MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
            bean.setDataSource(localDataSource);
            bean.setPlugins(paginationInterceptor()); //添加分页插件
            bean.setConfigLocation(new PathMatchingResourcePatternResolver().getResource("classpath:mybatis/config/mybatis-config.xml")); //使用动态数据源后spring boot的默认数据源配置失效, 所以要配置mapper xml和mybatis的配置xml
            bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/**/*Mapper.xml"));
            return bean.getObject();
        }
        /** 事务配置 */
        @Bean
        public PlatformTransactionManager localTransactionManager(){
            return new DataSourceTransactionManager(localDataSource);
        }
        /** mybatis-plus分页插件配置 */
        @Bean
        public PaginationInterceptor paginationInterceptor() {
            return new PaginationInterceptor();
        }
    }

    @Configuration
    @MapperScan(basePackages = "com.ryr.pool", sqlSessionFactoryRef = "poolSessionFactory")
    public static class PoolDataSource {
        private final DataSource poolDataSource;
        // @Qualifier("bigDataPool") 非主数据库
        public PoolDataSource(@Qualifier("bigDataPool") DataSource poolDataSource) {
            this.poolDataSource = poolDataSource;
        }
        @Bean
        public SqlSessionFactory poolSessionFactory() throws Exception {
            MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
            bean.setDataSource(poolDataSource);
            bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/pool/**/*Mapper.xml"));
            return bean.getObject();
        }
        @Bean
        public SqlSessionTemplate localSqlSessionTemplate() throws Exception {
            return new SqlSessionTemplate(poolSessionFactory());
        }
        @Bean
        public PlatformTransactionManager localTransactionManager(){
            return new DataSourceTransactionManager(poolDataSource);
        }
    }
}
 

你可能感兴趣的:(spring,boot,java)