SpringBoot Mybatis多數(shù)據(jù)源配置全過(guò)程
一. 配置文件
spring:
datasource:
# 數(shù)據(jù)源1
primary:
jdbc-url: jdbc:mysql://localhost/myblog?useUnicode=true&characterEncoding=utf-8
username: root
password: mysql
driver-class-name: com.mysql.cj.jdbc.Driver
# 數(shù)據(jù)源2
secondary:
jdbc-url: jdbc:mysql://localhost/pythonblog?useUnicode=true&characterEncoding=utf-8
username: root
password: mysql
driver-class-name: com.mysql.cj.jdbc.Driver
同時(shí)連接了Mysql中的兩個(gè)數(shù)據(jù)庫(kù),myblog和pythonblog

二. 多數(shù)據(jù)源配置類
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;
@Configuration
public class DataSourceConfiguration {
// Primary注解是在沒(méi)有指明使用哪個(gè)數(shù)據(jù)源的時(shí)候指定默認(rèn)使用的主數(shù)據(jù)源
@Primary
@Bean("primaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.primary")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean("secondaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.secondary")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
}
三. 多數(shù)據(jù)源Mybatis配置
注意事項(xiàng):
3.1 primary數(shù)據(jù)源配置和3.2 secondary數(shù)據(jù)源配置是兩種不同的配置方式,都能實(shí)現(xiàn)多數(shù)據(jù)源的效果.此處使用兩種不同的方式進(jìn)行配置,只是為了展示不同的配置方式.sqlSessionFactoryBean.setMapperLocations適用于Mybatis的Mapper接口文件和 .xml文件不在同一個(gè)目錄下的情況,用于指定 .xml文件 所在的文件路徑.@Primary注解用于指定默認(rèn)的數(shù)據(jù)源.
3.1 primary數(shù)據(jù)源配置
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import javax.annotation.Resource;
import javax.sql.DataSource;
@Configuration
@MapperScan(
// 指定該數(shù)據(jù)源掃描指定包下面的Mapper接口文件
basePackages = "com.example.jmw.mapper",
sqlSessionFactoryRef = "sqlSessionFactoryPrimary",
sqlSessionTemplateRef = "sqlSessionTemplatePrimary")
publc class DataSourcePrimaryConfig {
// 注入數(shù)據(jù)源1
@Resource
private DataSource primaryDataSource;
@Bean
@Primary
public SqlSessionFactory sqlSessionFactoryPrimary() throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(primaryDataSource);
return sqlSessionFactoryBean.getObject();
}
@Bean
@Primary
public SqlSessionTemplate sqlSessionTemplatePrimary() throws Exception {
return new SqlSessionTemplate(sqlSessionFactoryPrimary());
}
}

3.2 secondary數(shù)據(jù)源配置
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@Configuration
@MapperScan(
basePackages = "com.example.jmw.mapper1",
sqlSessionFactoryRef = "sqlSessionFactorySecondary")
public class DataSourceSecondaryConfig {
// mapper掃描xml文件的路徑
private static final String MAPPER_LOCATION = "classpath:mapper1/*.xml";
private DataSource secondaryDataSource;
// 通過(guò)構(gòu)造方法進(jìn)行注入
public DataSourceSecondaryConfig(@Qualifier("secondaryDataSource") DataSource secondaryDataSource) {
this.secondaryDataSource = secondaryDataSource;
}
@Bean
public SqlSessionFactory sqlSessionFactorySecondary() throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
// 指定數(shù)據(jù)源
sqlSessionFactoryBean.setDataSource(secondaryDataSource);
/*
獲取xml文件資源對(duì)象
當(dāng)Mapper接口所對(duì)應(yīng)的.xml文件與Mapper接口文件分離,存儲(chǔ)在 resources
文件夾下的時(shí)候,需要手動(dòng)指定.xml文件所在的路徑
*/
Resource[] resources = new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION);
sqlSessionFactoryBean.setMapperLocations(resources);
return sqlSessionFactoryBean.getObject();
}
@Bean
public DataSourceTransactionManager SecondaryDataSourceManager() {
return new DataSourceTransactionManager(secondaryDataSource);
}
}

四. 效果
import com.example.jmw.mapper.I18nMessageMapper;
import com.example.jmw.mapper1.BlogTagMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.List;
@Controller
@RequestMapping("/test12")
public class Test12Controller {
// 數(shù)據(jù)源1Mapper注入
@Resource
private I18nMessageMapper i18nMessageMapper;
// 數(shù)據(jù)源2Mapper注入
@Resource
private BlogTagMapper blogTagMapper;
@GetMapping("/selectManyDataSourceData")
@ResponseBody
public void selectManyDataSourceData() {
// 查詢數(shù)據(jù)源1中的數(shù)據(jù)
List<I18MessageEnttiy> allLocaleMessage = i18nMessageMapper.getAllLocaleMessage();
System.out.println(allLocaleMessage);
// 查詢數(shù)據(jù)源2中的數(shù)據(jù)
List<BlogTag> allBlogTag = blogTagMapper.getAllBlogTag();
System.out.println(allBlogTag);
}
}

總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- SpringBoot+mybatis-plus實(shí)現(xiàn)多數(shù)據(jù)源配置的詳細(xì)步驟
- SpringBoot+MybatisPlus+jdbc連接池配置多數(shù)據(jù)源的實(shí)現(xiàn)
- springboot druid mybatis多數(shù)據(jù)源配置方式
- SpringBoot整合Mybatis-Plus+Druid實(shí)現(xiàn)多數(shù)據(jù)源配置功能
- SpringBoot整合Mybatis實(shí)現(xiàn)多數(shù)據(jù)源配置與跨數(shù)據(jù)源事務(wù)實(shí)例
- Springboot集成mybatis實(shí)現(xiàn)多數(shù)據(jù)源配置詳解流程
- springboot mybatis druid配置多數(shù)據(jù)源教程
相關(guān)文章
SpringBoot+Tess4j實(shí)現(xiàn)牛的OCR識(shí)別工具的示例代碼
這篇文章主要介紹了SpringBoot+Tess4j實(shí)現(xiàn)牛的OCR識(shí)別工具的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
java實(shí)現(xiàn)波雷費(fèi)密碼算法示例代碼
這篇文章主要介紹了java實(shí)現(xiàn)波雷費(fèi)密碼算法示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
使用graalvm為帶有反射功能的java代碼生成native?image的示例詳解
graalvm讓native鏡像支持反射的關(guān)鍵是利用json提前告訴它哪些類的哪些方法會(huì)被反射調(diào)用,然后它就能力在運(yùn)行時(shí)支持反射了,這篇文章主要介紹了如何使用graalvm為帶有反射功能的java代碼生成native?image,需要的朋友可以參考下2024-02-02
Java并發(fā)系列之CyclicBarrier源碼分析
這篇文章主要為大家詳細(xì)分析了Java并發(fā)系列之CyclicBarrier源碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
Java使用Tesseract-Ocr識(shí)別數(shù)字
這篇文章主要介紹了Java使用Tesseract-Ocr識(shí)別數(shù)字的方法,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下2021-04-04
java復(fù)制文件的4種方式及拷貝文件到另一個(gè)目錄下的實(shí)例代碼
這篇文章主要介紹了java復(fù)制文件的4種方式,通過(guò)實(shí)例帶給大家介紹了java 拷貝文件到另一個(gè)目錄下的方法,需要的朋友可以參考下2018-06-06
springBoot整合shiro如何解決讀取不到@value值問(wèn)題
這篇文章主要介紹了springBoot整合shiro如何解決讀取不到@value值問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,2023-08-08

