Introducing MyBatis-Mate: Enterprise‑Level Features for MyBatis‑Plus
This article introduces MyBatis‑Mate, an official MyBatis‑Plus extension that provides enterprise‑grade capabilities such as dictionary binding, field encryption, data masking, dynamic table maintenance, audit logging, sharding, multi‑datasource switching, and customizable data‑scope handling, all illustrated with code examples.
MyBatis‑Mate is an official MyBatis‑Plus extension that offers a suite of enterprise‑grade features for handling data more efficiently and securely.
Key capabilities include dictionary binding, field encryption, data masking, dynamic DDL maintenance, audit logging, data permissions, sharding, and multi‑datasource management.
Main Features
Dictionary binding
Field encryption
Data masking
Dynamic table structure maintenance
Audit record logging
Data scope (permissions)
Database sharding, dynamic datasource, read/write separation, health‑check auto‑switch
Usage
Import the starter dependency in your Spring Boot project:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-mate-starter</artifactId>
<version>1.0.8</version>
</dependency>Import the annotation package for entity partitioning:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-mate-annotation</artifactId>
<version>1.0.8</version>
</dependency>Dictionary Binding (FieldDict)
Example of mapping a user_sex dictionary to a sexText property:
@FieldDict(type = "user_sex", target = "sexText")
private Integer sex;
private String sexText;Implement IDataDict to provide the dictionary data source:
@Component
public class DataDict implements IDataDict {
private Map
SEX_MAP = new ConcurrentHashMap
() {{
put("0", "女");
put("1", "男");
}};
@Override
public String getNameByCode(FieldDict fieldDict, String code) {
System.err.println("字段类型:" + fieldDict.type() + ",编码:" + code);
return SEX_MAP.get(code);
}
}Field Encryption
Use @FieldEncrypt to encrypt a field automatically:
@FieldEncrypt(algorithm = Algorithm.PBEWithMD5AndDES)
private String password;Data Masking
Apply @FieldSensitive with built‑in or custom strategies to mask sensitive data:
@FieldSensitive(type = "testStrategy")
private String username;
@FieldSensitive(type = SensitiveType.mobile)
private String mobile;Custom strategy configuration:
@Configuration
public class SensitiveStrategyConfig {
@Bean
public ISensitiveStrategy sensitiveStrategy() {
return new SensitiveStrategy().addStrategy("testStrategy", t -> t + "***test***");
}
}DDL Automatic Maintenance
Implement IDdl to supply SQL files for schema creation, supporting MySQL and PostgreSQL:
@Component
public class PostgresDdl implements IDdl {
@Override
public List
getSqlFiles() {
return Arrays.asList("db/tag-schema.sql", "D:\\db\\tag-data.sql");
}
}Dynamic Datasource Switching
Use @Sharding on mappers to switch between datasource groups:
@Mapper
@Sharding("mysql")
public interface UserMapper extends BaseMapper
{
@Sharding("postgres")
Long selectByUsername(String username);
}Data Permission (DataScope)
Annotate mapper methods with @DataScope and @DataColumn to enforce row‑level permissions:
@DataScope(type = "test", value = {
@DataColumn(alias = "u", name = "department_id"),
@DataColumn(alias = "u", name = "mobile")
})
@Select("select u.* from user u")
List
selectTestList(IPage
page, Long id, @Param("name") String username);The generated SQL will include appropriate IN and LIKE clauses based on the defined scope.
For more examples, visit the official repository: mybatis-mate-example .
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.