springboot?jdbcTemplate?多源配置及特殊場景使用說明
以mysql 說明:
對于多數(shù)據(jù)源中大致分為兩種 一個mysql服務(wù)器,多個庫,另外一種就是多個mysql服務(wù)器多個庫表。
對于以上通用配置如下:以mysql8說明
#第一個庫 spring.datasource.master.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.master.jdbc-url=jdbc:mysql://localhost:3306/aa?&serverTimezone=Asia/Shanghai spring.datasource.master.username=** spring.datasource.master.password=** spring.datasource.master.driver-class-name=com.mysql.cj.jdbc.Driver #第二個庫 spring.datasource.slave.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.slave.jdbcurl=jdbc:mysql://localhost:3306/bb?&serverTimezone=Asia/Shanghai #spring.datasource.slave.username=** #spring.datasource.slave.password=** #spring.datasource.slave.driver-class-name=com.mysql.cj.jdbc.Driver
mysql8和mysql5版本上配置的驅(qū)動要注意 ,8對應(yīng) com.mysql.cj.jdbc.Driver 5對應(yīng)com.mysql.jdbc.Driver
另外 我使用的是jdbc ,所以url就使用 jdbcurl.
以下 配置說明:
@Configuration
public class DataSourceConfiguration {
@Bean
@ConfigurationProperties(prefix = "spring.datasource.master")
@Primary
public DataSource masterDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix = "spring.datasource.slave")
public DataSource slaveDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@Primary
public JdbcTemplate masterJdbcTemplate(@Qualifier("masterDataSource") DataSource primaryDataSource) {
return new JdbcTemplate(primaryDataSource);
}
@Bean
public JdbcTemplate slaveJdbcTemplate(@Qualifier("slaveDataSource") DataSource secondaryDataSource) {
return new JdbcTemplate(secondaryDataSource);
}
}以下使用
@Resource(name = "masterJdbcTemplate") private JdbcTemplate masterJdbcTemplate; @Resource(name = "slaveJdbcTemplate") private JdbcTemplate slaveJdbcTemplate;
這以上 特別說明,針對bean的名字一定要備注上,不然會默認(rèn)指向。
有一種特殊情景下使用說明:
在數(shù)據(jù)同步的時候,需要進(jìn)行執(zhí)行事務(wù)。但是又是在同一個服務(wù)器下的兩個庫,這個時候我們實(shí)際執(zhí)行的是mysql庫的事務(wù)。這個時候我們使用一種不區(qū)分庫而又要執(zhí)行的事務(wù)。以下進(jìn)行說明:
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.jdbc-url=jdbc:mysql://127.0.0.1:3306/?&serverTimezone=Asia/Shanghai spring.datasource.username=** spring.datasource.password=** spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
使用這種,不指定庫,只指定服務(wù)器。
配置:
@Configuration
public class DataSourceConfiguration {
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}使用:
@Autowired private JdbcTemplate jdbcTemplate;
備注:這種模式使用,就需要在執(zhí)行sql的方式注意: 格式: <database>.<tableName>
例如: 需要執(zhí)行a數(shù)據(jù)庫的b表的sql: a.b
補(bǔ)充一種使用方法 一個事務(wù)涉及到的多個sql,這些sql分別對應(yīng)不同的database(事務(wù)針對服務(wù)器)
@Autowired
private TransactionTemplate transactionTemplate;
transactionTemplate.execute(status -> {
try {
jdbcTemplate.execute("SET GTID_NEXT='" +gtid + "'");
List<String> arrSql = JSON.parseArray(sqlArr, String.class);
for (String sql : arrSql) {
jdbcTemplate.execute(sql);
}
return null;
} catch (Exception e) {
status.setRollbackOnly();
e.printStackTrace();
return null;
}
});到此這篇關(guān)于springboot jdbcTemplate 多源配置以及特殊場景使用的文章就介紹到這了,更多相關(guān)springboot jdbcTemplate 多源配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot?配置多個JdbcTemplate的實(shí)現(xiàn)步驟
- SpringBoot中使用JdbcTemplate訪問Oracle數(shù)據(jù)庫的案例詳解
- SpringBoot2使用JTA組件實(shí)現(xiàn)基于JdbcTemplate多數(shù)據(jù)源事務(wù)管理(親測好用)
- 詳解SpringBoot中JdbcTemplate的事務(wù)控制
- SpringBoot多數(shù)據(jù)源配置詳細(xì)教程(JdbcTemplate、mybatis)
- SpringBoot jdbctemplate使用方法解析
- springBoot使用JdbcTemplate代碼實(shí)例
- 詳解springboot采用多數(shù)據(jù)源對JdbcTemplate配置的方法
- SpringBoot使用JdbcTemplate操作數(shù)據(jù)庫
相關(guān)文章
Java?Spring的核心與設(shè)計(jì)思想你知道嗎
這篇文章主要為大家詳細(xì)介紹了Java?Spring的核心與設(shè)計(jì)思想,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03
Java實(shí)現(xiàn)鼠標(biāo)拖放功能的方法
這篇文章主要介紹了Java實(shí)現(xiàn)鼠標(biāo)拖放功能的方法,很實(shí)用的功能,需要的朋友可以參考下2014-07-07
java實(shí)現(xiàn)裝飾器模式(Decorator Pattern)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)裝飾器模式Decorator Pattern,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-10-10
SpringBoot-application.yml多環(huán)境配置詳解
本文主要介紹了SpringBoot-application.yml多環(huán)境配置詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07

