SpringBoot 配置多資料來源 MyBatis
在 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
相關文章
- springboot+druid+mybatis plus的多資料來源配置Spring BootUIMyBatis
- Springboot 配置多資料來源Mybatis的UnderScore不生效Spring BootMyBatis
- Springboot 多資料來源配置,結合tk-mybatisSpring BootMyBatis
- MyBatis配置多資料來源MyBatis
- springBoot 多資料來源配置Spring Boot
- springboot多資料來源配置Spring Boot
- SpringBoot配置多資料來源Spring Boot
- SpringBoot 整合多資料來源(MyBatis + Druid)Spring BootMyBatisUI
- SpringBoot 的多資料來源配置Spring Boot
- 基於SpirngBoot2.0+ 的 SpringBoot+Mybatis 多資料來源配置Spring BootMyBatis
- 全程解析,MyBatis在SpringBoot中的動態多資料來源配置MyBatisSpring Boot
- SpringBoot多資料來源Spring Boot
- springboot新增多資料來源連線池並配置MybatisSpring BootMyBatis
- SpringBoot整合MyBatis-Plus實現多資料來源操作Spring BootMyBatis
- 【教程】Spring+Mybatis環境配置多資料來源SpringMyBatis
- SpringBoot | 3.1 配置資料來源Spring Boot
- springboot 配置多個資料來源,@MapperScan方式繫結Spring BootAPP
- 多資料來源配置
- Springboot整合mybatis實現多資料來源所遇到的問題Spring BootMyBatis
- SpringBoot資料來源相關配置Spring Boot
- SpirngBoot整合Mybatis Plus多資料來源bootMyBatis
- Spring多資料來源配置Spring
- web 配置多資料來源Web
- SpringBoot+Mybatis+ Druid+PageHelper 實現多資料來源並分頁Spring BootMyBatisUI
- springboot 2 Hikari 多資料來源配置問題(dataSourceClassName or jdbcUrl is required)Spring BootJDBCUI
- springboot(七):springboot+mybatis多資料Spring BootMyBatis
- Spring Boot + Mybatis 多資料來源配置實現讀寫分離Spring BootMyBatis
- springboot 配置DRUID資料來源的方法Spring BootUI
- 資料來源(DataSource)是什麼以及SpringBoot中資料來源配置Spring Boot
- mybatis 多資料來源動態切換MyBatis
- Spring Boot 多資料來源配置Spring Boot
- Spring Boot 配置多資料來源Spring Boot
- SpringBoot 2.x quartz多資料來源定時任務配置Spring Bootquartz
- 談一談Spring-Mybatis在多資料來源配置上的坑SpringMyBatis
- 談一談 Spring-Mybatis 在多資料來源配置上的坑SpringMyBatis
- Spring Boot 2.x基礎教程:MyBatis的多資料來源配置Spring BootMyBatis
- SpringBoot整合MyBatisPlus配置動態資料來源Spring BootMyBatis
- Spring Boot 原始碼分析 資料來源 + Mybatis 配置Spring Boot原始碼MyBatis