前言
本文主要介绍使用spring boot 配置多个数据库,即动态数据库
开始搭建
首先创建一个SpringWeb项目——dynamicdb(spring-boot2.5.7)
然后引入相关依赖lombok、swagger2、mybatis-plus,如下:
代码语言:javascript复制<?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.5.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>dynamicdb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dynamicdb</name>
<description>dynamicdb</description>
<properties>
<java.version>8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.28</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!-- mybatis-plus 依赖 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- sqlSessionTemplate 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- spring-boot dynamic 动态数据库依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>2.5.6</version>
</dependency>
<!-- druid 动态简易使用依赖 @DS("db1")-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.20</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
然后在包dynamicdb下面创建controller、mapper、dbmodel, 然后在resource下面创建mapper文件夹及问题,如下图:
然后配置application,启用swagger2和添加mapper扫描,如下:
代码语言:javascript复制@EnableSwagger2
@Log4j2
@SpringBootApplication
@MapperScan("com.example.dynamicdb.mapper")
public class DynamicdbApplication {
public static void main(String[] args) {
SpringApplication.run(DynamicdbApplication.class, args);
}
}
然后编辑resources下的application.yml,如下:
代码语言:javascript复制server:
port: 8082
servlet:
context-path: /api
spring:
servlet:
multipart:
max-file-size: 50MB
max-request-size: 50MB
datasource:
dynamic: #使用动态数据库配置
primary: db1 # 配置默认数据库
datasource:
db1: # 数据源1
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost/db1
username: root
password: 1qaz!QAZ
db2: # 数据源2
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost/db2
username: root
password: 1qaz!QAZ
druid:
initial-size: 1
max-active: 20
min-idle: 1
max-wait: 60000
autoconfigure:
exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure # 去除druid配置
mybatis-plus:
mapper-locations: classpath:mapper/*.xml #指定resources下的mapper文件夹下是存储映射XML的文件夹
然后创建SqlSessionController,使用SqlSession来加载Mapper,代码如下:
代码语言:javascript复制@RestController
public class SqlSessionController {
@Autowired
private SqlSession sqlSession;
@GetMapping(value = "/SqlSession/normalSql")
@ResponseBody
@ApiOperation(value = "默认查询,从主数据库查询", notes = "当前配置的主数据是db1-primary: db1")
public List<user> normalSql() {
//读取第一个数据库的值
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<user> users = mapper.test();
return users;
}
@GetMapping(value = "/SqlSession/data_from_db1")
@ResponseBody
@DS("db1")
@ApiOperation(value = "从db1数据库查询数据", notes = "查询db1")
public List<user> data_from_db1() {
//读取第一个数据库的值
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<user> users = mapper.test();
return users;
}
@GetMapping(value = "/SqlSession/data_from_db2")
@ResponseBody
@DS("db2")
@ApiOperation(value = "从db2数据库查询数据", notes = "查询db2")
public List<user> data_from_db2(Integer id) {
//读取第二个数据库的值
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<user> users = mapper.test();
return users;
}
}
代码中使用 @DS("db1")来指定该接口内使用的数据库。
再创建一个MapperController,测试使用Mapper直接访问数据库,如下
代码语言:javascript复制@RestController
public class MapperController {
@Autowired
private UserMapper userMapper;
@GetMapping(value = "/Mapper/normalSql")
@ResponseBody
@ApiOperation(value = "使用Mapper查询,从主数据库查询", notes = "当前配置的主数据是db1-primary: db1")
public List<user> normalSql() {
//读取第一个数据库的值
List<user> users = userMapper.test();
return users;
}
}
----------------------------------------------------------------------------------------------------
到此,Spring配置动态数据库就已经介绍完了。
代码已经传到Github上了,欢迎大家下载。
Github地址:https://github.com/kiba518/dynamicdb