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

Java mysql詳細(xì)講解雙數(shù)據(jù)源配置使用

 更新時間:2022年06月30日 08:59:46   作者:念舊、sunshine  
在開發(fā)過程中我們常常會用到兩個數(shù)據(jù)庫,一個數(shù)據(jù)用來實現(xiàn)一些常規(guī)的增刪改查,另外一個數(shù)據(jù)庫用來實時存數(shù)據(jù)。進(jìn)行數(shù)據(jù)的統(tǒng)計分析??梢宰x寫分離??梢愿玫膬?yōu)化和提高效率;或者兩個數(shù)據(jù)存在業(yè)務(wù)分離的時候也需要多個數(shù)據(jù)源來實現(xiàn)

使用方式

application.properties中數(shù)據(jù)庫配置

#數(shù)據(jù)庫配置
spring.datasource.db1.jdbc-url=jdbc:mysql://localhost:3306/gds?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false 
spring.datasource.db1.username=root
spring.datasource.db1.password=root
spring.datasource.db1.driver-class-name=com.mysql.cj.jdbc.Driver

spring.datasource.db2.jdbc-url=jdbc:mysql://localhost:3306/zkhx?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false
spring.datasource.db2.username=root
spring.datasource.db2.password=root
spring.datasource.db2.driver-class-name=com.mysql.cj.jdbc.Driver

config文件配置

1、配置 spring.datasource.db1

注:basePackages=“com.zkhx.dao.master”:prefix= “spring.datasource.db1”

綁定master目錄下使用的是數(shù)據(jù)庫db1 也就是gds

package com.zkhx.config;
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.beans.factory.annotation.Qualifier;
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 org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
@Configuration
// 配置mybatis的接口類放的地方
@MapperScan(basePackages = "com.zkhx.dao.master", sqlSessionFactoryRef = "dbSqlSessionFactory")
public class DataSourceConfig {
    @Bean(name = "db")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.db1")
    public DataSource getDateSource1() {
        return DataSourceBuilder.create().build();
    }
    @Bean
    @ConfigurationProperties(prefix = "mybatis.configuration")
    public org.apache.ibatis.session.Configuration configuration() {
        return new org.apache.ibatis.session.Configuration();
    }
    @Bean(name = "dbSqlSessionFactory")
    @Primary
    public SqlSessionFactory test1SqlSessionFactory(@Qualifier("db") DataSource datasource, org.apache.ibatis.session.Configuration configuration)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        bean.setConfiguration(configuration);
        bean.setMapperLocations(
                // 設(shè)置mybatis的xml所在位置
                new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/master/*.xml"));
        return bean.getObject();
    }
    @Bean("dbSqlSessionTemplate")
    // 表示這個數(shù)據(jù)源是默認(rèn)數(shù)據(jù)源
    @Primary
    public SqlSessionTemplate test1sqlsessiontemplate(
            @Qualifier("dbSqlSessionFactory") SqlSessionFactory sessionfactory) {
        return new SqlSessionTemplate(sessionfactory);
    }
}

2、配置 spring.datasource.db2

package com.zkhx.config;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "com.zkhx.dao.vice", sqlSessionFactoryRef = "db2SqlSessionFactory")
public class DataSourceViceConfig {
    @Bean(name = "db2")
    @ConfigurationProperties(prefix = "spring.datasource.db2")
    public DataSource getDateSource2() {
        return DataSourceBuilder.create().build();
    }
    @Bean(name = "db2SqlSessionFactory")
    public SqlSessionFactory test2SqlSessionFactory(@Qualifier("db2") DataSource datasource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        bean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/vice/*.xml"));
        return bean.getObject();
    }
    @Bean("db2SqlSessionTemplate")
    public SqlSessionTemplate test2sqlsessiontemplate(
            @Qualifier("db2SqlSessionFactory") SqlSessionFactory sessionfactory) {
        return new SqlSessionTemplate(sessionfactory);
    }
}

3、截圖

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zkhx.dao.vice.StDBDataDao">
</mapper>

到此這篇關(guān)于Java mysql詳細(xì)講解雙數(shù)據(jù)源配置使用的文章就介紹到這了,更多相關(guān)Java 雙數(shù)據(jù)源內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 使用Swagger實現(xiàn)接口版本號管理方式

    使用Swagger實現(xiàn)接口版本號管理方式

    這篇文章主要介紹了使用Swagger實現(xiàn)接口版本號管理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • SecurityUtils.getSubject().getPrincipal()為null的問題

    SecurityUtils.getSubject().getPrincipal()為null的問題

    這篇文章主要介紹了SecurityUtils.getSubject().getPrincipal()為null的問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • mybatis-plus多表查詢操作方法

    mybatis-plus多表查詢操作方法

    這篇文章主要介紹了mybatis-plus多表查詢操作方法,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2023-12-12
  • Spring實戰(zhàn)之使用Expression接口進(jìn)行表達(dá)式求值操作示例

    Spring實戰(zhàn)之使用Expression接口進(jìn)行表達(dá)式求值操作示例

    這篇文章主要介紹了Spring實戰(zhàn)之使用Expression接口進(jìn)行表達(dá)式求值操作,結(jié)合實例形式分析了Spring操作Expression接口實現(xiàn)表達(dá)式運算的操作技巧與相關(guān)注意事項,需要的朋友可以參考下
    2019-12-12
  • Java讀取Map的兩種方法與對比

    Java讀取Map的兩種方法與對比

    相信大家都知道在Java中Map的使用非常頻繁,我們經(jīng)常會需要對Map進(jìn)行遍歷和讀取,那么下面這篇文章將展示兩種遍歷的方法以及簡要分析。有需要的可以參考借鑒,下面來一起看看吧。
    2016-11-11
  • JPA多數(shù)據(jù)源分布式事務(wù)處理方案

    JPA多數(shù)據(jù)源分布式事務(wù)處理方案

    這篇文章主要為大家介紹了JPA多數(shù)據(jù)源分布式事務(wù)處理的兩種事務(wù)方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-02-02
  • Java匿名內(nèi)部類原理與用法詳解

    Java匿名內(nèi)部類原理與用法詳解

    這篇文章主要介紹了Java匿名內(nèi)部類原理與用法,結(jié)合實例形式分析了Java匿名內(nèi)部類的概念、原理、應(yīng)用與相關(guān)操作注意事項,需要的朋友可以參考下
    2019-09-09
  • SpringBoot開發(fā)案例之配置Druid數(shù)據(jù)庫連接池的示例

    SpringBoot開發(fā)案例之配置Druid數(shù)據(jù)庫連接池的示例

    本篇文章主要介紹了SpringBoot開發(fā)案例之配置Druid數(shù)據(jù)庫連接池的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-03-03
  • Spring Boot 搭建 ELK正確看日志的配置流程

    Spring Boot 搭建 ELK正確看日志的配置流程

    這篇文章主要介紹了Spring Boot 搭建 ELK正確看日志的配置流程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-03-03
  • Java利用釘釘機(jī)器人實現(xiàn)發(fā)送群消息

    Java利用釘釘機(jī)器人實現(xiàn)發(fā)送群消息

    這篇文章主要為大家詳細(xì)介紹了Java語言如何通過釘釘機(jī)器人發(fā)送群消息通知,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2022-09-09

最新評論

双城市| 慈溪市| 青海省| 惠来县| 阜南县| 城口县| 常州市| 玉树县| 新巴尔虎右旗| 康保县| 华安县| 夹江县| 平谷区| 土默特左旗| 涿鹿县| 西宁市| 宜州市| 萨迦县| 理塘县| 梁河县| 邹平县| 新建县| 平谷区| 团风县| 三门县| 台北市| 弥渡县| 高邑县| 资兴市| 玛多县| 宿松县| 衡阳县| 彭阳县| 永州市| 曲麻莱县| 纳雍县| 南丰县| 科尔| 大冶市| 和顺县| 扶余县|