Mybatis-plus多数据源 + 数据库连接明文加密

2023-02-12,,,

核心依赖

        <!--mybatis-plus 核心组件-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
<!--mybatis-plus多数据源开发-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
</project>

数据库连接明文加密

1.生成秘钥

2.生成密文

3.密文填写配置文件

 @Test
public void testJiaMi(){
//生成秘钥
String randomKey = AES.generateRandomKey();
String data2 = "jdbc:mysql://localhost:3306/study03?useSSL=false&serverTimezone=UTC";
// 随机密钥加密
String result = AES.encrypt(data2, randomKey);
System.out.println(randomKey+"||"+result);
}

秘钥 在启动参数 (Program arguments) --mpw.key=4b57e89bac82a797

类似如下填写

  datasource:
driver-class-name: com.mysql.jdbc.Driver
url:mpw:dNjT0C8R2vh972whSGklO69WmcQZH494voJk38q/JmdnswjmjMJpyyQvGii+YmLe
username: mpw:3IAJ/lzGvlG5+CSXhe2S2g==
password: mpw:3IAJ/lzGvlG5+CSXhe2S2g==

多数据源配置

server:
port: 8082
spring:
profiles:
active: prod
datasource: #使用了mybatis-plus的加密,密匙传递在启动参数
#mybatis-plus多数据源框架
dynamic:
primary: master #设置默认的数据源或者数据源组,默认值即为master
strict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
datasource:
master:
driver-class-name: com.mysql.jdbc.Driver
url: mpw:dNjT0C8R2vh972whSGklO69WmcQZH494voJk38q/Jmfr7P7L5i6A9aReXemBPXCB6125DwN+5EYRTy/oNOA4AXe2/1XDMnOuQPXNVCJ2C4k=
username: mpw:3IAJ/lzGvlG5+CSXhe2S2g==
password: mpw:3IAJ/lzGvlG5+CSXhe2S2g==
slave_1:
url: mpw:dNjT0C8R2vh972whSGklOyqz7hR9ntRUU58MOiSftOelxMKOCB2rquB+qAZ5KF8IJAV7bVvXjNKSt6qpsnKaV6nz6jV0A27NhwchZZroWgQ=
username: mpw:3IAJ/lzGvlG5+CSXhe2S2g==
password: mpw:3IAJ/lzGvlG5+CSXhe2S2g==
driver-class-name: com.mysql.jdbc.Driver
slave_2:
url: mpw:dNjT0C8R2vh972whSGklO69WmcQZH494voJk38q/JmfDVhJi8IkUYln5KNoyZqSOKZXC0FiZKBGg46AYzRk1lRv6ZdJdSRi/hSoxPoph5VI=
username: mpw:3IAJ/lzGvlG5+CSXhe2S2g==
password: mpw:3IAJ/lzGvlG5+CSXhe2S2g==
driver-class-name: com.mysql.jdbc.Driver
mybatis-plus:
type-aliases-package: com.example.entity #实体类别名
mapper-locations: classpath:mapper/*.xml # 扫描的xml
configuration:
map-underscore-to-camel-case: true #开启驼峰命名
cache-enabled: false
check-config-location: true # 检查xml是否存在
type-enums-package: com.example.enumpackage #通用枚举开启

使用切换数据源

package com.example.controller;

import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.List;
import java.util.Map; /**
* @description: mybatisPlus多数据源
* @author: GuoTong
* @createTime: 2021-08-21 22:53
* @since JDK 1.8 OR 11
**/
@RestController
public class MoreDataSource { @Autowired
private JdbcTemplate jdbcTemplate; @RequestMapping("/datasource01")
@DS("master")
public Object datasource01() {
List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from user");
return list;
} @RequestMapping("/datasource02")
@DS("slave_1")
public Object datasource02() {
List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from user");
return list;
} @RequestMapping("/datasource03")
@DS("slave_2")
public Object datasource03() {
List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from user");
return list;
}
}

效果

http://localhost:8082/datasource01

http://localhost:8082/datasource02

http://localhost:8082/datasource03

结束

Mybatis-plus多数据源 + 数据库连接明文加密的相关教程结束。

《Mybatis-plus多数据源 + 数据库连接明文加密.doc》

下载本文的Word格式文档,以方便收藏与打印。