您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關springboot中怎么利用mybatis-plus 配置多數據源,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
1.創建一個空的springboot項目
2.配置pom.xml配置文件,只修改dependencies里面的內容,可以直接替換
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.2.0</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.9</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!-- mybatis-plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.1</version> </dependency> <!-- 多數據源配置 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>dynamic-datasource-spring-boot-starter</artifactId> <version>3.2.0</version> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency></dependencies>
3.配置數據源
spring: datasource:dynamic: primary: master #指定默認數據庫,下面可以配置多個數據庫,不僅僅是兩個,master就是其中一個數據庫的名字,名字對應就可以,自己隨便取 datasource:master: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://數據庫1ip地址:數據庫1端口/數據庫1?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&allowMultiQueries=true username: 數據庫1用戶 password: 數據庫1密碼slave: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://數據庫2ip地址:數據庫2端口/數據庫2?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&allowMultiQueries=true username: 數據庫2用戶 password: 數據庫2密碼
4.新建對應
entity, mapper, service,impl
如果需要調用不用的數據庫源,只需要再impl里面使用注釋@DS("數據庫名稱")即可
5.編寫定時器調用數據查詢,也可以在控制器中調用數據查詢,查詢方式是一樣的
package com.xyz.dsjy.task;import com.xyz.dsjy.entity.Enterprise;import com.xyz.dsjy.entity.FjflCredit;import com.xyz.dsjy.service.EnterpriseService;import com.xyz.dsjy.service.FjflCreditService;import lombok.AllArgsConstructor;import org.springframework.context.annotation.Configuration;import org.springframework.scheduling.annotation.EnableScheduling;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;import java.time.LocalDateTime;@AllArgsConstructor@Component@Configuration //1.主要用于標記配置類,兼備Component的效果。@EnableScheduling // 2.開啟定時任務public class MultithreadScheduleTask {public final FjflCreditService fjflCrediService; public final EnterpriseService enterpriseService; @Scheduled(fixedRate=5000)private void configureTasks() { FjflCredit fs = fjflCrediService.getById(1); System.out.println(fs); System.err.println("執行靜態定時任務時間: " + LocalDateTime.now()); }@Scheduled(fixedRate=6000)private void configureTasks2() { Enterprise et = enterpriseService.getById(80); System.out.println(et); System.err.println("執行靜態定時任務時間2222: " + LocalDateTime.now()); } }
6.修改啟動類 @MapperScan("com.xyz.dsjy.mapper") 添加mapper掃描
package com.xyz.dsjy;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication@MapperScan("com.xyz.dsjy.mapper") //添加啟動掃mapper包下的mapper類public class DsjyApplication {public static void main(String[] args) { SpringApplication.run(DsjyApplication.class, args); } }
7.啟動項目,正確輸出我們需要的結果
遇到的坑:
坑1:
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver clas
因為pom.xml引入了druid和dynamic,而我們使用的是dynamic,所以只需要刪除druid引入即可。
坑2:
2021-04-08 15:52:53.242 INFO 20108 --- [ main] com.zaxxer.hikari.HikariDataSource : master - Starting...
2021-04-08 15:52:54.125 INFO 20108 --- [ main] com.zaxxer.hikari.HikariDataSource : master - Start completed.
2021-04-08 15:52:54.126 INFO 20108 --- [ main] com.zaxxer.hikari.HikariDataSource : slave - Starting...
2021-04-08 15:52:54.942 INFO 20108 --- [ main] com.zaxxer.hikari.HikariDataSource : slave - Start completed.
2021-04-08 15:52:54.942 INFO 20108 --- [ main] c.b.d.d.DynamicRoutingDataSource : dynamic-datasource - load a datasource named [slave] success
2021-04-08 15:52:54.943 INFO 20108 --- [ main] c.b.d.d.DynamicRoutingDataSource : dynamic-datasource - load a datasource named [master] success
2021-04-08 15:52:54.943 INFO 20108 --- [ main] c.b.d.d.DynamicRoutingDataSource : dynamic-datasource initial loaded [2] datasource,primary
........
2021-04-08 15:52:54.989 INFO 20108 --- [ main] com.zaxxer.hikari.HikariDataSource : slave - Shutdown initiated...
2021-04-08 15:52:54.996 INFO 20108 --- [ main] com.zaxxer.hikari.HikariDataSource : slave - Shutdown completed.
2021-04-08 15:52:54.996 INFO 20108 --- [ main] com.zaxxer.hikari.HikariDataSource : master - Shutdown initiated...
2021-04-08 15:52:55.164 INFO 20108 --- [ main] com.zaxxer.hikari.HikariDataSource : master - Shutdown completed.
2021-04-08 15:52:55.165 INFO 20108 --- [ main] c.b.d.d.DynamicRoutingDataSource : dynamic-datasource all closed success,bye
可以看到啟動過程中我們配置的多數據庫源Start completed.之后遇到異常之后又被shutdown了。
因為引入了多個Mybatis 的jar包引起的,檢查bom.xml文件是否引入了mybatis和mybatis-plus兩個依賴,如果是去掉mybatis即可。
上述就是小編為大家分享的springboot中怎么利用mybatis-plus 配置多數據源了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。