Springboot集成mybatis實(shí)現(xiàn)多數(shù)據(jù)源配置詳解流程
新建springboot工程,引入web、mysql、mybatis依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
在application.properties中配置多數(shù)據(jù)源
spring.datasource.primary.jdbc-url=jdbc:mysql://localhost:3306/ds0
spring.datasource.primary.username=root
spring.datasource.primary.password=root
spring.datasource.primary.driver-class-name=com.mysql.cj.jdbc.Driverspring.datasource.secondary.jdbc-url=jdbc:mysql://localhost:3306/ds1
spring.datasource.secondary.username=root
spring.datasource.secondary.password=root
spring.datasource.secondary.driver-class-name=com.mysql.cj.jdbc.Driver
多數(shù)據(jù)源配置與單個(gè)數(shù)據(jù)源配置不同點(diǎn)在于,spring.datasource之后多了一個(gè)數(shù)據(jù)源名稱primary/secondary用來區(qū)分不同的數(shù)據(jù)源;
初始化數(shù)據(jù)源
新建一個(gè)配置類,用來加載多個(gè)數(shù)據(jù)源完成初始化。
@Configuration
public class DataSourceConfiguration {
@Primary
@Bean
@ConfigurationProperties(prefix = "spring.datasource.primary")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix = "spring.datasource.secondary")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
}通過@ConfigurationProperties就可以知道這兩個(gè)數(shù)據(jù)源分別加載了spring.datasource.primary.*和spring.datasource.secondary.*的配置。@Primary注解指定了主數(shù)據(jù)源,當(dāng)不指定數(shù)據(jù)源時(shí),就會(huì)使用該主數(shù)據(jù)源。
mybatis配置
@Configuration
@MapperScan(
basePackages = "com*.primary",
sqlSessionFactoryRef = "sqlSessionFactoryPrimary",
sqlSessionTemplateRef = "sqlSessionTemplatePrimary")
public class PrimaryConfig {
private DataSource primaryDataSource;
public PrimaryConfig(@Qualifier("primaryDataSource") DataSource primaryDataSource) {
this.primaryDataSource = primaryDataSource;
}
@Bean
public SqlSessionFactory sqlSessionFactoryPrimary() throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(primaryDataSource);
return bean.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionTemplatePrimary() throws Exception {
return new SqlSessionTemplate(sqlSessionFactoryPrimary());
}
}@Configuration
@MapperScan(
basePackages = "com.*.secondary",
sqlSessionFactoryRef = "sqlSessionFactorySecondary",
sqlSessionTemplateRef = "sqlSessionTemplateSecondary")
public class SecondaryConfig {
private DataSource secondaryDataSource;
public SecondaryConfig(@Qualifier("secondaryDataSource") DataSource secondaryDataSource) {
this.secondaryDataSource = secondaryDataSource;
}
@Bean
public SqlSessionFactory sqlSessionFactorySecondary() throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(secondaryDataSource);
return bean.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionTemplateSecondary() throws Exception {
return new SqlSessionTemplate(sqlSessionFactorySecondary());
}
}配置類上使用@MapperScan注解來指定當(dāng)前數(shù)據(jù)源下定義的實(shí)體和mapper的包路徑,還注入了sqlSessionFactory和sqlSessionTemplate,通過@Qualifier注解指定了對(duì)應(yīng)的數(shù)據(jù)源,其名字對(duì)應(yīng)在DataSourceConfiguration配置類中的數(shù)據(jù)源定義的函數(shù)名。
各個(gè)對(duì)數(shù)據(jù)源對(duì)應(yīng)路徑下的實(shí)體和mapper
@Data
@NoArgsConstructor
public class UserPrimary {
private Long id;
private String user_name;
private Integer age;
public UserPrimary(String name, Integer age) {
this.user_name = name;
this.age = age;
}
}
public interface UserMapperPrimary {
@Select("SELECT * FROM USER_0 WHERE USER_NAME = #{name}")
UserPrimary findByName(@Param("name") String name);
@Insert("INSERT INTO USER_0 (USER_NAME, AGE) VALUES(#{name}, #{age})")
int insert(@Param("name") String name, @Param("age") Integer age);
@Delete("DELETE FROM USER_0")
int deleteAll();
}@Data
@NoArgsConstructor
public class UserSecondary {
private Long id;
private String user_name;
private Integer age;
public UserSecondary(String name, Integer age) {
this.user_name = name;
this.age = age;
}
}
public interface UserMapperSecondary {
@Select("SELECT * FROM USER_1 WHERE USER_NAME = #{name}")
UserSecondary findByName(@Param("name") String name);
@Insert("INSERT INTO USER_1 (USER_NAME, AGE) VALUES(#{name}, #{age})")
int insert(@Param("name") String name, @Param("age") Integer age);
@Delete("DELETE FROM USER_1")
int deleteAll();
}測(cè)試
@Test
public void test() {
// 往Primary數(shù)據(jù)源插入一條數(shù)據(jù)
userMapperPrimary.insert("caocao", 20);
// 從Primary數(shù)據(jù)源查詢剛才插入的數(shù)據(jù),配置正確就可以查詢到
UserPrimary userPrimary = userMapperPrimary.findByName("caocao");
Assert.assertEquals(20, userPrimary.getAge().intValue());
// 從Secondary數(shù)據(jù)源查詢剛才插入的數(shù)據(jù),配置正確應(yīng)該是查詢不到的
UserSecondary userSecondary = userMapperSecondary.findByName("caocao");
Assert.assertNull(userSecondary);
// 往Secondary數(shù)據(jù)源插入一條數(shù)據(jù)
userMapperSecondary.insert("sunquan", 21);
// 從Primary數(shù)據(jù)源查詢剛才插入的數(shù)據(jù),配置正確應(yīng)該是查詢不到的
userPrimary = userMapperPrimary.findByName("sunquan");
Assert.assertNull(userPrimary);
// 從Secondary數(shù)據(jù)源查詢剛才插入的數(shù)據(jù),配置正確就可以查詢到
userSecondary = userMapperSecondary.findByName("sunquan");
Assert.assertEquals(21, userSecondary.getAge().intValue());
}到此這篇關(guān)于Springboot集成mybatis實(shí)現(xiàn)多數(shù)據(jù)源配置詳解流程的文章就介紹到這了,更多相關(guān)Springboot多數(shù)據(jù)源配置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java微信授權(quán)登陸的實(shí)現(xiàn)示例
微信授權(quán)登錄,官方文檔寫的比較簡(jiǎn)潔。所以對(duì)于會(huì)的人一目了然,而對(duì)于新手剛?cè)腴T的人來說是舉步維艱。本文詳細(xì)的介紹了Java微信授權(quán)登陸的實(shí)現(xiàn)示例,感興趣的朋友可以了解一下2021-06-06
Java中SpringSecurity密碼錯(cuò)誤5次鎖定用戶的實(shí)現(xiàn)方法
這篇文章主要介紹了Java中SpringSecurity密碼錯(cuò)誤5次鎖定用戶的實(shí)現(xiàn)方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03
Java實(shí)現(xiàn)在不同線程中運(yùn)行的代碼實(shí)例
這篇文章主要介紹了Java實(shí)現(xiàn)在不同線程中運(yùn)行的代碼,結(jié)合具體實(shí)例形式分析了java多線程操作的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-04-04
解決idea爆紅 cant resolve symbol String的問題解析
連著出差幾個(gè)禮拜沒有使用idea開發(fā)工具,突然一天打開電腦發(fā)現(xiàn)idea里的代碼全部爆紅,懵逼不如所措,很多朋友建議我按住Alt+回車設(shè)置jdk就能解決,但是仍然報(bào)錯(cuò),經(jīng)過幾個(gè)小時(shí)的倒騰最終解決,遇到此問題的朋友參考下本文吧2021-06-06
Java打亂ArrayList生成一個(gè)隨機(jī)序列列表
有時(shí)候會(huì)需要將一個(gè)ArrayList或者數(shù)組中的數(shù)字打亂,方便后續(xù)使用,比如隨機(jī)出題、答案選項(xiàng)打亂、連線題打亂、抽獎(jiǎng)號(hào)碼打亂等等,把我自己寫的一段代碼貼出來分享給大家。2016-08-08
巧用FutureTask 線程池輕松解決接口超時(shí)問題
這篇文章主要為大家介紹了使用FutureTask結(jié)合線程池輕松解決接口超時(shí)問題的巧妙用法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(61)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你2021-08-08

