SpringBoot 配置多資料來源   MyBatis

weixin_34308389發表於2018-12-20

在 SpringBoot 專案中,可能需要使用 MyBatis 對不同資料庫進行操作,而SpringBoot預設配置只是使用單資料來源。
本文主要描述如何配置 SpringBoot 多資料來源 MyBatis。
GitHub https://github.com/oldguys/MultipleDataSourceDemo

Step1: 排除自動裝配。
SpringBoot會自動掃描配置資料來源,需要先排除自動裝配,防止報找不到多重例項,找不到準確引用。

package com.oldguy.example;

import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@SpringBootApplication(exclude = {

DataSourceAutoConfiguration.class,

      MybatisAutoConfiguration.class,

})

public class MultipleDatasourceDemoApplication {

public static void main(String[] args) {

SpringApplication.run(MultipleDatasourceDemoApplication.class, args);

  }

}

Step2: 從SpringBoot中自動裝配程式碼中,取出需要的部分。

package com.oldguy.example.modules.common.configs;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.boot.autoconfigure.MybatisProperties;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
 * @author ren
 * @date 2018/12/20
 */
public abstract class AbstractMybatisConfiguration {

    /**
     *  獲取Template
     * @param properties
     * @param sqlSessionFactory
     * @return
     */
    protected SqlSessionTemplate getSqlSessionTemplate(MybatisProperties properties, SqlSessionFactory sqlSessionFactory) {
        ExecutorType executorType = properties.getExecutorType();
        if (executorType != null) {
            return new SqlSessionTemplate(sqlSessionFactory, executorType);
        } else {
            return new SqlSessionTemplate(sqlSessionFactory);
        }
    }

    /**
     *  獲取 SessionFactory
     * @param dataSource
     * @param properties
     * @param resourceLoader
     * @return
     */
    protected SqlSessionFactoryBean getSqlSessionFactoryBean(DruidDataSource dataSource, MybatisProperties
            properties, ResourceLoader resourceLoader) {
        SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
        factory.setDataSource(dataSource);
        factory.setVfs(SpringBootVFS.class);

        factory.setConfigLocation(resourceLoader.getResource(properties.getConfigLocation()));

        org.apache.ibatis.session.Configuration configuration = properties.getConfiguration();
        if (configuration == null && !StringUtils.hasText(properties.getConfigLocation())) {
            configuration = new org.apache.ibatis.session.Configuration();
        }

        factory.setConfiguration(configuration);
        if (properties.getConfigurationProperties() != null) {
            factory.setConfigurationProperties(properties.getConfigurationProperties());
        }

        if (StringUtils.hasLength(properties.getTypeAliasesPackage())) {
            factory.setTypeAliasesPackage(properties.getTypeAliasesPackage());
        }
        if (StringUtils.hasLength(properties.getTypeHandlersPackage())) {
            factory.setTypeHandlersPackage(properties.getTypeHandlersPackage());
        }
        if (!ObjectUtils.isEmpty(properties.resolveMapperLocations())) {
            factory.setMapperLocations(properties.resolveMapperLocations());
        }
        return factory;
    }

}

Step3: 實現資料模板

配置例項模組 Test1

package com.oldguy.example.configs;

import com.alibaba.druid.pool.DruidDataSource;
import com.oldguy.example.modules.common.configs.AbstractMybatisConfiguration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.MybatisProperties;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ResourceLoader;
import org.springframework.transaction.annotation.EnableTransactionManagement;


/**
 * @author ren
 * @date 2018/12/20
 */
@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = "com.oldguy.example.modules.test1.dao.jpas",
        sqlSessionFactoryRef = "test1SqlSessionFactory",
        sqlSessionTemplateRef = "test1SqlSessionTemplate"
)
public class Test1DataSourceConfiguration extends AbstractMybatisConfiguration {

    @Bean(name = "test1DataSource")
    @ConfigurationProperties(prefix = "test1.datasource")
    public DruidDataSource test1DataSource() {
        return new DruidDataSource();
    }

    @Bean(name = "test1Properties")
    @ConfigurationProperties(prefix = "test1.mybatis")
    public MybatisProperties MybatisProperties() {
        return new MybatisProperties();
    }

    @Bean(name = "test1SqlSessionFactory")
    public SqlSessionFactory sqlSessionFactoryBean(@Qualifier("test1DataSource") DruidDataSource dataSource,
                                                   @Qualifier("test1Properties") MybatisProperties properties,
                                                   ResourceLoader resourceLoader) throws Exception {
        return getSqlSessionFactoryBean(dataSource, properties, resourceLoader).getObject();
    }

    @Bean(name = "test1SqlSessionTemplate")
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("test1Properties") MybatisProperties properties,
                                                 @Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return getSqlSessionTemplate(properties, sqlSessionFactory);
    }

}

配置例項模組 Test2

package com.oldguy.example.configs;

import com.alibaba.druid.pool.DruidDataSource;
import com.oldguy.example.modules.common.configs.AbstractMybatisConfiguration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.MybatisProperties;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ResourceLoader;
import org.springframework.transaction.annotation.EnableTransactionManagement;


/**
 * @author ren
 * @date 2018/12/20
 */
@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = "com.oldguy.example.modules.test2.dao.jpas",
        sqlSessionFactoryRef = "test2SqlSessionFactory",
        sqlSessionTemplateRef = "test2SqlSessionTemplate"
)
public class Test2DataSourceConfiguration extends AbstractMybatisConfiguration {

    @Bean(name = "test2DataSource")
    @ConfigurationProperties(prefix = "test2.datasource")
    public DruidDataSource test2DataSource() {
        return new DruidDataSource();
    }

    @Bean(name = "test2Properties")
    @ConfigurationProperties(prefix = "test2.mybatis")
    public MybatisProperties MybatisProperties() {
        return new MybatisProperties();
    }

    @Bean(name = "test2SqlSessionFactory")
    public SqlSessionFactory sqlSessionFactoryBean(@Qualifier("test2DataSource") DruidDataSource dataSource,
                                                   @Qualifier("test2Properties") MybatisProperties properties,
                                                   ResourceLoader resourceLoader) throws Exception {
        return getSqlSessionFactoryBean(dataSource, properties, resourceLoader).getObject();
    }

    @Bean(name = "test2SqlSessionTemplate")
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("test1Properties") MybatisProperties properties,
                                                 @Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return getSqlSessionTemplate(properties, sqlSessionFactory);
    }

}

Step4:配置 yaml檔案

test1:
  datasource:
    username: root
    password: root
    url: jdbc:mysql://127.0.0.1:3306/multiple_datasource1?useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
  mybatis:
    mapper-locations: classpath:mappers/test1/*.xml
    type-aliases-package: com.oldguy.example.modules.test1.dao.entities;
    config-location: classpath:configs/myBatis-config.xml

test2:
  datasource:
    username: root
    password: root
    url: jdbc:mysql://127.0.0.1:3306/multiple_datasource2?useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
  mybatis:
    mapper-locations: classpath:mappers/test2/*.xml
    type-aliases-package: com.oldguy.example.modules.test2.dao.entities;
    config-location: classpath:configs/myBatis-config.xml

到此完成了MyBatis多資料來源配置
程式碼可以參考 GitHub https://github.com/oldguys/MultipleDataSourceDemo

相關文章