springboot?jdbcTemplate?多源配置及特殊場(chǎng)景使用說(shuō)明
以mysql 說(shuō)明:
對(duì)于多數(shù)據(jù)源中大致分為兩種 一個(gè)mysql服務(wù)器,多個(gè)庫(kù),另外一種就是多個(gè)mysql服務(wù)器多個(gè)庫(kù)表。
對(duì)于以上通用配置如下:以mysql8說(shuō)明
#第一個(gè)庫(kù) 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 #第二個(gè)庫(kù) 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ū)動(dòng)要注意 ,8對(duì)應(yīng) com.mysql.cj.jdbc.Driver 5對(duì)應(yīng)com.mysql.jdbc.Driver
另外 我使用的是jdbc ,所以u(píng)rl就使用 jdbcurl.
以下 配置說(shuō)明:
@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;
這以上 特別說(shuō)明,針對(duì)bean的名字一定要備注上,不然會(huì)默認(rèn)指向。
有一種特殊情景下使用說(shuō)明:
在數(shù)據(jù)同步的時(shí)候,需要進(jìn)行執(zhí)行事務(wù)。但是又是在同一個(gè)服務(wù)器下的兩個(gè)庫(kù),這個(gè)時(shí)候我們實(shí)際執(zhí)行的是mysql庫(kù)的事務(wù)。這個(gè)時(shí)候我們使用一種不區(qū)分庫(kù)而又要執(zhí)行的事務(wù)。以下進(jìn)行說(shuō)明:
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
使用這種,不指定庫(kù),只指定服務(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ù)庫(kù)的b表的sql: a.b
補(bǔ)充一種使用方法 一個(gè)事務(wù)涉及到的多個(gè)sql,這些sql分別對(duì)應(yīng)不同的database(事務(wù)針對(duì)服務(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 多源配置以及特殊場(chǎng)景使用的文章就介紹到這了,更多相關(guān)springboot jdbcTemplate 多源配置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot?配置多個(gè)JdbcTemplate的實(shí)現(xiàn)步驟
- SpringBoot中使用JdbcTemplate訪問(wèn)Oracle數(shù)據(jù)庫(kù)的案例詳解
- SpringBoot2使用JTA組件實(shí)現(xiàn)基于JdbcTemplate多數(shù)據(jù)源事務(wù)管理(親測(cè)好用)
- 詳解SpringBoot中JdbcTemplate的事務(wù)控制
- SpringBoot多數(shù)據(jù)源配置詳細(xì)教程(JdbcTemplate、mybatis)
- SpringBoot jdbctemplate使用方法解析
- springBoot使用JdbcTemplate代碼實(shí)例
- 詳解springboot采用多數(shù)據(jù)源對(duì)JdbcTemplate配置的方法
- SpringBoot使用JdbcTemplate操作數(shù)據(jù)庫(kù)
相關(guān)文章
Java?Spring的核心與設(shè)計(jì)思想你知道嗎
這篇文章主要為大家詳細(xì)介紹了Java?Spring的核心與設(shè)計(jì)思想,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助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,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10
SpringBoot-application.yml多環(huán)境配置詳解
本文主要介紹了SpringBoot-application.yml多環(huán)境配置詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07

