spring boot mybatis plus 多数据库 多数据源


spring boot mybatis plus 多数据库 多数据源

DataSourceConfig

package com.xxx.xxx.config;

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 javax.sql.DataSource;

/**
 * @author xxx
 */
@Configuration
public class DataSourceConfig {

    /**
     * SQL Server 的DataSource实例
     *
     * @return
     */
    @Primary //配置一个主连接
    @Bean(name = DataBaseModel.DATA_SQLSERVER)
    @Qualifier(DataBaseModel.DATA_SQLSERVER)
    @ConfigurationProperties(prefix = DataBaseModel.DATA_SQLSERVER_PREFIX)
    public DataSource SQLServerDataSource() {
        return DataSourceBuilder.create().build();
    }


    /**
     * SQL Server 的DataSource实例 - WMS
     *
     * @return
     */
    //@Primary //配置一个主连接
    @Bean(name = DataBaseModel.DATA_SQLSERVER_WMS)
    @Qualifier(DataBaseModel.DATA_SQLSERVER_WMS)
    @ConfigurationProperties(prefix = DataBaseModel.DATA_SQLSERVER_WMS_PREFIX)
    public DataSource SQLServerDataSourceWms() {
        return DataSourceBuilder.create().build();
    }


    /**
     * MYSQL 的DataSource实例
     *
     * @return
     */
    @Bean(name = DataBaseModel.DATA_MYSQL)
    @Qualifier(DataBaseModel.DATA_MYSQL)
    @ConfigurationProperties(prefix = DataBaseModel.DATA_MYSQL_PREFIX)
    public DataSource MYSQLDataSource() {
        return DataSourceBuilder.create().build();
    }
}

定义配置文件application.properties中的key名称 DataBaseModel

package com.xxx.xxx.config;

public interface DataBaseModel {


    /**
     * SQL Server 数据库
     */
    String DATA_SQLSERVER ="SQLServerDataSource";

    /**
     * SQL Server 数据库
     */
    String DATA_SQLSERVER_PREFIX ="spring.datasource.sqlserver";

    /**
     * SQL Server 数据库 - Mapper
     * 这里定义扫描 SQL Server 数据库的mapper文件目录
     */
    String DATA_SQLSERVER_MAPPER_PATH = "com.xxx.xxx.mapper.sqlserver";


    /**
     * MySQL 数据库
     */
    String DATA_MYSQL ="MYSQLDataSource";

    /**
     * MySQL 数据库
     */
    String DATA_MYSQL_PREFIX  ="spring.datasource.mysql";

    /**
     * MySQL 数据库 - Mapper
     * 这里定义扫描 mysql 数据库的mapper文件目录
     */
    String DATA_MYSQL_MAPPER_PATH = "com.xxx.xxx.mapper.mysql";

}

SQLServer 数据库链接配置文件 SQLServerDataSourceConfig

package com.xxx.xxx.config;

import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

/**
 * @author xxx
 */
@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = DataBaseModel.DATA_SQLSERVER_MAPPER_PATH, sqlSessionTemplateRef  = "SQLServerSqlSessionTemplate")
public class SQLServerDataSourceConfig {


    @Autowired
    private MybatisPlusInterceptor mybatisPlusInterceptor;

    @Bean(name = "SQLServerSqlSessionFactory")
    @Primary
    public SqlSessionFactory SQLServerSqlSessionFactory(@Qualifier(DataBaseModel.DATA_SQLSERVER) DataSource dataSource) throws Exception {


        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dataSource);

//        关键代码 设置 MyBatis-Plus 分页插件
        Interceptor[] plugins = {mybatisPlusInterceptor};
        sqlSessionFactory.setPlugins(plugins);

        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setJdbcTypeForNull(JdbcType.NULL);
        configuration.setMapUnderscoreToCamelCase(true);
        configuration.setCacheEnabled(false);
        sqlSessionFactory.setConfiguration(configuration);

        return sqlSessionFactory.getObject();
    }

    @Bean(name = "SQLServerTransactionManager")
    @Primary
    public DataSourceTransactionManager SQLServerTransactionManager(@Qualifier(DataBaseModel.DATA_SQLSERVER) DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "SQLServerSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate SQLServerSqlSessionTemplate(@Qualifier("SQLServerSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}

MYSQL 数据库链接配置文件 MYSQLDataSourceConfig

package com.xxx.xxx.config;

import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * @author xxx
 */
@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = DataBaseModel.DATA_MYSQL_MAPPER_PATH, sqlSessionTemplateRef  = "MYSQLSqlSessionTemplate")
public class MYSQLDataSourceConfig {


    @Autowired
    private MybatisPlusInterceptor mybatisPlusInterceptor;

    @Bean(name = "MYSQLSqlSessionFactory")
    @Primary
    public SqlSessionFactory MYSQLSqlSessionFactory(@Qualifier("MYSQLDataSource") DataSource dataSource) throws Exception {

        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dataSource);

//        关键代码 设置 MyBatis-Plus 分页插件
        Interceptor[] plugins = {mybatisPlusInterceptor};
        sqlSessionFactory.setPlugins(plugins);

        MybatisConfiguration configuration = new MybatisConfiguration();

        configuration.setJdbcTypeForNull(JdbcType.NULL);
        configuration.setMapUnderscoreToCamelCase(true);
        configuration.setCacheEnabled(false);
        sqlSessionFactory.setConfiguration(configuration);

        return sqlSessionFactory.getObject();
    }

    @Bean(name = "MYSQLTransactionManager")
    @Primary
    public DataSourceTransactionManager MYSQLTransactionManager(@Qualifier("MYSQLDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "MYSQLSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate MYSQLSqlSessionTemplate(@Qualifier("MYSQLSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}

文件 application.properties 中的配置


# sqlserver
spring.datasource.sqlserver.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.sqlserver.url=jdbc:sqlserver://192.168.1.22;database=dbname
spring.datasource.sqlserver.jdbc-url=jdbc:sqlserver://192.168.1.22;database=dbname
spring.datasource.sqlserver.username=sa
spring.datasource.sqlserver.password=12345678

# mysql
spring.datasource.mysql.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.mysql.url=jdbc:mysql://192.168.1.23:3306/dbname?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
spring.datasource.mysql.jdbc-url=jdbc:mysql://192.168.1.23:3306/dbname?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
spring.datasource.mysql.username=root
spring.datasource.mysql.password=12345678

Mybatis Plus 配置文件 MybatisPlusConfig

package com.xxx.xxx.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class MybatisPlusConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {

        PaginationInnerInterceptor paginationInterceptor = new PaginationInnerInterceptor();
        //分页 单页限制,默认无限制
        //paginationInterceptor.setMaxLimit(1000);

        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();

        interceptor.addInnerInterceptor(paginationInterceptor);

        return interceptor;
    }

    /**
     * MyBatis plus 乐观锁
     *
     * @return
     */
    @Bean
    public OptimisticLockerInnerInterceptor optimisticLocker() {
        return new OptimisticLockerInnerInterceptor();
    }
}

注意:这个 MybatisPlusConfig 是新版本的配置文件。版本 >= 3.4.0