1, open-source project Address: MyBatis Plus & Dynamic Datasource
Maven configuration:
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.0.6</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>dynamic-datasource-spring-boot-starter</artifactId> <version>2.4.2</version> </dependency>
Profile Description:
Server: Port: 2080 logging: Level: cn.mrxionge.bootdemo: Debug org.springframework.web: Debug org.springframework.data: Debug the mybatis - PLUS: Executor - of the type: the Simple the Spring: the DataSource: # Configure connection pool hikari hikari: Minimum -idle: . 4 maximum -pool-size: 16 Connection -timeout: 10000 IDLE -timeout: 30000 Connection -init-SQL: SET names utf8mb4 # dynamic data source configuration dynamic: #主数据源 primary: a01 datasource: #数据源a01 a01: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/a01?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai username: root password: 123456 #数据源a02 a02: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/a02?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai username: root password: 123456 #数据源a03 a03: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/a03?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai username: root password: 123456
Test: New mapper three different files, each file corresponding to a data mapper source, by calling the different mapper file for query data from different databases
Mapper example:
@DS(value = "a01") @Mapper public interface MapperA01 extends BaseMapper<UserInfo> { @Select("SELECT * FROM user_info") List<UserInfo> getAllUser(); }
@DS(value = "a02") @Mapper public interface MapperA02 extends BaseMapper<UserInfo> { @Select("SELECT * FROM user_info") List<UserInfo> getAllUser(); }
@DS(value = "a03") @Mapper public interface MapperA03 extends BaseMapper<UserInfo> { @Select("SELECT * FROM user_info") List<UserInfo> getAllUser(); }
Service Example:
@Service public class the AppService { @Autowired Private MapperA01 mapperA01; @Autowired Private MapperA02 mapperA02; @Autowired Private MapperA03 mapperA03; / * * * A01 query data repository * * @return list of user information * / List <the UserInfo> getUser01 () { return mapperA01.getAllUser (); } / * * * A02 query data repository * * @return list of user information * / list <the UserInfo> getUser02 () { returnmapperA02.getAllUser (); } / * * * A03 query data repository * * @return list of user information * / List <the UserInfo> getUser03 () { return mapperA03.getAllUser (); } }
Controller Example:
@RestController public class AppHandler { @Autowired private AppService service; @GetMapping(path = "/get01") public Mono get01() { //查询A01数据 return Mono.fromSupplier(service::getUser01); } @GetMapping(path = "/get02") public Mono get02() { //查询A02数据 return Mono.fromSupplier(service::getUser02); } @GetMapping(path = "/get03" ) Public Mono get03 () { // query data A03 return Mono.fromSupplier (:: getUser03-Service); } }
Disclaimer: This article is the original article CSDN bloggers "MrXionGe", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/qq_31226223/article/details/85322561