欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot整合mysql、postgres及sqlserver實現(xiàn)多數(shù)據(jù)源配置實戰(zhàn)案例

 更新時間:2023年12月01日 10:38:31   作者:袁久  
在工作中業(yè)務(wù)的發(fā)展或業(yè)務(wù)數(shù)據(jù)隔離的場景下,通常需要一個項目中引入多個數(shù)據(jù)源,但SpringBoot默認(rèn)的自動化配置是單數(shù)據(jù)源的,這篇文章主要給大家介紹了關(guān)于SpringBoot整合mysql、postgres及sqlserver實現(xiàn)多數(shù)據(jù)源配置的相關(guān)資料,需要的朋友可以參考下

分享一下近期處理的一個小demo,關(guān)于配置多數(shù)據(jù)源實現(xiàn)不同服務(wù)之間的數(shù)據(jù)推送和數(shù)據(jù)治理。第一次接觸到pg庫和sqlserver一頭霧水,選擇了JDBC+mybatis-plus的方式去鏈接。

1、首先要引入以下依賴

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.18.12</version>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<version>4.5.10</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.62</version>
		</dependency>
		<!--mysql 驅(qū)動-->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>8.0.22</version>
		</dependency>
		<!--postgresql 驅(qū)動-->
		<dependency>
			<groupId>org.postgresql</groupId>
			<artifactId>postgresql</artifactId>
			<scope>runtime</scope>
		</dependency>
		<!--sqlserver 配置-->
		<dependency>
			<groupId>com.microsoft.sqlserver</groupId>
			<artifactId>mssql-jdbc</artifactId>
			<version>9.4.0.jre8</version>
		</dependency>
		<!--//spring默認(rèn)的jdbc連接-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.1.13</version>
		</dependency>
		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-boot-starter</artifactId>
			<version>3.4.0</version>
		</dependency>
		<dependency>
			<groupId>cn.afterturn</groupId>
			<artifactId>easypoi-base</artifactId>
			<version>4.4.0</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<version>2.7.12</version>
				<configuration>
					<!-- 指定該Main Class為全局的唯一入口 -->
					<mainClass>com.zkgl.ZsjDemoApplication</mainClass>
				</configuration>
				<executions>
					<execution>
						<goals>
							<!--可以把依賴的包都打包到生成的Jar包中 -->
							<goal>repackage</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>

2、demo的項目結(jié)構(gòu)如下

3、yml配置文件

server:
  port:3666
spring:
  application:
    name: multiple-data
  datasource:
    show-sql: false
    db1:
      jdbc-url: jdbc:mysql://ip地址:端口/數(shù)據(jù)庫名?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
      username: 數(shù)據(jù)庫賬號
      password: 數(shù)據(jù)庫密碼
      driver-class-name: com.mysql.cj.jdbc.Driver
    db2:
      jdbc-url: jdbc:postgresql://ip地址:端口/數(shù)據(jù)庫名?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
      username: 數(shù)據(jù)庫賬號
      password: 數(shù)據(jù)庫密碼
      driver-class-name: org.postgresql.Driver
    db3:
      jdbc-url: jdbc:sqlserver://ip地址:端口;databaseName=數(shù)據(jù)庫名
      username: 數(shù)據(jù)庫賬號
      password: 數(shù)據(jù)庫密碼
      driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver

4、配置類

package com.zkgl.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.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
/**
 * @Author: 袁振豪
 * @Description:
 * @Date: Created in  2023-07-23 11:04 PM
 * @Modified By:
 */
@Configuration
@MapperScan(basePackages = "com.zkgl.dao.db1",sqlSessionFactoryRef = "db1SqlSessionFactory")
public class MysqlDataSourceConfig {
    static final String MAPPER_LOCATION = "classpath:/mapper/db1/*.xml";
    @Bean("db1DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.db1")
    public DataSource getDb1DataSource(){
        return DataSourceBuilder.create().build();
    }
    @Bean("db1SqlSessionFactory")
    public SqlSessionFactory db1SqlSessionFactory(@Qualifier("db1DataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION));
        return bean.getObject();
    }
    @Bean("db1SqlSessionTemplate")
    public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("db1SqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
package com.zkgl.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.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
/**
 * @Author: 袁振豪
 * @Description:
 * @Date: Created in  2023-07-23 11:04 PM
 * @Modified By:
 */
@Configuration
@MapperScan(basePackages = "com.zkgl.dao.db2",sqlSessionFactoryRef = "db2SqlSessionFactory")
public class PgDataSourceConfig {

    static final String MAPPER_LOCATION = "classpath:/mapper/db2/*.xml";
    @Bean("db2DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.db2")
    public DataSource getDb2DataSource(){
        return DataSourceBuilder.create().build();
    }
    @Bean("db2SqlSessionFactory")
    public SqlSessionFactory db2SqlSessionFactory(@Qualifier("db2DataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION));
        return bean.getObject();
    }
    @Bean("db2SqlSessionTemplate")
    public SqlSessionTemplate db2SqlSessionTemplate(@Qualifier("db2SqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
package com.zkgl.config;

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;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: 袁振豪
 * @Description:
 * @Date: Created in  2023-07-23 11:04 PM
 * @Modified By:
 */
@Configuration
@MapperScan(basePackages = "com.zkgl.dao.db3",sqlSessionFactoryRef = "db3SqlSessionFactory")
public class SqlServerDataSourceConfig {
    static final String MAPPER_LOCATION = "classpath:/mapper/db3/*.xml";
    @Bean("db3DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.db3")
    public DataSource getDb1DataSource(){
        return DataSourceBuilder.create().build();
    }
    @Bean("db3SqlSessionFactory")
    public SqlSessionFactory db1SqlSessionFactory(@Qualifier("db3DataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION));
        return bean.getObject();
    }
    @Bean("db3SqlSessionTemplate")
    public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("db3SqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

5、controller、dao、service以及對應(yīng)的.xml文件可以根據(jù)自身業(yè)務(wù)情況進行開發(fā),再次不做過多贅述。

總結(jié):要進行多數(shù)據(jù)源的配置,以此案例為例,最重要的是對各個庫的適配,也就是各自的驅(qū)動。眾所周知,要使用JDBC連接數(shù)據(jù)庫,主要有以下步驟:

  • 注冊驅(qū)動
  • 建立數(shù)據(jù)庫連接
  • 創(chuàng)建數(shù)據(jù)庫操作對象
  • 執(zhí)行SQL語句
  • 處理查詢結(jié)果集
  • 關(guān)閉資源 

而在本案例中,pom中引入了相關(guān)依賴,在yml配置了驅(qū)動,之后在config中以Bean的形式分別命名和初始化相關(guān)配置,這樣在Springboot項目中,通過@SpringBootApplication注解中的@EnableAutoConfigtion注解就可以掃描到這些配置好的Bean,從而正常使用了。

總結(jié)

到此這篇關(guān)于SpringBoot整合mysql、postgres及sqlserver實現(xiàn)多數(shù)據(jù)源配置的文章就介紹到這了,更多相關(guān)SpringBoot多數(shù)據(jù)源配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Logback動態(tài)修改日志級別的方法

    Logback動態(tài)修改日志級別的方法

    這篇文章主要介紹了Logback動態(tài)修改日志級別的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11
  • eclipse報錯 eclipse啟動報錯解決方法

    eclipse報錯 eclipse啟動報錯解決方法

    本文將介紹eclipse啟動報錯解決方法,需要了解的朋友可以參考下
    2012-11-11
  • Java+MySQL實現(xiàn)設(shè)計優(yōu)惠券系統(tǒng)

    Java+MySQL實現(xiàn)設(shè)計優(yōu)惠券系統(tǒng)

    這篇文章主要介紹了Java+MySQL實現(xiàn)設(shè)計優(yōu)惠券系統(tǒng),文章基于Java與MySQL的相關(guān)資料展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-05-05
  • 運用Spring?Aop+注解實現(xiàn)日志記錄

    運用Spring?Aop+注解實現(xiàn)日志記錄

    我們都知道Spring框架的兩大特性分別是 IOC (控制反轉(zhuǎn))和 AOP (面向切面),這個是每一個Spring學(xué)習(xí)視頻里面一開始都會提到的,這里,如果我們使用Aop來記錄日志,那是再好不過了,感興趣的朋友跟隨小編一起學(xué)習(xí)下Spring?Aop注解實現(xiàn)日志記錄的過程吧
    2022-01-01
  • Java簡易學(xué)生成績系統(tǒng)寫法實例

    Java簡易學(xué)生成績系統(tǒng)寫法實例

    在本篇文章里小編給大家分享的是關(guān)于Java簡易學(xué)生成績系統(tǒng)寫法實例以及相關(guān)知識點,有需要的朋友們可以學(xué)習(xí)下。
    2019-09-09
  • java腳本使用不同版本jdk的說明介紹

    java腳本使用不同版本jdk的說明介紹

    本文介紹了在Java中執(zhí)行JavaScript腳本的幾種方式,包括使用ScriptEngine、Nashorn和GraalVM,ScriptEngine適用于JDK8及以下版本,而Nashorn和GraalVM分別推薦在JDK8及以上和JDK11及以上使用,后者支持更多語言并性能更優(yōu)
    2025-01-01
  • Java分形繪制山脈模型

    Java分形繪制山脈模型

    這篇文章主要為大家詳細(xì)介紹了Java分形繪制山脈模型,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • java 對文件夾目錄進行深度遍歷實例代碼

    java 對文件夾目錄進行深度遍歷實例代碼

    這篇文章主要介紹了java 對文件夾目錄進行深度遍歷實例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • response.setContentType()參數(shù)以及作用詳解

    response.setContentType()參數(shù)以及作用詳解

    這篇文章主要介紹了response.setContentType()參數(shù)以及作用詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • java json字符串轉(zhuǎn)JSONObject和JSONArray以及取值的實例

    java json字符串轉(zhuǎn)JSONObject和JSONArray以及取值的實例

    這篇文章主要介紹了java json字符串轉(zhuǎn)JSONObject和JSONArray以及取值的實例的相關(guān)資料,需要的朋友可以參考下
    2017-05-05

最新評論