飞道的博客

spring boot集成 mysql、sqlserver、oracle多数据源

210人阅读  评论(0)

应用场景:业务系统开发中,经常会遇到链接多数据源等情况

本文实现:实现链接mysql,oracle,sqlserver,可同时对3中数据源操作

项目目录如下

config目录下,在项目启动时加载不同的数据库sqlSessionFactory

controller目录下,正常的调用接口

dao目录下,分别为对具体的数据库访问方法,为了区分不同数据库,这里分了mysql、sqlserver、oracle目录,分别对mysql、sql server、oracle数据库操作

service目录下,正常的业务处理

resource下mybatis目录分别对3中数据库具体操作

config下mysql配置类


  
  1. package com.liufan.mutildatasource.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.context. annotation.Primary;
  12. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
  13. import javax.sql.DataSource;
  14. //表示这个类为一个配置类
  15. @Configuration
  16. // 配置mybatis的接口类放的地方
  17. @MapperScan(basePackages = "com.liufan.mutildatasource.dao.mysql", sqlSessionFactoryRef = "mysqlSqlSessionFactory")
  18. public class MysqlConfig {
  19. // 将这个对象放入Spring容器中
  20. @Bean(name = "mysqlDataSource")
  21. // 读取application.yml 中的配置参数映射成为一个对象
  22. // prefix表示参数的前缀
  23. @ConfigurationProperties(prefix = "spring.datasource.mysql")
  24. public DataSource getDateSource1() {
  25. return DataSourceBuilder.create().build();
  26. }
  27. @Bean(name = "mysqlSqlSessionFactory")
  28. // @Qualifier表示查找Spring容器中名字为mysqlDataSource的对象
  29. public SqlSessionFactory mysqlSqlSessionFactory( @Qualifier("mysqlDataSource") DataSource datasource)
  30. throws Exception {
  31. SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
  32. bean.setDataSource(datasource);
  33. bean.setMapperLocations(
  34. // 设置mybatis的xml所在位置
  35. new PathMatchingResourcePatternResolver().getResources( "classpath*:mybatis/mysql/*.xml"));
  36. return bean.getObject();
  37. }
  38. @Bean("mysqlSqlSessionTemplate")
  39. // 表示这个数据源是默认数据源
  40. @Primary
  41. public SqlSessionTemplate mysqlsqlsessiontemplate(
  42. @Qualifier("mysqlSqlSessionFactory") SqlSessionFactory sessionfactory) {
  43. return new SqlSessionTemplate(sessionfactory);
  44. }
  45. }

sql server配置类


  
  1. package com.liufan.mutildatasource.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.context. annotation.Primary;
  12. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
  13. import javax.sql.DataSource;
  14. //表示这个类为一个配置类
  15. @Configuration
  16. // 配置mybatis的接口类放的地方
  17. @MapperScan(basePackages = "com.liufan.mutildatasource.dao.sqlserver", sqlSessionFactoryRef = "sqlserverSqlSessionFactory")
  18. public class SqlServerConfig {
  19. // 将这个对象放入Spring容器中
  20. @Bean(name = "sqlserverDataSource")
  21. // 表示这个数据源是默认数据源
  22. @Primary
  23. // 读取application.yml 中的配置参数映射成为一个对象
  24. // prefix表示参数的前缀
  25. @ConfigurationProperties(prefix = "spring.datasource.sqlserver")
  26. public DataSource getDateSource1() {
  27. return DataSourceBuilder.create().build();
  28. }
  29. @Bean(name = "sqlserverSqlSessionFactory")
  30. // 表示这个数据源是默认数据源
  31. @Primary
  32. // @Qualifier表示查找Spring容器中名字为sqlserverDataSource的对象
  33. public SqlSessionFactory sqlserverSqlSessionFactory( @Qualifier("sqlserverDataSource") DataSource datasource)
  34. throws Exception {
  35. SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
  36. bean.setDataSource(datasource);
  37. bean.setMapperLocations(
  38. // 设置mybatis的xml所在位置
  39. new PathMatchingResourcePatternResolver().getResources( "classpath*:mybatis/sqlserver/*.xml"));
  40. return bean.getObject();
  41. }
  42. @Bean("sqlserverSqlSessionTemplate")
  43. public SqlSessionTemplate sqlserversqlsessiontemplate(
  44. @Qualifier("sqlserverSqlSessionFactory") SqlSessionFactory sessionfactory) {
  45. return new SqlSessionTemplate(sessionfactory);
  46. }
  47. }

oracle配置类


  
  1. package com.liufan.mutildatasource.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.context. annotation.Primary;
  12. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
  13. import javax.sql.DataSource;
  14. //表示这个类为一个配置类
  15. @Configuration
  16. // 配置mybatis的接口类放的地方
  17. @MapperScan(basePackages = "com.liufan.mutildatasource.dao.oracle", sqlSessionFactoryRef = "oracleSqlSessionFactory")
  18. public class OracleConfig {
  19. // 将这个对象放入Spring容器中
  20. @Bean(name = "oracleDataSource")
  21. // 读取application.yml 中的配置参数映射成为一个对象
  22. // prefix表示参数的前缀
  23. @ConfigurationProperties(prefix = "spring.datasource.oracle")
  24. public DataSource getDateSource1() {
  25. return DataSourceBuilder.create().build();
  26. }
  27. @Bean(name = "oracleSqlSessionFactory")
  28. // @Qualifier表示查找Spring容器中名字为oracleDataSource的对象
  29. public SqlSessionFactory oracleSqlSessionFactory( @Qualifier("oracleDataSource") DataSource datasource)
  30. throws Exception {
  31. SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
  32. bean.setDataSource(datasource);
  33. bean.setMapperLocations(
  34. // 设置mybatis的xml所在位置
  35. new PathMatchingResourcePatternResolver().getResources( "classpath*:mybatis/oracle/*.xml"));
  36. return bean.getObject();
  37. }
  38. @Bean("oracleSqlSessionTemplate")
  39. public SqlSessionTemplate oraclesqlsessiontemplate(
  40. @Qualifier("oracleSqlSessionFactory") SqlSessionFactory sessionfactory) {
  41. return new SqlSessionTemplate(sessionfactory);
  42. }
  43. }

application.yml内容配置多种数据源,datasource下sqlserver、mysql、oracle分别代表定义数据库别名,可自定义名称,但是必须和上面配置类寻找路径相同


  
  1. server:
  2. port: 8080
  3. ### 数据配置
  4. spring:
  5. datasource:
  6. sqlserver:
  7. jdbc-url: jdbc:sqlserver: //192.168.0.208:1433;database=aaaa;
  8. username: aaa
  9. password: aaa
  10. driver- class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
  11. oracle:
  12. jdbc-url: jdbc:oracle:thin:@ 192.168 .0 .209: 1521/orcl
  13. username: aaa
  14. password: aaa
  15. driver- class-name: oracle.jdbc.driver.OracleDriver
  16. mysql:
  17. jdbc-url: jdbc:mysql: //192.168.0.125:3306/tspsdb?useUnicode=true&characterEncoding=utf8
  18. username: aaa
  19. password: aaa
  20. driver- class-name: com.mysql.jdbc.Driver

pom.xml文件如下,oracle和sqlserver采用本地引入方式


  
  1. <dependency>
  2. <groupId>org.mybatis.spring.boot </groupId>
  3. <artifactId>mybatis-spring-boot-starter </artifactId>
  4. <version>2.1.2 </version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot </groupId>
  8. <artifactId>spring-boot-devtools </artifactId>
  9. <scope>runtime </scope>
  10. <optional>true </optional>
  11. </dependency>
  12. <dependency>
  13. <groupId>com.oracle.ojdbc </groupId>
  14. <artifactId>ojdbc8 </artifactId>
  15. <scope>runtime </scope>
  16. </dependency>
  17. <dependency>
  18. <groupId>mysql </groupId>
  19. <artifactId>mysql-connector-java </artifactId>
  20. <scope>runtime </scope>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.springframework.boot </groupId>
  24. <artifactId>spring-boot-configuration-processor </artifactId>
  25. <optional>true </optional>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.projectlombok </groupId>
  29. <artifactId>lombok </artifactId>
  30. <optional>true </optional>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.boot </groupId>
  34. <artifactId>spring-boot-starter-test </artifactId>
  35. <scope>test </scope>
  36. <exclusions>
  37. <exclusion>
  38. <groupId>org.junit.vintage </groupId>
  39. <artifactId>junit-vintage-engine </artifactId>
  40. </exclusion>
  41. </exclusions>
  42. </dependency>
  43. <dependency>
  44. <groupId>com.alibaba </groupId>
  45. <artifactId>fastjson </artifactId>
  46. <version>1.2.66 </version>
  47. </dependency>
  48. <dependency>
  49. <groupId>org.springframework.boot </groupId>
  50. <artifactId>spring-boot-starter-web </artifactId>
  51. </dependency>
  52. <dependency>
  53. <groupId>com.microsoft.sqlserver </groupId>
  54. <artifactId>sqljdbc4 </artifactId>
  55. <version>4.0 </version>
  56. <scope>system </scope>
  57. <systemPath>${project.basedir}/lib/sqljdbc4-4.0.jar </systemPath>
  58. </dependency>
  59. <dependency>
  60. <groupId>com.oracle </groupId>
  61. <artifactId>ojdbc6 </artifactId>
  62. <version>11.2 </version>
  63. <scope>system </scope>
  64. <systemPath>${project.basedir}/lib/ojdbc-6.jar </systemPath>
  65. </dependency>
  66. <dependency>
  67. <groupId>cn.easyproject </groupId>
  68. <artifactId>orai18n </artifactId>
  69. <version>12.1.0.2.0 </version>
  70. </dependency>
  71. </dependencies>
  72. <!-- 阿里云仓库 -->
  73. <repositories> <!-- 代码库 -->
  74. <repository>
  75. <id>maven-ali </id>
  76. <url>http://maven.aliyun.com/nexus/content/groups/public// </url>
  77. <releases>
  78. <enabled>true </enabled>
  79. </releases>
  80. <snapshots>
  81. <enabled>true </enabled>
  82. <updatePolicy>always </updatePolicy>
  83. <checksumPolicy>fail </checksumPolicy>
  84. </snapshots>
  85. </repository>
  86. </repositories>

注意点:xml文件命名空间对应到具体dao下具体类

启动项目,通过controller中方法访问,输出如下图内容,controller和service就不贴代码了,

代码地址:https://github.com/liufan1993/multipleDataSources

 


转载:https://blog.csdn.net/it1993/article/details/105106189
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场