Java mysql詳細講解雙數據源配置使用
使用方式
application.properties中數據庫配置
#數據庫配置
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目錄下使用的是數據庫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(
// 設置mybatis的xml所在位置
new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/master/*.xml"));
return bean.getObject();
}
@Bean("dbSqlSessionTemplate")
// 表示這個數據源是默認數據源
@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>
到此這篇關于Java mysql詳細講解雙數據源配置使用的文章就介紹到這了,更多相關Java 雙數據源內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Linux系統(tǒng)下搭建Java開發(fā)環(huán)境
本文主要是記錄了如何在Linux環(huán)境下一步步安裝JAVA JDK環(huán)境,非常簡單實用,有需要的朋友可以參考下2014-10-10
解決maven build 無反應,直接terminated的問題
下面小編就為大家?guī)硪黄鉀Qmaven build 無反應,直接terminated的問題。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06
SpringBoot集成FastDFS依賴實現文件上傳的示例
這篇文章主要介紹了SpringBoot集成FastDFS依賴實現文件上傳,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-05-05
SpringSecurity request過濾問題示例小結
這篇文章主要介紹了SpringSecurity request過濾問題示例小結,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-02-02

