• springboot 多数据源(如何连接两个数据库)


    由于项目需要,对原有项目新增 别的数据库数据的交互。原有项目数据源为MySQL,新增数据源为Oracle。

    第一步application.yml配置

    1. spring:
    2. datasource:
    3. one:
    4. type: com.alibaba.druid.pool.DruidDataSource
    5. driverClassName: com.mysql.cj.jdbc.Driver
    6. jdbc-url: jdbc:mysql://127.0.0.1:3306/abc #测试数据库
    7. username: root
    8. password: root
    9. two:
    10. type: com.alibaba.druid.pool.DruidDataSource
    11. driverClassName: oracle.jdbc.driver.OracleDriver
    12. jdbc-url: jdbc:oracle:thin:@127.0.0.1:1521/def #测试数据库,该数据库连接可以是远程服务器
    13. username: u23
    14. password: u23
     第二步:添加pom依赖,在pom文件中新增
    1. <dependency>
    2. <groupId>mysqlgroupId>
    3. <artifactId>mysql-connector-javaartifactId>
    4. dependency>
    5. <dependency>
    6. <groupId>com.oraclegroupId>
    7. <artifactId>ojdbc6artifactId>
    8. <version>11.2.0.4.0version>
    9. dependency>
    10. <dependency>
    11. <groupId>mysqlgroupId>
    12. <artifactId>mysql-connector-javaartifactId>
    13. <scope>runtimescope>
    14. dependency>
    15. <dependency>
    16. <groupId>org.springframework.bootgroupId>
    17. <artifactId>spring-boot-starter-data-jpaartifactId>
    18. <version>1.5.9.RELEASEversion>
    19. dependency>
    第三步 配置Oracle数据库依赖,该处理比较关键。
    

    在项目工作空间有pom.xml 下  在CMD中执行以下语句,红色为你本地安装oracle目录下的ojdbc6.jar包,DgroupId,DartifactId,Dversion与pom文件oracle驱动内容一致。

    mvn install:install-file -Dfile="E:\app\product\11.2.0\dbhome_1\jdbc\lib\ojdbc6.jar" -DgroupId="com.oracle" -DartifactId="ojdbc6" -Dversion="11.2.0.4.0" -Dpackaging="jar"

    执行完成后在你本地maven依赖库中你会发现oracle依赖包已经处理好了

    *注  以上针对项目服务与数据库都在一个服务器上的情况,执行后都正常。

    但测试连接远程服务器上的Oracle发现出现一下问题:SpringBoot 2.x HikariPool - Driver does not support get/set network timeout for connect  提示驱动不支持,找了很多发现

    原因:ojdbc6没实现这个jdbc标准的函数而已

    解决方案

    使用高版本的Ojdbc即可,本次使用的是Ojdbc7,下载(连接:https://aerfazhe.lanzouw.com/ibhD4yo3r4h
    后安装到本地Mvn库中即可。方法一样。感谢https://www.cnblogs.com/aerfazhe/p/15791252.html博主)

    更新POM中ojdbc

    1. com.oracle
    2. ojdbc7
    3. 12.0.1.2

    第四步  application启动类增加注解,@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})意为:去掉springboot服务启动时对于数据源配置的自动导入。

    1. @SpringBootApplication
    2. @EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
    3. public class TicketsApplication {
    4. public static void main(String[] args) {
    5. SpringApplication.run(TicketsApplication.class, args);
    6. }
    7. }

    第五步,该步就是具体的代码

    新建两个数据源类,类中的

    @MapperScan  basePackages = "com.ticket.tickets.dao.test1"  该路径为你的Mapper Java类所在文件夹路径。

    sqlSessionFactoryRef = "SqlSessionFactory1"  该值为该类中你自己命名的SqlSessionFactory的bean名字。

    MySQL如下 MysqlConfig.java

    1. import javax.sql.DataSource;
    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 org.springframework.jdbc.datasource.DataSourceTransactionManager;
    14. @Configuration
    15. @MapperScan(basePackages = "com.ticket.tickets.dao.test1",sqlSessionFactoryRef = "SqlSessionFactory1")
    16. public class MysqlConfig {
    17. @Primary
    18. @Bean(name = "DataSource1")
    19. @ConfigurationProperties(prefix = "spring.datasource.one")
    20. public DataSource testDataSource() {
    21. return DataSourceBuilder.create().build();
    22. }
    23. @Primary
    24. @Bean(name = "SqlSessionFactory1")
    25. public SqlSessionFactory testSqlSessionFactory(@Qualifier("DataSource1") DataSource dataSource) throws Exception {
    26. SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    27. bean.setDataSource(dataSource);
    28. bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/*.xml"));
    29. return bean.getObject();
    30. }
    31. @Primary
    32. @Bean(name = "TransactionManager1")
    33. public DataSourceTransactionManager testTransactionManager(@Qualifier("DataSource1") DataSource dataSource) {
    34. return new DataSourceTransactionManager(dataSource);
    35. }
    36. @Primary
    37. @Bean(name = "SqlSessionTemplate1")
    38. public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("SqlSessionFactory1") SqlSessionFactory sqlSessionFactory) throws Exception {
    39. return new SqlSessionTemplate(sqlSessionFactory);
    40. }
    41. }

    ORACLE如下  OracleConfig.java

    1. import org.apache.ibatis.session.SqlSessionFactory;
    2. import org.mybatis.spring.SqlSessionFactoryBean;
    3. import org.mybatis.spring.SqlSessionTemplate;
    4. import org.mybatis.spring.annotation.MapperScan;
    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.context.annotation.Configuration;
    10. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
    11. import org.springframework.jdbc.datasource.DataSourceTransactionManager;
    12. import javax.sql.DataSource;
    13. @Configuration
    14. @MapperScan(basePackages = "com.ticket.tickets.dao.test2",sqlSessionFactoryRef = "SqlSessionFactory2")
    15. public class OracleConfig {
    16. @Bean(name = "DataSource2")
    17. @ConfigurationProperties(prefix = "spring.datasource.two")
    18. public DataSource testDataSource() {
    19. return DataSourceBuilder.create().build();
    20. }
    21. @Bean(name = "SqlSessionFactory2")
    22. public SqlSessionFactory testSqlSessionFactory(@Qualifier("DataSource2") DataSource dataSource) throws Exception {
    23. SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    24. bean.setDataSource(dataSource);
    25. bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/*.xml"));
    26. return bean.getObject();
    27. }
    28. @Bean(name = "TransactionManager2")
    29. public DataSourceTransactionManager testTransactionManager(@Qualifier("DataSource2") DataSource dataSource) {
    30. return new DataSourceTransactionManager(dataSource);
    31. }
    32. @Bean(name = "SqlSessionTemplate2")
    33. public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("SqlSessionFactory2") SqlSessionFactory sqlSessionFactory) throws Exception {
    34. return new SqlSessionTemplate(sqlSessionFactory);
    35. }
    36. }

    两个mapper类

    StudentMapper.java

    1. import com.ticket.tickets.pojo.Student;
    2. import org.springframework.stereotype.Component;
    3. @Component
    4. public interface StudentMapper {
    5. Student selectByPrimaryKey(Integer id);
    6. }

    StuMapper.java 

    1. import com.ticket.tickets.pojo.Stu;
    2. import org.springframework.stereotype.Component;
    3. @Component
    4. public interface StuMapper {
    5. Stu selectByPrimaryKey(int id);
    6. }

    两个实体类 Student  Stu  根据你自己数据库字段创建。

    工程的resources 下新建mybatis 文件夹,新建两个mapper.xml文件,注意核对namespace路径

    StudentMapper.xml

    1. "1.0" encoding="UTF-8" ?>
    2. mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    3. <mapper namespace="com.ticket.tickets.dao.test1.StudentMapper">
    4. <select id="selectByPrimaryKey" resultType="com.ticket.tickets.pojo.Student" parameterType="java.lang.Integer" >
    5. select * from student
    6. where id = #{id,jdbcType=INTEGER}
    7. select>
    8. mapper>

    StuMapper.xml 

    1. "1.0" encoding="UTF-8" ?>
    2. mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    3. <mapper namespace="com.ticket.tickets.dao.test2.StuMapper">
    4. <select id="selectByPrimaryKey" resultType="com.ticket.tickets.pojo.Stu" parameterType="java.lang.Integer" >
    5. select * from stu where id = #{id}
    6. select>
    7. mapper>

    编写controller 调用

    1. import com.ticket.tickets.dao.test1.StudentMapper;
    2. import com.ticket.tickets.dao.test2.StuMapper;
    3. import com.ticket.tickets.pojo.Stu;
    4. import com.ticket.tickets.pojo.Student;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.web.bind.annotation.GetMapping;
    7. import org.springframework.web.bind.annotation.RequestMapping;
    8. import org.springframework.web.bind.annotation.RestController;
    9. @RestController
    10. @RequestMapping(value = "/stu")
    11. public class JDBCController {
    12. @Autowired
    13. private StudentMapper studentMapper;
    14. @Autowired
    15. private StuMapper stuMapper;
    16. @GetMapping("/getStudent")
    17. public Student getStudentByPostgres(){
    18. return studentMapper.selectByPrimaryKey(2);
    19. }
    20. @GetMapping("/getStu")
    21. public Stu getStuByOracle(){
    22. return stuMapper.selectByPrimaryKey(2);
    23. }
    24. }

    开始调用

    测试调用mysql数据库数据

    测试调用oracle数据库数据

    以上为本人处理过程,如对您没有帮助请忽略。

  • 相关阅读:
    循环肿瘤细胞(CTCs)分选进样系统微小正负压精密控制的解决方案
    Oracle中i列定义int类型对应Java中java.math.BigDecimal类型
    aijs 对象排序
    Gallery2设置状态栏和底部栏背景流程
    基于Java+SpringBoot+Vue+uniapp微信小程序实现仓储管理系统
    ubuntu搭建MongoDB副本集
    求最大公约数的几种常见的方法 【详解】
    IDEA高效编程快捷键
    如何给element el-date-picker日期组件设置时间按照时间禁用
    软件测试/测试开发丨App自动化测试-弹窗异常处理
  • 原文地址:https://blog.csdn.net/xueshenghu/article/details/127107924