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多數據源內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- 一篇文章搞定數據庫連接池
- 如何在SpringBoot 中使用 Druid 數據庫連接池
- springboot項目數據庫密碼如何加密
- Spring操作JdbcTemplate數據庫的方法學習
- Spring框架基於註解開發CRUD詳解