springboot+springJdbc+postgresql 实现多数据源的配置

 更新时间:2021年09月06日 10:26:42   作者:董懂  
本文主要介绍了springboot+springJdbc+postgresql 实现多数据源的配置,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

背景

最近公司在服务拆迁,接口转移,相同的功能接口到要迁移到对应的服务中,因为时间比较赶,别问为什么没给时间,没人,没资源,但是活还是得干的,为了减少工作量和稳妥的需要分两步走

  • 先迁移相关代码,保证包的路径不变,请求接口的路径不变
  • 将迁移的相关代码进行迁表迁库(这目前还没做,计划9月实施)

实施

配置文件

数据库配置相关类

import com.alibaba.druid.pool.DruidDataSource;

import java.io.Serializable;
import java.sql.SQLException;

public class JdbcConfig implements Serializable {
   public JdbcConfig() {
      super();
      // TODO Auto-generated constructor stub
   }

   public boolean isDecrypt() {
      return decrypt;
   }

   public void setDecrypt(boolean decrypt) {
      this.decrypt = decrypt;
   }

   public String getDriverClass() {
      return driverClass;
   }

   public void setDriverClass(String driverClass) {
      this.driverClass = driverClass;
   }

   public String getTerminalUrl() { return terminalUrl; }

   public void setTerminalUrl(String terminalUrl) { this.terminalUrl = terminalUrl;   }

   public String getSlaveurl() {
      return slaveurl;
   }

   public void setSlaveurl(String slaveurl) {
      this.slaveurl = slaveurl;
   }

   public String getUsername() {
      return username;
   }

   public void setUsername(String username) {
      this.username = username;
   }

   public String getPassword() {
      return password;
   }

   public void setPassword(String password) {
      this.password = password;
   }

   public int getInitialSize() {
      return initialSize;
   }

   public void setInitialSize(int initialSize) {
      this.initialSize = initialSize;
   }

   public int getMinIdle() {
      return minIdle;
   }

   public void setMinIdle(int minIdle) {
      this.minIdle = minIdle;
   }

   public int getMaxActive() {
      return maxActive;
   }

   public void setMaxActive(int maxActive) {
      this.maxActive = maxActive;
   }

   private boolean decrypt;
   private String driverClass;
   private String terminalUrl;
   private String slaveurl;
   private String username;
   private String password;
   private int initialSize;
   private int minIdle;
   private int maxActive;

   public DruidDataSource mainds() throws SQLException {
      DruidDataSource ds = ds();
      ds.setUrl(terminalUrl);
      return ds;
   }

   public DruidDataSource slaveds() throws SQLException {
      DruidDataSource ds = ds();
      ds.setUrl(slaveurl);
      return ds;
   }

   private DruidDataSource ds() throws SQLException {
      DruidDataSource ds = new DruidDataSource();
      ds.setUrl(terminalUrl);
      ds.setUsername(username);
      ds.setPassword(password);
      ds.setFilters("config");
      ds.setConnectionProperties("config.decrypt=" + decrypt);
      ds.setInitialSize(initialSize);
      ds.setMaxActive(maxActive);
      ds.setTimeBetweenEvictionRunsMillis(60000);
      ds.setMinEvictableIdleTimeMillis(300000);
      ds.setValidationQuery("select 'X'");
      ds.setTestWhileIdle(true);
      ds.setTestOnBorrow(false);
      ds.setTestOnReturn(false);

      System.out.println("terminalUrl: " + terminalUrl);
      return ds;

   }
}

相关配置

@Bean
@ConfigurationProperties(prefix = "jdbc")
public JdbcConfig jdbcConfig() {
   return new JdbcConfig();
}
@Bean
@ConfigurationProperties(prefix = "business.jdbc")
public JdbcConfig businessJdbcConfig() {
   return new JdbcConfig();
}

@Bean(initMethod = "init", destroyMethod = "close")
public DruidDataSource businessDataSource() throws SQLException {
   return businessJdbcConfig().mainds();
}

@Bean(initMethod = "init", destroyMethod = "close")
public DruidDataSource masterDataSource() throws SQLException {
   return jdbcConfig().mainds();
}

@Bean("jdbctemplate")
public JdbcTemplate jdbcTemplate() throws SQLException {
   JdbcTemplate t = new JdbcTemplate();
   t.setDataSource(masterDataSource());
   return t;
}

@Bean("businessJdbctemplate")
public JdbcTemplate businessjdbcTemplate() throws SQLException {
   JdbcTemplate t = new JdbcTemplate();
   t.setDataSource(businessDataSource());
   return t;
}

使用

@Bean
public RobotDataDao robotDataDao() throws SQLException {
   RobotDataDao dao = new RobotDataDao();
   dao.setJdbcTemplate(jdbcTemplate());
   dao.setTableName("t_business_robot_data");
   dao.setPrimaryKey("id");
   dao.setSeqName("seq_t_business");
   return dao;
}
@Bean
public VDeviceDao vdeviceDao() throws SQLException {
   VDeviceDao dao = new VDeviceDao();
   dao.setJdbcTemplate(businessjdbcTemplate());
   dao.setTableName("t_device");
   dao.setPrimaryKey("id");
   dao.setSeqName("seq_t_default");
   return dao;
}

特别注意的,一定要配置的,因为现在有多数据源了就要配置对应的事务配置,单个默认的,多个就要指定

@Configuration
public class TransactionConfig  {
    @Bean
    public PlatformTransactionManager bfscrmTransactionManager(@Qualifier("masterDataSource") DataSource masterDataSource) {
        return new DataSourceTransactionManager(masterDataSource);
    }
}

这就配置好了多个数据源了

到此这篇关于springboot+springJdbc+postgresql 实现多数据源的配置的文章就介绍到这了,更多相关springboot+springJdbc+postgresql多数据源内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Spring Boot整合JWT的实现步骤

    Spring Boot整合JWT的实现步骤

    本文主要介绍了Spring Boot整合JWT,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-08-08
  • javaMybatis映射属性,高级映射详解

    javaMybatis映射属性,高级映射详解

    下面小编就为大家带来一篇javaMybatis映射属性,高级映射详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-11-11
  • 解读JDK1.8 默认使用什么垃圾收集器

    解读JDK1.8 默认使用什么垃圾收集器

    这篇文章主要介绍了解读JDK1.8 默认使用什么垃圾收集器,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-05-05
  • IntelliJ IDEA 好用插件之analyze inspect code详解

    IntelliJ IDEA 好用插件之analyze inspect code详解

    这篇文章主要介绍了IntelliJ IDEA 好用插件之analyze inspect code的相关知识,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2020-12-12
  • 解决SpringBoot项目启动后网页显示Please sign in的问题

    解决SpringBoot项目启动后网页显示Please sign in的问题

    这篇文章主要介绍了解决SpringBoot项目启动后网页显示Please sign in的问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-04-04
  • 详解JAVA 函数式编程

    详解JAVA 函数式编程

    这篇文章主要介绍了JAVA 函数式编程的相关资料,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-07-07
  • 利用SpringMVC和Ajax实现文件上传功能

    利用SpringMVC和Ajax实现文件上传功能

    这篇文章主要为大家详细介绍了利用SpringMVC和Ajax实现文件上传功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-08-08
  • Java实现多个数组间的排列组合

    Java实现多个数组间的排列组合

    这篇文章主要为大家详细介绍了Java实现多个数组间的排列组合,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-02-02
  • Java设计模式之责任链模式

    Java设计模式之责任链模式

    这篇文章介绍了Java设计模式之责任链模式,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-10-10
  • Java spring定时任务详解

    Java spring定时任务详解

    这篇文章主要为大家详细介绍了Spring定时任务,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2021-10-10

最新评论