当前位置:   article > 正文

SpringBoot整合mysql、postgres、sqlserver实现多数据源配置案例_spring boot 项目集成mysql和sqlserver

spring boot 项目集成mysql和sqlserver

        分享一下近期处理的一个小demo,关于配置多数据源实现不同服务之间的数据推送和数据治理。第一次接触到pg库和sqlserver一头雾水,选择了JDBC+mybatis-plus的方式去链接。

1、首先要引入以下依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-test</artifactId>
  5. <scope>test</scope>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-web</artifactId>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.projectlombok</groupId>
  13. <artifactId>lombok</artifactId>
  14. <version>1.18.12</version>
  15. <optional>true</optional>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.apache.httpcomponents</groupId>
  19. <artifactId>httpclient</artifactId>
  20. <version>4.5.10</version>
  21. </dependency>
  22. <dependency>
  23. <groupId>com.alibaba</groupId>
  24. <artifactId>fastjson</artifactId>
  25. <version>1.2.62</version>
  26. </dependency>
  27. <!--mysql 驱动-->
  28. <dependency>
  29. <groupId>mysql</groupId>
  30. <artifactId>mysql-connector-java</artifactId>
  31. <version>8.0.22</version>
  32. </dependency>
  33. <!--postgresql 驱动-->
  34. <dependency>
  35. <groupId>org.postgresql</groupId>
  36. <artifactId>postgresql</artifactId>
  37. <scope>runtime</scope>
  38. </dependency>
  39. <!--sqlserver 配置-->
  40. <dependency>
  41. <groupId>com.microsoft.sqlserver</groupId>
  42. <artifactId>mssql-jdbc</artifactId>
  43. <version>9.4.0.jre8</version>
  44. </dependency>
  45. <!--//spring默认的jdbc连接-->
  46. <dependency>
  47. <groupId>org.springframework.boot</groupId>
  48. <artifactId>spring-boot-starter-jdbc</artifactId>
  49. </dependency>
  50. <dependency>
  51. <groupId>com.alibaba</groupId>
  52. <artifactId>druid</artifactId>
  53. <version>1.1.13</version>
  54. </dependency>
  55. <dependency>
  56. <groupId>com.baomidou</groupId>
  57. <artifactId>mybatis-plus-boot-starter</artifactId>
  58. <version>3.4.0</version>
  59. </dependency>
  60. <dependency>
  61. <groupId>cn.afterturn</groupId>
  62. <artifactId>easypoi-base</artifactId>
  63. <version>4.4.0</version>
  64. </dependency>
  65. </dependencies>
  66. <build>
  67. <plugins>
  68. <plugin>
  69. <groupId>org.springframework.boot</groupId>
  70. <artifactId>spring-boot-maven-plugin</artifactId>
  71. <version>2.7.12</version>
  72. <configuration>
  73. <!-- 指定该Main Class为全局的唯一入口 -->
  74. <mainClass>com.zkgl.ZsjDemoApplication</mainClass>
  75. </configuration>
  76. <executions>
  77. <execution>
  78. <goals>
  79. <!--可以把依赖的包都打包到生成的Jar包中 -->
  80. <goal>repackage</goal>
  81. </goals>
  82. </execution>
  83. </executions>
  84. </plugin>
  85. </plugins>
  86. </build>

2、demo的项目结构如下

3、yml配置文件

  1. server:
  2. port:3666
  3. spring:
  4. application:
  5. name: multiple-data
  6. datasource:
  7. show-sql: false
  8. db1:
  9. jdbc-url: jdbc:mysql://ip地址:端口/数据库名?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
  10. username: 数据库账号
  11. password: 数据库密码
  12. driver-class-name: com.mysql.cj.jdbc.Driver
  13. db2:
  14. jdbc-url: jdbc:postgresql://ip地址:端口/数据库名?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
  15. username: 数据库账号
  16. password: 数据库密码
  17. driver-class-name: org.postgresql.Driver
  18. db3:
  19. jdbc-url: jdbc:sqlserver://ip地址:端口;databaseName=数据库名
  20. username: 数据库账号
  21. password: 数据库密码
  22. driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver

4、配置类

  1. package com.zkgl.config;
  2. import org.apache.ibatis.session.SqlSessionFactory;
  3. import org.mybatis.spring.SqlSessionFactoryBean;
  4. import org.mybatis.spring.SqlSessionTemplate;
  5. import org.mybatis.spring.annotation.MapperScan;
  6. import org.springframework.beans.factory.annotation.Qualifier;
  7. import org.springframework.boot.context.properties.ConfigurationProperties;
  8. import org.springframework.boot.jdbc.DataSourceBuilder;
  9. import org.springframework.context.annotation.Bean;
  10. import org.springframework.context.annotation.Configuration;
  11. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
  12. import javax.sql.DataSource;
  13. /**
  14. * @Author: 袁振豪
  15. * @Description:
  16. * @Date: Created in 2023-07-23 11:04 PM
  17. * @Modified By:
  18. */
  19. @Configuration
  20. @MapperScan(basePackages = "com.zkgl.dao.db1",sqlSessionFactoryRef = "db1SqlSessionFactory")
  21. public class MysqlDataSourceConfig {
  22. static final String MAPPER_LOCATION = "classpath:/mapper/db1/*.xml";
  23. @Bean("db1DataSource")
  24. @ConfigurationProperties(prefix = "spring.datasource.db1")
  25. public DataSource getDb1DataSource(){
  26. return DataSourceBuilder.create().build();
  27. }
  28. @Bean("db1SqlSessionFactory")
  29. public SqlSessionFactory db1SqlSessionFactory(@Qualifier("db1DataSource") DataSource dataSource) throws Exception {
  30. SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
  31. bean.setDataSource(dataSource);
  32. bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION));
  33. return bean.getObject();
  34. }
  35. @Bean("db1SqlSessionTemplate")
  36. public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("db1SqlSessionFactory") SqlSessionFactory sqlSessionFactory){
  37. return new SqlSessionTemplate(sqlSessionFactory);
  38. }
  39. }
  1. package com.zkgl.config;
  2. import org.apache.ibatis.session.SqlSessionFactory;
  3. import org.mybatis.spring.SqlSessionFactoryBean;
  4. import org.mybatis.spring.SqlSessionTemplate;
  5. import org.mybatis.spring.annotation.MapperScan;
  6. import org.springframework.beans.factory.annotation.Qualifier;
  7. import org.springframework.boot.context.properties.ConfigurationProperties;
  8. import org.springframework.boot.jdbc.DataSourceBuilder;
  9. import org.springframework.context.annotation.Bean;
  10. import org.springframework.context.annotation.Configuration;
  11. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
  12. import javax.sql.DataSource;
  13. /**
  14. * @Author: 袁振豪
  15. * @Description:
  16. * @Date: Created in 2023-07-23 11:04 PM
  17. * @Modified By:
  18. */
  19. @Configuration
  20. @MapperScan(basePackages = "com.zkgl.dao.db2",sqlSessionFactoryRef = "db2SqlSessionFactory")
  21. public class PgDataSourceConfig {
  22. static final String MAPPER_LOCATION = "classpath:/mapper/db2/*.xml";
  23. @Bean("db2DataSource")
  24. @ConfigurationProperties(prefix = "spring.datasource.db2")
  25. public DataSource getDb2DataSource(){
  26. return DataSourceBuilder.create().build();
  27. }
  28. @Bean("db2SqlSessionFactory")
  29. public SqlSessionFactory db2SqlSessionFactory(@Qualifier("db2DataSource") DataSource dataSource) throws Exception {
  30. SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
  31. bean.setDataSource(dataSource);
  32. bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION));
  33. return bean.getObject();
  34. }
  35. @Bean("db2SqlSessionTemplate")
  36. public SqlSessionTemplate db2SqlSessionTemplate(@Qualifier("db2SqlSessionFactory") SqlSessionFactory sqlSessionFactory){
  37. return new SqlSessionTemplate(sqlSessionFactory);
  38. }
  39. }
  1. package com.zkgl.config;
  2. import org.apache.ibatis.session.SqlSessionFactory;
  3. import org.mybatis.spring.SqlSessionFactoryBean;
  4. import org.mybatis.spring.SqlSessionTemplate;
  5. import org.springframework.beans.factory.annotation.Qualifier;
  6. import org.springframework.boot.context.properties.ConfigurationProperties;
  7. import org.springframework.boot.jdbc.DataSourceBuilder;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
  10. import javax.sql.DataSource;
  11. import org.mybatis.spring.annotation.MapperScan;
  12. import org.springframework.context.annotation.Configuration;
  13. /**
  14. * @Author: 袁振豪
  15. * @Description:
  16. * @Date: Created in 2023-07-23 11:04 PM
  17. * @Modified By:
  18. */
  19. @Configuration
  20. @MapperScan(basePackages = "com.zkgl.dao.db3",sqlSessionFactoryRef = "db3SqlSessionFactory")
  21. public class SqlServerDataSourceConfig {
  22. static final String MAPPER_LOCATION = "classpath:/mapper/db3/*.xml";
  23. @Bean("db3DataSource")
  24. @ConfigurationProperties(prefix = "spring.datasource.db3")
  25. public DataSource getDb1DataSource(){
  26. return DataSourceBuilder.create().build();
  27. }
  28. @Bean("db3SqlSessionFactory")
  29. public SqlSessionFactory db1SqlSessionFactory(@Qualifier("db3DataSource") DataSource dataSource) throws Exception {
  30. SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
  31. bean.setDataSource(dataSource);
  32. bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION));
  33. return bean.getObject();
  34. }
  35. @Bean("db3SqlSessionTemplate")
  36. public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("db3SqlSessionFactory") SqlSessionFactory sqlSessionFactory){
  37. return new SqlSessionTemplate(sqlSessionFactory);
  38. }
  39. }

5、controller、dao、service以及对应的.xml文件可以根据自身业务情况进行开发,再次不做过多赘述。

总结:要进行多数据源的配置,以此案例为例,最重要的是对各个库的适配,也就是各自的驱动。众所周知,要使用JDBC连接数据库,主要有以下步骤:

  1. 注册驱动
  2. 建立数据库连接
  3. 创建数据库操作对象
  4. 执行SQL语句
  5. 处理查询结果集
  6. 关闭资源 

而在本案例中,pom中引入了相关依赖,在yml配置了驱动,之后在config中以Bean的形式分别命名和初始化相关配置,这样在Springboot项目中,通过@SpringBootApplication注解中的@EnableAutoConfigtion注解就可以扫描到这些配置好的Bean,从而正常使用了。


                
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/394177
推荐阅读
相关标签
  

闽ICP备14008679号