SpringBoot Mybatis多數(shù)據(jù)源配置全過程
更新時間:2025年11月12日 08:50:43 作者:fengyehongWorld
本文詳細介紹了如何在Java項目中配置多數(shù)據(jù)源,包括配置文件設置、多數(shù)據(jù)源配置類、MyBatis配置等,通過兩種不同的方式配置primary和secondary數(shù)據(jù)源,展示了如何處理Mapper接口文件和.xml文件不在同一目錄下的情況,并使用@Primary注解指定默認數(shù)據(jù)源
一. 配置文件
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
同時連接了Mysql中的兩個數(shù)據(jù)庫,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注解是在沒有指明使用哪個數(shù)據(jù)源的時候指定默認使用的主數(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配置
注意事項:
3.1 primary數(shù)據(jù)源配置和3.2 secondary數(shù)據(jù)源配置是兩種不同的配置方式,都能實現(xiàn)多數(shù)據(jù)源的效果.此處使用兩種不同的方式進行配置,只是為了展示不同的配置方式.sqlSessionFactoryBean.setMapperLocations適用于Mybatis的Mapper接口文件和 .xml文件不在同一個目錄下的情況,用于指定 .xml文件 所在的文件路徑.@Primary注解用于指定默認的數(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;
// 通過構造方法進行注入
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文件資源對象
當Mapper接口所對應的.xml文件與Mapper接口文件分離,存儲在 resources
文件夾下的時候,需要手動指定.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);
}
}

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

