Java mysql詳細(xì)講解雙數(shù)據(jù)源配置使用
使用方式
application.properties中數(shù)據(jù)庫(kù)配置
#數(shù)據(jù)庫(kù)配置
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.Driverspring.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ù)庫(kù)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的接口類(lèi)放的地方 @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") // 表示這個(gè)數(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java Mybatis數(shù)據(jù)源之工廠(chǎng)模式
- Java Spring詳解如何配置數(shù)據(jù)源注解開(kāi)發(fā)以及整合Junit
- 一小時(shí)迅速入門(mén)Mybatis之bind與多數(shù)據(jù)源支持 Java API
- 如何在Java SpringBoot項(xiàng)目中配置動(dòng)態(tài)數(shù)據(jù)源你知道嗎
- Java使用C3P0數(shù)據(jù)源鏈接數(shù)據(jù)庫(kù)
- Java自動(dòng)化測(cè)試中多數(shù)據(jù)源的切換(實(shí)例講解)
- Java注解實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)源切換的實(shí)例代碼
- Spring MVC配置雙數(shù)據(jù)源實(shí)現(xiàn)一個(gè)java項(xiàng)目同時(shí)連接兩個(gè)數(shù)據(jù)庫(kù)的方法
- java 與testng利用XML做數(shù)據(jù)源的數(shù)據(jù)驅(qū)動(dòng)示例詳解
相關(guān)文章
Linux系統(tǒng)下搭建Java開(kāi)發(fā)環(huán)境
本文主要是記錄了如何在Linux環(huán)境下一步步安裝JAVA JDK環(huán)境,非常簡(jiǎn)單實(shí)用,有需要的朋友可以參考下2014-10-10eclipse自動(dòng)提示和自動(dòng)補(bǔ)全功能實(shí)現(xiàn)方法
這篇文章主要介紹了eclipse自動(dòng)提示和自動(dòng)補(bǔ)全的相關(guān)內(nèi)容,文中向大家分享了二者的實(shí)現(xiàn)方法代碼,需要的朋友可以了解下。2017-09-09解決maven build 無(wú)反應(yīng),直接terminated的問(wèn)題
下面小編就為大家?guī)?lái)一篇解決maven build 無(wú)反應(yīng),直接terminated的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06SpringBoot集成FastDFS依賴(lài)實(shí)現(xiàn)文件上傳的示例
這篇文章主要介紹了SpringBoot集成FastDFS依賴(lài)實(shí)現(xiàn)文件上傳,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05SpringSecurity request過(guò)濾問(wèn)題示例小結(jié)
這篇文章主要介紹了SpringSecurity request過(guò)濾問(wèn)題示例小結(jié),本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-02-02java結(jié)合email實(shí)現(xiàn)自動(dòng)推送功能
這篇文章主要介紹了java結(jié)合email實(shí)現(xiàn)自動(dòng)推送功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03Java基礎(chǔ)教程之理解Annotation詳細(xì)介紹
這篇文章主要介紹了Java基礎(chǔ)教程之理解Annotation詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2017-01-01Mybatis獲取參數(shù)值和查詢(xún)功能的案例詳解
這篇文章主要介紹了Mybatis獲取參數(shù)值和查詢(xún)功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03