最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot Mybatis多數(shù)據(jù)源配置全過(guò)程

 更新時(shí)間:2025年11月12日 08:50:43   作者:fengyehongWorld  
本文詳細(xì)介紹了如何在Java項(xiàng)目中配置多數(shù)據(jù)源,包括配置文件設(shè)置、多數(shù)據(jù)源配置類、MyBatis配置等,通過(guò)兩種不同的方式配置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ù)庫(kù),myblogpythonblog

二. 多數(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è)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Intellij?IDEA如何查看所有斷點(diǎn)

    Intellij?IDEA如何查看所有斷點(diǎn)

    這篇文章主要介紹了Intellij?IDEA如何查看所有斷點(diǎn)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • SpringBoot+Tess4j實(shí)現(xiàn)牛的OCR識(shí)別工具的示例代碼

    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 substring原理及使用方法實(shí)例

    Java substring原理及使用方法實(shí)例

    這篇文章主要介紹了Java substring原理及使用方法實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • java實(shí)現(xiàn)波雷費(fèi)密碼算法示例代碼

    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為帶有反射功能的java代碼生成native?image的示例詳解

    graalvm讓native鏡像支持反射的關(guān)鍵是利用json提前告訴它哪些類的哪些方法會(huì)被反射調(diào)用,然后它就能力在運(yùn)行時(shí)支持反射了,這篇文章主要介紹了如何使用graalvm為帶有反射功能的java代碼生成native?image,需要的朋友可以參考下
    2024-02-02
  • Java并發(fā)系列之CyclicBarrier源碼分析

    Java并發(fā)系列之CyclicBarrier源碼分析

    這篇文章主要為大家詳細(xì)分析了Java并發(fā)系列之CyclicBarrier源碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • Java使用Tesseract-Ocr識(shí)別數(shù)字

    Java使用Tesseract-Ocr識(shí)別數(shù)字

    這篇文章主要介紹了Java使用Tesseract-Ocr識(shí)別數(shù)字的方法,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下
    2021-04-04
  • SpringBoot底層注解詳解

    SpringBoot底層注解詳解

    這篇文章主要介紹了SpringBoot底層注解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2023-05-05
  • java復(fù)制文件的4種方式及拷貝文件到另一個(gè)目錄下的實(shí)例代碼

    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)題

    這篇文章主要介紹了springBoot整合shiro如何解決讀取不到@value值問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,
    2023-08-08

最新評(píng)論

阳泉市| 绍兴县| 当雄县| 商洛市| 隆回县| 礼泉县| 武邑县| 荆门市| 台湾省| 七台河市| 府谷县| 宝鸡市| 延吉市| 庆城县| 防城港市| 道真| 高密市| 论坛| 博客| 益阳市| 蓝田县| 婺源县| 额敏县| 伊吾县| 临海市| 镇远县| 广宗县| 邳州市| 双桥区| 正宁县| 廉江市| 连州市| 鹤山市| 寻乌县| 临汾市| 星座| 保亭| 济阳县| 治县。| 黄浦区| 广宁县|