MyBatisPlus又在搞事了!发布神器,一个依赖轻松搞定权限问题

今天介绍一个 MyBatis - Plus 官方发布的神器:mybatis-mate 为 mp 企业级模块 , 支持分库分表 , 数据审计、数据敏感词过滤(AC算法) , 字段加密 , 字典回写(数据绑定) , 数据权限 , 表结构自动生成 SQL 维护等 , 旨在更敏捷优雅处理数据 。
 
1. 主要功能
 

  • 字典绑定
  • 字段加密
  • 数据脱敏
  • 表结构动态维护
  • 数据审计记录
  • 数据范围(数据权限)
  • 数据库分库分表、动态据源、读写分离、数- - 据库健康检查自动切换 。
 
2.使用
 
2.1 依赖导入
 
Spring Boot 引入自动依赖注解包
 
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-mate-starter</artifactId>
<version>1.0.8</version>
</dependency>
 
注解(实体分包使用)
 
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-mate-annotation</artifactId>
<version>1.0.8</version>
</dependency>
 
2.2 字段数据绑定(字典回写)
 
例如 user_sex 类型 sex 字典结果映射到 sexText 属性
 
@FieldDict(type = "user_sex", target = "sexText")
private Integer sex;
private String sexText;
 
实现 IDataDict 接口提供字典数据源 , 注入到 Spring 容器即可 。
 
@Component
public class DataDict implements IDataDict {
/**
* 从数据库或缓存中获取
*/
private Map<String, String> SEX_MAP = new ConcurrentHashMap<String, String>() {{
put("0", "女");
put("1", "男");
}};
@Override
public String getNameByCode(FieldDict fieldDict, String code) {
System.err.println("字段类型:" + fieldDict.type() + " , 编码:" + code);
return SEX_MAP.get(code);
}
}
 
2.3 字段加密
 
属性 @FieldEncrypt 注解即可加密存储 , 会自动解密查询结果 , 支持全局配置加密密钥算法 , 及注解密钥算法 , 可以实现 IEncryptor 注入自定义算法 。
 
@FieldEncrypt(algorithm = Algorithm.PBEWithMD5AndDES)
private String password;
 
2.4 字段脱敏
 
属性 @FieldSensitive 注解即可自动按照预设策略对源数据进行脱敏处理 , 默认 SensitiveType 内置 9 种常用脱敏策略 。
 
例如:中文名、银行卡账号、手机号码等 脱敏策略 。也可以自定义策略如下:
 
@FieldSensitive(type = "testStrategy")
private String username;
@FieldSensitive(type = SensitiveType.mobile)
private String mobile;
 
自定义脱敏策略 testStrategy 添加到默认策略中注入 Spring 容器即可 。
 
@Configuration
public class SensitiveStrategyConfig {
/**
* 注入脱敏策略
*/
@Bean
public ISensitiveStrategy sensitiveStrategy() {
// 自定义 testStrategy 类型脱敏处理
return new SensitiveStrategy().addStrategy("testStrategy", t -> t + "***test***");
}
}
 
例如:文章敏感词过滤
 
/**
* 演示文章敏感词过滤
*/
@RestController
public class ArticleController {
@Autowired
private SensitiveWordsMApper sensitiveWordsMapper;
// 测试访问下面地址观察请求地址、界面返回数据及控制台( 普通参数 )
// 无敏感词 http://localhost:8080/info?content=tom&see=1&age=18
// 英文敏感词 http://localhost:8080/info?content=my%20content%20is%20Tomcat&see=1&age=18
// 汉字敏感词 http://localhost:8080/info?content=%E7%8E%8B%E5%AE%89%E7%9F%B3%E5%94%90%E5%AE%8B%E5%85%AB%E5%A4%A7%E5%AE%B6&see=1
// 多个敏感词 http://localhost:8080/info?content=%E7%8E%8B%E5%AE%89%E7%9F%B3%E6%9C%89%E4%B8%80%E5%8F%AA%E7%8C%ABtomcat%E6%B1%A4%E5%A7%86%E5%87%AF%E7%89%B9&see=1&size=6
// 插入一个字变成非敏感词 http://localhost:8080/info?content=%E7%8E%8B%E7%8C%AB%E5%AE%89%E7%9F%B3%E6%9C%89%E4%B8%80%E5%8F%AA%E7%8C%ABtomcat%E6%B1%A4%E5%A7%86%E5%87%AF%E7%89%B9&see=1&size=6


推荐阅读