SpringBoot Mybatis多數(shù)據(jù)源配置全過程
更新時(shí)間:2025年11月12日 08:50:43 作者:fengyehongWorld
本文詳細(xì)介紹了如何在Java項(xiàng)目中配置多數(shù)據(jù)源,包括配置文件設(shè)置、多數(shù)據(jù)源配置類、MyBatis配置等,通過兩種不同的方式配置primary和secondary數(shù)據(jù)源,展示了如何處理Mapper接口文件和.xml文件不在同一目錄下的情況,并使用@Primary注解指定默認(rèn)數(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
同時(shí)連接了Mysql中的兩個(gè)數(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注解是在沒有指明使用哪個(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;
// 通過構(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項(xiàng)目創(chuàng)建使用+配置文件+日志文件詳解
Spring的出現(xiàn)是為了簡化 Java 程序開發(fā),而 SpringBoot 的出現(xiàn)是為了簡化 Spring 程序開發(fā),這篇文章主要介紹了SpringBoot項(xiàng)目創(chuàng)建使用+配置文件+日志文件,需要的朋友可以參考下2023-02-02
Java負(fù)載均衡算法實(shí)現(xiàn)之輪詢和加權(quán)輪詢
網(wǎng)上找了不少負(fù)載均衡算法的資源,都不夠全面,后來自己結(jié)合了網(wǎng)上的一些算法實(shí)現(xiàn),下面這篇文章主要給大家介紹了關(guān)于Java負(fù)載均衡算法實(shí)現(xiàn)之輪詢和加權(quán)輪詢的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04
springmvc中RequestMappingHandlerAdapter與HttpMessageConverter的
今天小編就為大家分享一篇關(guān)于springmvc中RequestMappingHandlerAdapter與HttpMessageConverter的裝配講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-01-01

