In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how to configure multiple data sources with mybatis-plus in springboot". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope that this article "how to configure multiple data sources with mybatis-plus in springboot" can help you solve the problem.
1. Create an empty springboot project
two。 Configure the pom.xml configuration file. Only modify the contents of the dependencies, and you can directly replace it.
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. Configure the data source
Spring: datasource:dynamic: primary: master # specifies the default database. You can configure multiple databases below, not just two. Master is the name of one of the databases, and the name corresponds to it. Take datasource:master: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql:// database 1ip address: database 1 port / database 1?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&allowMultiQueries=true username: database 1 user password: database 1 password slave: driver-class-name: Com.mysql.cj.jdbc.Driver url: jdbc:mysql:// database 2ip address: database 2 port / database 2?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&allowMultiQueries=true username: database 2 user password: database 2 password
4. Create a new correspondence
Entity, mapper, service,impl
If you need to call an unused database source, you only need to use the annotation @ DS ("database name") in impl
5. Write a timer to call the data query, or you can call the data query in the controller in the same way
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. It is mainly used for marking configuration classes and has the effect of Component. @ EnableScheduling / / 2. Open timed task 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 ("time to execute static scheduled task:" + LocalDateTime.now ());} @ Scheduled (fixedRate=6000) private void configureTasks2 () {Enterprise et = enterpriseService.getById (80) System.out.println (et); System.err.println ("time to execute static scheduled tasks 2222:" + LocalDateTime.now ());}}
6. Modify the startup class @ MapperScan ("com.xyz.dsjy.mapper") to add mapper scan
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") / / add the mapper class public class DsjyApplication {public static void main (String [] args) {SpringApplication.run (DsjyApplication.class, args);} under the boot scan mapper package
7. Start the project and correctly output the results we need
Pit encountered:
Pit 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
Because pom.xml introduces druid and dynamic, and we use dynamic, we just need to remove the druid introduction.
Pit 2:
2021-04-08 15 com.zaxxer.hikari.HikariDataSource 5222 INFO 20108-[main] com.zaxxer.hikari.HikariDataSource: master-Starting...
2021-04-08 15 com.zaxxer.hikari.HikariDataSource 52V 54.125 INFO 20108-[main] com.zaxxer.hikari.HikariDataSource: master-Start completed.
2021-04-08 15 com.zaxxer.hikari.HikariDataSource 52V 54.126 INFO 20108-[main] com.zaxxer.hikari.HikariDataSource: slave-Starting...
2021-04-08 15 com.zaxxer.hikari.HikariDataSource 52 INFO 20108-[main] com.zaxxer.hikari.HikariDataSource: slave-Start completed.
2021-04-08 15 INFO 52V 54.942 INFO 20108-[main] c.b.d.d.DynamicRoutingDataSource: dynamic-datasource-load a datasource named [slave] success
2021-04-08 15 INFO 52V 54.943 INFO 20108-[main] c.b.d.d.DynamicRoutingDataSource: dynamic-datasource-load a datasource named [master] success
2021-04-08 15 c.b.d.d.DynamicRoutingDataSource 52V 54.943 INFO 20108-[main] c.b.d.d.DynamicRoutingDataSource: dynamic-datasource initial loaded [2] datasource,primary
.
2021-04-08 15 com.zaxxer.hikari.HikariDataSource 52V 54.989 INFO 20108-[main] com.zaxxer.hikari.HikariDataSource: slave-Shutdown initiated...
2021-04-08 15 com.zaxxer.hikari.HikariDataSource 52 INFO 20108-[main] com.zaxxer.hikari.HikariDataSource: slave-Shutdown completed.
2021-04-08 15 com.zaxxer.hikari.HikariDataSource 52V 54.996 INFO 20108-[main] com.zaxxer.hikari.HikariDataSource: master-Shutdown initiated...
2021-04-08 15 com.zaxxer.hikari.HikariDataSource 52V 55.164 INFO 20108-[main] com.zaxxer.hikari.HikariDataSource: master-Shutdown completed.
2021-04-08 15 c.b.d.d.DynamicRoutingDataSource 52V 52.165 INFO 20108-[main] c.b.d.d.DynamicRoutingDataSource: dynamic-datasource all closed success,bye
You can see the multiple database source Start completed. Exe that we configured during startup. After encountering an exception, I was shutdown again.
Because of the introduction of multiple Mybatis jar packages, check whether the bom.xml file introduces mybatis and mybatis-plus dependencies, if it is to remove mybatis.
This is the end of the introduction on "how to configure multiple data sources with mybatis-plus in springboot". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.