SpringBoot整合Mybatis-plus多数据源
背景说明:在项目中需要访问多个数据库的时候,我们该怎么办呢?下面我介绍一个极其方便的动态切换数据源的方法。
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo-spring-mybatisplus</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo-spring-mybatisplus</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- mybatis-plus 需要引入的包 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<!-- mybatis-plus配置不同数据源 需要引入的包 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
<!-- 阿里云连接池 配置文件在 spring.autoconfigure.exclude 引入 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.8</version>
</dependency>
<!-- 引入lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml
spring:
autoconfigure:
exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
datasource:
dynamic:
primary: mysql1 #设置默认数据库源
strict: false #严格匹配数据源,默认为false,true未匹配到指定数据库时抛异常,false使用默认数据源
datasource:
mysql1:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/demo_mybatisplus?serverTimezone=UTC
username: root
password: root
mysql2:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/demo_mybatisplus2?serverTimezone=UTC
username: root
password: root
com.example.demospringmybatisplus.pojo
@Data
@NoArgsConstructor //生成无参构造函数
@AllArgsConstructor //生成全参数构造函数
@ToString
@TableName(value = "user") //指定查询的表名为user
public class User {
// 以下为 mybatis-plus测试
private Integer id;
private String name;
private String password;
private String sex;
private Date createTime;
private Date updateTime;
}
@Data
@NoArgsConstructor //生成无参构造函数
@AllArgsConstructor //生成全参数构造函数
@ToString
@TableName(value = "user") //指定查询的表名为user
public class User2 {
// 以下为 mybatis-plus测试
private Integer id;
private String name;
private String password;
private String email;
}
com.example.demospringmybatisplus.mapper
@DS("mysql1")
@Repository("UserMapper")
public interface UserMapper extends BaseMapper<User> {
}
@DS("mysql2")
@Repository("User2Mapper")
public interface User2Mapper extends BaseMapper<User2> {
}
项目启动类:DemoSpringMybatisplusApplication
@MapperScan("com.example.demospringmybatisplus.mapper")
@SpringBootApplication
public class DemoSpringMybatisplusApplication {
public static void main(String[] args) {
SpringApplication.run(DemoSpringMybatisplusApplication.class, args);
}
}
项目测试类:DemoSpringMybatisplusApplicationTests
@SpringBootTest
class DemoSpringMybatisplusApplicationTests {
@Autowired
UserMapper userMapper;
@Autowired
User2Mapper user2Mapper;
@Test
void contextLoads() {
User user = userMapper.selectById(1);
System.out.println(user.toString());
User2 user2 = user2Mapper.selectById(1);
System.out.println(user2.toString());
}
}
项目运行截图(DemoSpringMybatisplusApplicationTests启动):
项目已经上传到 GitHub 和 Gitee
GitHub:demo-spring-mybatisplus
Gitee:demo-spring-mybatisplus
版权声明:本文为qq_45072346原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。