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

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

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

分享一下近期處理的一個(gè)小demo,關(guān)于配置多數(shù)據(jù)源實(shí)現(xiàn)不同服務(wù)之間的數(shù)據(jù)推送和數(shù)據(jù)治理。第一次接觸到pg庫(kù)和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ū)動(dòng)-->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>8.0.22</version>
		</dependency>
		<!--postgresql 驅(qū)動(dòng)-->
		<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的項(xiàng)目結(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ù)庫(kù)名?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
      username: 數(shù)據(jù)庫(kù)賬號(hào)
      password: 數(shù)據(jù)庫(kù)密碼
      driver-class-name: com.mysql.cj.jdbc.Driver
    db2:
      jdbc-url: jdbc:postgresql://ip地址:端口/數(shù)據(jù)庫(kù)名?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
      username: 數(shù)據(jù)庫(kù)賬號(hào)
      password: 數(shù)據(jù)庫(kù)密碼
      driver-class-name: org.postgresql.Driver
    db3:
      jdbc-url: jdbc:sqlserver://ip地址:端口;databaseName=數(shù)據(jù)庫(kù)名
      username: 數(shù)據(jù)庫(kù)賬號(hào)
      password: 數(shù)據(jù)庫(kù)密碼
      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以及對(duì)應(yīng)的.xml文件可以根據(jù)自身業(yè)務(wù)情況進(jìn)行開(kāi)發(fā),再次不做過(guò)多贅述。

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

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

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

總結(jié)

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

相關(guān)文章

  • Spring IOC簡(jiǎn)單理解及創(chuàng)建對(duì)象的方式

    Spring IOC簡(jiǎn)單理解及創(chuàng)建對(duì)象的方式

    這篇文章主要介紹了Spring IOC簡(jiǎn)單理解及創(chuàng)建對(duì)象的方式,本文通過(guò)兩種方式給大家介紹創(chuàng)建對(duì)象的方法,通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2021-09-09
  • Springboot項(xiàng)目使用html5的video標(biāo)簽完成視頻播放功能

    Springboot項(xiàng)目使用html5的video標(biāo)簽完成視頻播放功能

    這篇文章主要介紹了Springboot項(xiàng)目使用html5的video標(biāo)簽完成視頻播放功能,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12
  • Spring依賴注入(DI)兩種方式的示例詳解

    Spring依賴注入(DI)兩種方式的示例詳解

    這篇文章主要介紹了Spring依賴注入(DI)的兩種方式:setter注入和構(gòu)造器注入。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2022-06-06
  • Java常見(jiàn)的鎖策略圖文詳解(附實(shí)例代碼)

    Java常見(jiàn)的鎖策略圖文詳解(附實(shí)例代碼)

    Java中的鎖方法是指通過(guò)特定的機(jī)制來(lái)確保多線程環(huán)境下對(duì)共享資源的互斥訪問(wèn),以避免數(shù)據(jù)不一致和競(jìng)態(tài)條件,這篇文章主要介紹了Java常見(jiàn)鎖策略的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-10-10
  • 關(guān)于@Component和@Bean使用注意

    關(guān)于@Component和@Bean使用注意

    這篇文章主要介紹了關(guān)于@Component和@Bean使用注意,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • Java Flink窗口觸發(fā)器Trigger的用法詳解

    Java Flink窗口觸發(fā)器Trigger的用法詳解

    Trigger(窗口觸發(fā)器)決定了窗口(由 WindowAssigner 產(chǎn)生)什么時(shí)候調(diào)用窗口處理函數(shù)。可以根據(jù)指定的時(shí)間或數(shù)據(jù)元素條件來(lái)決定什么時(shí)候觸發(fā)。本文將詳細(xì)講講其用法,需要的可以參考一下
    2022-07-07
  • Spring?AI聊天功能開(kāi)發(fā)步驟

    Spring?AI聊天功能開(kāi)發(fā)步驟

    本文給大家介紹Spring?AI聊天功能開(kāi)發(fā)步驟,首先引入依賴,繼承父版本的springboot依賴,最好是比較新的依賴,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2024-04-04
  • Java中的傳值與傳引用實(shí)現(xiàn)過(guò)程解析

    Java中的傳值與傳引用實(shí)現(xiàn)過(guò)程解析

    這篇文章主要介紹了java中的傳值與傳引用實(shí)現(xiàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • spring data 連接mongodb的兩種方式

    spring data 連接mongodb的兩種方式

    這篇文章主要介紹了spring data mongodb連接方式詳解,本文給大家分享兩種連接方式,通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2021-08-08
  • java并發(fā)問(wèn)題概述

    java并發(fā)問(wèn)題概述

    這篇文章主要介紹了java并發(fā)問(wèn)題概述,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-12-12

最新評(píng)論

潍坊市| 武宁县| 顺义区| 永济市| 定安县| 灵川县| 德昌县| 稷山县| 房山区| 阿坝县| 墨玉县| 息烽县| 德格县| 沐川县| 西乡县| 张家界市| 正宁县| 米林县| 会宁县| 华亭县| 沾益县| 揭东县| 原阳县| 本溪| 宁南县| 桐城市| 乌拉特中旗| 蛟河市| 略阳县| 原平市| 凭祥市| 姚安县| 西平县| 县级市| 仁怀市| 车险| 罗山县| 谢通门县| 黔南| 鹿泉市| 临朐县|