SpringBoot Mybatis如何配置多數(shù)據(jù)源并分包
看了不少網(wǎng)上關(guān)于多數(shù)據(jù)源的配置,大致可分為兩類,分包方式和通過切面方式;
樣例已上傳至github:https://github.com/dadachao/multids
第一個(gè)子項(xiàng)目ds01即時(shí)使用分包方式完成多數(shù)據(jù)源配置。
總結(jié)項(xiàng)目中出現(xiàn)的問題和解決辦法:
數(shù)據(jù)庫的連接信息:
連接信息是寫在db.properties文件中的:
#數(shù)據(jù)庫ds1
spring.datasource.ds1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.ds1.url=jdbc:mysql://localhost:3306/ds1?serverTimezone=UTC
spring.datasource.ds1.username=root
spring.datasource.ds1.password=root
#數(shù)據(jù)庫ds2
spring.datasource.ds2.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.ds2.url=jdbc:mysql://localhost:3306/ds2?serverTimezone=UTC
spring.datasource.ds2.username=root
spring.datasource.ds2.password=root
這些信息將在配置類DbConfig1.java中引用。一開始我是通過使用注解@ImportResource(...)引進(jìn)db.properties文件,但在運(yùn)行時(shí)報(bào)了org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; 前言中不允許有內(nèi)容的錯(cuò)誤;使用這個(gè)注解也是我瞎猜的。后是通過使用注解@PropertySource(value = "classpath:/db.properties",encoding = "utf-8")解決問題。
其次是關(guān)于在配置類中使用@ConfigurationProperties注解自動(dòng)注入連接信息值(value)的問題:spring.datasource.ds1.url=jdbc:mysql://localhost:3306/ds1?serverTimezone=UTC
注意要使用.url而不是.jdbc-url;
指定數(shù)據(jù)連接池類型DataType:
數(shù)據(jù)源類型可以在配置類生成DataSource的方法中指定:
@Bean(name = "ds1DataSource")
@Primary
@ConfigurationProperties(prefix = "spring.datasource.ds1")
public DataSource getDataSource(){
DataSourceBuilder<?> dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.type(com.alibaba.druid.pool.DruidDataSource.class);
return dataSourceBuilder.build();
}
指定***Mapper.xml文件的路徑掃描問題:(相當(dāng)重要)
使用配置類進(jìn)行數(shù)據(jù)源相關(guān)進(jìn)行配置后,原先在application.yml中配置的相關(guān)參數(shù)就不起作用了(原因未知),原先我是在application.yml中配置了.xml文件的掃描路徑:
mybatis:
mapper-locations: classpath:/mybatis/**/*.xml
type-aliases-package: com.kong.ds01.model
但在運(yùn)行時(shí)報(bào)錯(cuò):Mapper Bound Error(not found);后來通過在配置類中寫入掃描路徑解決:
public final static String mapperXmlLocation = "classpath:mybatis/*/*.xml";
@Bean(name = "ds1SqlSessionFactory")
@Primary
public SqlSessionFactory getSqlSessionFactory(@Qualifier("ds1DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperXmlLocation));
return sqlSessionFactoryBean.getObject();
}
而且通過這種方法表示任意路徑不能使用/**/,要使用/*/,否則識(shí)別不出來又會(huì)報(bào)相同的錯(cuò)誤,這點(diǎn)真是太坑了!
指定執(zhí)行器的類型(Execute.Type):
可以通過在配置類中的sqlSessionTemplate中指定:
@Bean(name = "ds1SqlSessionTemplate")
@Primary
public SqlSessionTemplate getSqlSessionTemplate(@Qualifier("ds1SqlSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory, ExecutorType.BATCH);
}
指定為BATCH類型后在進(jìn)行批量操作時(shí)效率有明顯的提高。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java?Map接口子類HashMap遍歷與LinkedHashMap詳解
這篇文章主要介紹了java?Map接口子類HashMap遍歷與LinkedHashMap詳解,Map接口下的集合與Collection接口下的集合,它們存儲(chǔ)數(shù)據(jù)的形式不同,感興趣的小伙伴可以參考下面文章詳細(xì)內(nèi)容介紹2022-06-06
在mybatis中使用mapper進(jìn)行if條件判斷
這篇文章主要介紹了在mybatis中使用mapper進(jìn)行if條件判斷,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01
SpringMVC數(shù)據(jù)輸出相關(guān)知識(shí)總結(jié)
今天帶大家學(xué)習(xí)SpringMVC的相關(guān)知識(shí),文中對(duì)SpringMVC數(shù)據(jù)輸出作了非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)的小伙伴們很有幫助,需要的朋友可以參考下2021-06-06
idea切換分支的時(shí)候,忽略一些無用的修改設(shè)置
這篇文章主要介紹了idea切換分支的時(shí)候,忽略一些無用的修改操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02
Java如何將字符串String轉(zhuǎn)換為整型Int
這篇文章主要介紹了Java如何將字符串String轉(zhuǎn)換為整型Int,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-08-08
SpringBoot實(shí)現(xiàn)掃碼登錄的項(xiàng)目實(shí)踐
本文主要介紹了SpringBoot實(shí)現(xiàn)掃碼登錄的項(xiàng)目實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
Spring中一個(gè)少見的引介增強(qiáng)IntroductionAdvisor
這篇文章主要為大家介紹了Spring中一個(gè)少見的引介增強(qiáng)IntroductionAdvisor實(shí)戰(zhàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08

