Java | MyBatis 配置多數(shù)據(jù)源
Mybatis 配置多數(shù)據(jù)源
Table of Contents
前言使用步驟1. 引入庫(kù)2. 配置多數(shù)據(jù)源3. 配置
前言
在開(kāi)發(fā)一些報(bào)表項(xiàng)目時(shí),很容易涉及到從多個(gè)數(shù)據(jù)源獲取數(shù)據(jù),這里介紹一下如何給 Mybatis 在配置多數(shù)據(jù)源.
使用步驟
1. 引入庫(kù)
正常引入 mybatis 依賴即可
2. 配置多數(shù)據(jù)源
spring:
datasource:
report1:
driver-class-name: com.mysql.jdbc.Driver
jdbc-url: jdbc:mysql://127.0.0.1:3306/report_1
username: root
password: 123456
report2:
driver-class-name: com.mysql.jdbc.Driver
jdbc-url: jdbc:mysql://127.0.0.1:3306/report_2
username: root
password: 123456
3. 配置
配置report1
import com.github.pagehelper.PageInterceptor;
import java.util.Properties;
import javax.sql.DataSource;
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.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@MapperScan(basePackages = {
"com.example.demo.mapper.report1"}, sqlSessionFactoryRef = "sqlSessionFactoryReport1")
public class ReportMybatisConfiguration {
@Bean(name = "reportDB")
@ConfigurationProperties(prefix = "spring.datasource.report1")
public DataSource dataSource1() {
return DataSourceBuilder.create().build();
}
@Bean
public SqlSessionFactory sqlSessionFactoryReport1() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource1());
PageInterceptor pageInterceptor = new PageInterceptor();
Properties properties = new Properties();
properties.setProperty("helperDialect", "mysql");
properties.setProperty("reasonable", "true");
properties.setProperty("supportMethodsArguments", "true");
properties.setProperty("params", "count=countSql");
pageInterceptor.setProperties(properties);
factoryBean.setPlugins(pageInterceptor);
return factoryBean.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionTemplateReport1() throws Exception {
return new SqlSessionTemplate(sqlSessionFactoryReport1());
}
}
配置report2
import com.github.pagehelper.PageInterceptor;
import java.util.Properties;
import javax.sql.DataSource;
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.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@MapperScan(basePackages = {
"com.example.demo.mapper.report2"}, sqlSessionFactoryRef = "sqlSessionFactoryReport2")
public class Report2MybatisConfiguration {
@Bean(name = "report2DB")
@ConfigurationProperties(prefix = "spring.datasource.report2")
public DataSource dataSource2() {
return DataSourceBuilder.create().build();
}
@Bean
public SqlSessionFactory sqlSessionFactoryReport2() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource2());
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
configuration.setMapUnderscoreToCamelCase(true);
factoryBean.setConfiguration(configuration);
PageInterceptor pageInterceptor = new PageInterceptor();
Properties properties = new Properties();
properties.setProperty("helperDialect", "mysql");
properties.setProperty("reasonable", "true");
properties.setProperty("supportMethodsArguments", "true");
properties.setProperty("params", "count=countSql");
pageInterceptor.setProperties(properties);
factoryBean.setPlugins(pageInterceptor);
return factoryBean.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionTemplateReport2() throws Exception {
return new SqlSessionTemplate(sqlSessionFactoryReport2());
}
}
這樣我們就配置完了,在配置的過(guò)程中加入 PageInterceptor 分頁(yè)插件,如果不需要可以刪除 PageInterceptor 關(guān)聯(lián)代碼
這樣配置后,在包 com.example.demo.mapper.report1 編寫(xiě)的 mapper 則使用 report1 庫(kù), 同理在 包 com.example.demo.mapper.report1 編寫(xiě)的 mapper 則使用 report2 庫(kù)。
這樣就完成了多數(shù)據(jù)源的配置,同理也可以配置更多的數(shù)據(jù)源

最近的時(shí)間不太多,接下來(lái)將寫(xiě)幾篇關(guān)于日常工作中用到的一些技術(shù)以及如何與 Spring Boot 整合的文章,計(jì)劃如下:
數(shù)據(jù)庫(kù)表結(jié)構(gòu)管理 flayway
MyBatis 的整合使用
Spring Data REST 快速構(gòu)建一個(gè) restful 項(xiàng)目
使用 Spring Integration 來(lái)配置分布式鎖
Sentinel 的單機(jī)使用和 Sentinel Dashboard 的搭建
使用 maven git commit 插件來(lái)生成打包版本號(hào),方便線程排查
關(guān)于 Spring Boot 和 Spring Cloud 匹配版本的查看方法
如果快速將一個(gè) Spring Boot 項(xiàng)目,改成Spring Cloud 項(xiàng)目
使用 Spring Boot Admin 來(lái)監(jiān)控我們的 Spring Cloud 項(xiàng)目
風(fēng)控規(guī)則集的開(kāi)發(fā)實(shí)踐
