這篇文章主要介紹“springboot中如何利用mybatis-plus配置多數(shù)據(jù)源”的相關(guān)知識,小編通過實(shí)際案例向大家展示操作過程,操作方法簡單快捷,實(shí)用性強(qiáng),希望這篇“springboot中如何利用mybatis-plus配置多數(shù)據(jù)源”文章能幫助大家解決問題。
在馬鞍山等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì) 網(wǎng)站設(shè)計(jì)制作按需規(guī)劃網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站建設(shè),營銷型網(wǎng)站建設(shè),外貿(mào)網(wǎng)站建設(shè),馬鞍山網(wǎng)站建設(shè)費(fèi)用合理。1.創(chuàng)建一個(gè)空的springboot項(xiàng)目
2.配置pom.xml配置文件,只修改dependencies里面的內(nèi)容,可以直接替換
org.springframework.boot spring-boot-starter org.apache.httpcomponents httpclient cn.hutool hutool-all 5.2.0 com.alibaba fastjson 1.2.9 org.projectlombok lombok true com.baomidou mybatis-plus-boot-starter 3.4.1 com.baomidou dynamic-datasource-spring-boot-starter 3.2.0 mysql mysql-connector-java runtime
3.配置數(shù)據(jù)源
spring: datasource:dynamic: primary: master #指定默認(rèn)數(shù)據(jù)庫,下面可以配置多個(gè)數(shù)據(jù)庫,不僅僅是兩個(gè),master就是其中一個(gè)數(shù)據(jù)庫的名字,名字對應(yīng)就可以,自己隨便取 datasource:master: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://數(shù)據(jù)庫1ip地址:數(shù)據(jù)庫1端口/數(shù)據(jù)庫1?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&allowMultiQueries=true username: 數(shù)據(jù)庫1用戶 password: 數(shù)據(jù)庫1密碼slave: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://數(shù)據(jù)庫2ip地址:數(shù)據(jù)庫2端口/數(shù)據(jù)庫2?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&allowMultiQueries=true username: 數(shù)據(jù)庫2用戶 password: 數(shù)據(jù)庫2密碼
4.新建對應(yīng)
entity, mapper, service,impl
如果需要調(diào)用不用的數(shù)據(jù)庫源,只需要再impl里面使用注釋@DS("數(shù)據(jù)庫名稱")即可
5.編寫定時(shí)器調(diào)用數(shù)據(jù)查詢,也可以在控制器中調(diào)用數(shù)據(jù)查詢,查詢方式是一樣的
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.主要用于標(biāo)記配置類,兼?zhèn)銫omponent的效果。@EnableScheduling // 2.開啟定時(shí)任務(wù)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("執(zhí)行靜態(tài)定時(shí)任務(wù)時(shí)間: " + LocalDateTime.now()); }@Scheduled(fixedRate=6000)private void configureTasks2() { Enterprise et = enterpriseService.getById(80); System.out.println(et); System.err.println("執(zhí)行靜態(tài)定時(shí)任務(wù)時(shí)間2222: " + LocalDateTime.now()); } }
6.修改啟動(dòng)類 @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") //添加啟動(dòng)掃mapper包下的mapper類public class DsjyApplication {public static void main(String[] args) { SpringApplication.run(DsjyApplication.class, args); } }
7.啟動(dòng)項(xiàng)目,正確輸出我們需要的結(jié)果
遇到的坑:
坑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
因?yàn)閜om.xml引入了druid和dynamic,而我們使用的是dynamic,所以只需要?jiǎng)h除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
可以看到啟動(dòng)過程中我們配置的多數(shù)據(jù)庫源Start completed.之后遇到異常之后又被shutdown了。
因?yàn)橐肓硕鄠€(gè)Mybatis 的jar包引起的,檢查bom.xml文件是否引入了mybatis和mybatis-plus兩個(gè)依賴,如果是去掉mybatis即可。
關(guān)于“springboot中如何利用mybatis-plus配置多數(shù)據(jù)源”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識點(diǎn)。