国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

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

這篇具有很好參考價值的文章主要介紹了SpringBoot整合mysql、postgres、sqlserver實現(xiàn)多數(shù)據(jù)源配置案例。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

? ? ? ? 分享一下近期處理的一個小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)如下

springboot sqlserver,java,mybatisspringboot sqlserver,java,mybatis

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ù)情況進(jìn)行開發(fā),再次不做過多贅述。

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

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

而在本案例中,pom中引入了相關(guān)依賴,在yml配置了驅(qū)動,之后在config中以Bean的形式分別命名和初始化相關(guān)配置,這樣在Springboot項目中,通過@SpringBootApplication注解中的@EnableAutoConfigtion注解就可以掃描到這些配置好的Bean,從而正常使用了。文章來源地址http://www.zghlxwxcb.cn/news/detail-740204.html

到了這里,關(guān)于SpringBoot整合mysql、postgres、sqlserver實現(xiàn)多數(shù)據(jù)源配置案例的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • SpringBoot整合Mybatis-Plus+Druid實現(xiàn)多數(shù)據(jù)源

    SpringBoot整合Mybatis-Plus+Druid實現(xiàn)多數(shù)據(jù)源

    ??本文主要講解 springboot +mybatisplus + druid 實現(xiàn)多數(shù)據(jù)源配置功能 ?? 主頁傳送門:?? 傳送 Spring Boot: ? ?Spring Boot是一個基于Spring框架的開源Java開發(fā)框架,旨在簡化Spring應(yīng)用程序的開發(fā)、配置和部署。它提供了一種快速、敏捷的方式來構(gòu)建獨立的、生產(chǎn)級別的Spring應(yīng)用程

    2024年02月09日
    瀏覽(25)
  • 【萬字長文】SpringBoot整合Atomikos實現(xiàn)多數(shù)據(jù)源分布式事務(wù)(提供Gitee源碼)

    前言:在最近的實際開發(fā)的過程中,遇到了在多數(shù)據(jù)源的情況下要保證原子性的問題,這個問題當(dāng)時遇到了也是思考了一段時間,后來通過搜集大量資料與學(xué)習(xí),最后是采用了分布式事務(wù)來解決這個問題,在講解之前,在我往期的博客提前搭好了一個SpringBoot整合MyBatis搭建M

    2024年02月14日
    瀏覽(26)
  • springboot+mybatis實現(xiàn)mysql和oracle多數(shù)據(jù)源

    在實際項目中很多時候會涉及到多個數(shù)據(jù)庫的訪問,或者數(shù)據(jù)庫讀寫分離的形式。 下面通過使用 Aspect+注解來實現(xiàn)mysql+oracle的多數(shù)據(jù)源配置(注意:事務(wù)一致性未提供) 首先要去oracle官網(wǎng)下載ojdbc的jar包,根據(jù)oracle的版本去下載,或者在下載的oracle的jdbc包下的lib里面有,然后

    2024年02月07日
    瀏覽(29)
  • SpringBoot整合MybatisPlus多數(shù)據(jù)源

    SpringBoot整合MybatisPlus多數(shù)據(jù)源

    相信在很多使用MybatisPlus框架的小伙伴都會遇到多數(shù)據(jù)源的配置問題,并且官網(wǎng)也給出了推薦使用多數(shù)據(jù)源 (dynamic-datasource-spring-boot-starter) 組件來實現(xiàn)。由于最近項目也在使用這個組件來實現(xiàn)多數(shù)據(jù)源切換,因此想了解一下該組件是如何運行的,經(jīng)過自己的調(diào)試,簡單記錄一

    2024年02月13日
    瀏覽(25)
  • springboot整合kafka多數(shù)據(jù)源

    springboot整合kafka多數(shù)據(jù)源

    在很多與第三方公司對接的時候,或者處在不同的網(wǎng)絡(luò)環(huán)境下,比如在互聯(lián)網(wǎng)和政務(wù)外網(wǎng)的分布部署服務(wù)的時候,我們需要對接多臺kafka來達(dá)到我們的業(yè)務(wù)需求,那么當(dāng)kafka存在多數(shù)據(jù)源的情況,就與單機的情況有所不同。 單機的情況 如果是單機的kafka我們直接通過springboot自

    2024年02月13日
    瀏覽(29)
  • SpringBoot整合Druid配置多數(shù)據(jù)源

    SpringBoot整合Druid配置多數(shù)據(jù)源

    目錄 1.初始化項目 1.1.初始化工程 1.2.添加依賴 1.3.配置yml文件 1.4.Spring Boot 啟動類中添加?@MapperScan?注解,掃描 Mapper 文件夾 1.5.配置使用數(shù)據(jù)源 1.5.1.注解方式 1.5.2.基于AOP手動實現(xiàn)多數(shù)據(jù)源原生的方式 2.結(jié)果展示 Mybatis-Plus:簡介 | MyBatis-Plus (baomidou.com) 在正式開始之前,先初始

    2024年02月01日
    瀏覽(38)
  • springboot整合druid及多數(shù)據(jù)源配置

    springboot整合druid及多數(shù)據(jù)源配置

    本篇主要分兩部分 ①springboot整合druid的代碼配置,以及druid的監(jiān)控頁面演示;②對實際場景中多數(shù)據(jù)源的配置使用進(jìn)行講解。 可以用idea快速生成一個可運行的demo工程,具體可以參考如何快速創(chuàng)建springboot項目 主要用到的依賴如下: ?配置數(shù)據(jù)庫需要的配置文件application.yml( 注

    2024年02月12日
    瀏覽(31)
  • springboot整合多數(shù)據(jù)源的配置以及動態(tài)切換數(shù)據(jù)源,注解切換數(shù)據(jù)源

    springboot整合多數(shù)據(jù)源的配置以及動態(tài)切換數(shù)據(jù)源,注解切換數(shù)據(jù)源

    在許多應(yīng)用程序中,可能需要使用多個數(shù)據(jù)庫或數(shù)據(jù)源來處理不同的業(yè)務(wù)需求。Spring Boot提供了簡便的方式來配置和使用多數(shù)據(jù)源,使開發(fā)人員能夠輕松處理多個數(shù)據(jù)庫連接。如果你的項目中可能需要隨時切換數(shù)據(jù)源的話,那我這篇文章可能能幫助到你 ??:這里對于pom文件

    2024年02月10日
    瀏覽(33)
  • SpringBoot+MybatisPlus+dynamic-datasources實現(xiàn)連接Postgresql和mysql多數(shù)據(jù)源

    SpringBoot+MybatisPlus+dynamic-datasources實現(xiàn)連接Postgresql和mysql多數(shù)據(jù)源

    dynamic-datasource-spring-boot-starter實現(xiàn)動態(tài)數(shù)據(jù)源Mysql和Sqlserver: dynamic-datasource-spring-boot-starter實現(xiàn)動態(tài)數(shù)據(jù)源Mysql和Sqlserver_dynamic-datasource-spring-boot-starter mysql sqlse-CSDN博客 SpringBoot中整合MybatisPlus快速實現(xiàn)Mysql增刪改查和條件構(gòu)造器: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/13

    2024年01月21日
    瀏覽(32)
  • SpringBoot -02 SpringBoot整合Mybatis、Druid數(shù)據(jù)源、單元測試、JSP

    mybatis起步依賴 mysql啟動依賴 數(shù)據(jù)源配置 mybatis相關(guān)配置 加載mybatis配置文件 mybatis別名配置 日志配置 加載mapper映射文件配置文件 http://localhost:8080/users Druid連接池后臺監(jiān)控:http://localhost:8080/druid/index.html SpringBoot對JSP的支持不是很友好,所以JSP很少被使用 springboot 不支持jsp 但是

    2024年02月06日
    瀏覽(54)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包