SpringBoot整合mysql、postgres及sqlserver實現(xiàn)多數(shù)據(jù)源配置實戰(zhà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)文章
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-05response.setContentType()參數(shù)以及作用詳解
這篇文章主要介紹了response.setContentType()參數(shù)以及作用詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08java json字符串轉(zhuǎn)JSONObject和JSONArray以及取值的實例
這篇文章主要介紹了java json字符串轉(zhuǎn)JSONObject和JSONArray以及取值的實例的相關(guān)資料,需要的朋友可以參考下2017-05-05