公司不同的业务线往往会进行数据隔离, 即不同的业务数据存在不同的数据库里.多数情况下使用同一个数据库链接配置, 但只是库名不一样. 那就需要我们配置多个数据源, 使用不同的mapper去操作不同的DB. 下面简单说明一下配置流程:
#myBatis config
spring.thymeleaf.cache=false
spring.devtools.restart.enabled=true
spring.devtools.restart.additional-paths=src/main/java
#数据源一
spring.datasource.driver-class-name=ru.yandex.clickhouse.ClickHouseDriver
spring.datasource.url=jdbc:clickhouse://XX.XX.X.X:XXXX/smartprogram
spring.datasource.tomcat.max-wait=6000
mybatis.type-aliases-package=com.xxx.biadmin.domain.model.clickhouse
mybatis.mapperLocations=classpath:mapper
#数据源二
spring.datasource.mofeed.driver-class-name=ru.yandex.clickhouse.ClickHouseDriver
spring.datasource.mofeed.url=jdbc:clickhouse://XX.XX.X.X:XXXX/mofeed
spring.datasource.mofeed.tomcat.max-wait=6000
mybatis.mofeed.type-aliases-package=com.xxx.biadmin.domain.model.clickhouse
mybatis.mofeed.mapperLocations=classpath:mappermofeed
Tips: 首先配置数据源, 上述配置文件中有几种数据源(可以是不同的库:mysql, oracle, clickHouse, mongodb等)就写几个配置, 详见下文DataSourceConfig类. 然后配置数据源与其对应的mapper, 即不同的业务线使用不同的mapper写SQL语句, 执行这个mapper的时候指定使用哪个DataSource.
这样简单的配置以后, 只需要新增几个配置文件类, 项目其他位置的代码(service, serviceImpl, dao, mapper内的SQL等)都不用更改, 在dao层执行查询调用时, mapper会自动知道自己应该用哪个库, 非常方便.
想要调试新增类的功能, 只需要在新增类里某处打个断点, springBoot启动的时候会走过那里, 具体的参数赋值就可以观察到了.
DataSourceConfig类 (配置数据源)
package com.xxx.xxx.configs.mybatis;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import javax.sql.DataSource;
/**
* 配置数据源
*/
@Configuration
public class DataSourceConfig {
@Bean(name = "dbSmartProgram")
@ConfigurationProperties(prefix = "spring.datasource")
@Primary
public DataSource dataSourceSmartProgram() {
return DataSourceBuilder.create().build();
}
@Bean(name = "dbMofeed")
@ConfigurationProperties(prefix = "spring.datasource.mofeed")
public DataSource dataSourceMofeed() {
return DataSourceBuilder.create().build();
}
}
MyBatisConfigSmartProgram类
package com.xxx.xxx.configs.mybatis;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import javax.annotation.Resource;
import javax.sql.DataSource;
/**
* 配置数据源smartprogram对应的mapper
*/
@Configuration
@MapperScan(basePackages = "com.xxx.xxx.xxx.dao.mapper",
sqlSessionFactoryRef = "sqlSessionFactorySmartProgram")
public class MyBatisConfigSmartProgram {
@Resource(name = "dbSmartProgram")
DataSource dbSmartProgram;
@Bean
@Primary
@ConfigurationProperties(prefix = "mybatis")
public SqlSessionFactoryBean sqlSessionFactorySmartProgram() {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dbSmartProgram);
return sqlSessionFactoryBean;
}
}
MyBatisConfigMofeed类
package com.xxx.xxx.configs.mybatis;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
import javax.sql.DataSource;
/**
* 配置数据源mofeed对应的mapper
*/
@Configuration
@MapperScan(basePackages = "com.xxx.xxx.xxx.dao.mappermofeed",
sqlSessionFactoryRef = "sqlSessionFactoryMofeed")
public class MyBatisConfigMofeed {
@Resource(name = "dbMofeed")
DataSource dbMofeed;
@Bean
@ConfigurationProperties(prefix = "mybatis.mofeed")
public SqlSessionFactoryBean sqlSessionFactoryMofeed() {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dbMofeed);
return sqlSessionFactoryBean;
}
}