Backend Development 17 min read

Introducing MyBatis‑Mate: Enterprise Features for Spring Boot

This article introduces MyBatis‑Mate, an official MyBatis‑Plus extension that provides enterprise‑level capabilities such as sharding, data auditing, field encryption, dictionary binding, data masking, automatic DDL maintenance, dynamic data‑source switching, distributed transaction logging, and fine‑grained data permissions for Spring Boot applications.

Top Architect
Top Architect
Top Architect
Introducing MyBatis‑Mate: Enterprise Features for Spring Boot

Today I introduce MyBatis‑Mate, an official MyBatis‑Plus tool that offers enterprise‑grade modules including sharding, data audit, data‑sensitive word filtering (Aho‑Corasick algorithm), field encryption, dictionary back‑fill, data permissions, and automatic DDL generation, aiming to handle data more agilely and elegantly.

Main Features

Dictionary binding

Field encryption

Data masking

Dynamic table structure maintenance

Data audit records

Data scope (data permissions)

Database sharding, dynamic data source, read‑write separation, automatic health‑check failover

2. Usage

2.1 Dependency Import

Spring Boot automatic dependency annotation package:

<dependency>
  <groupId>com.baomidou</groupId>
  <artifactId>mybatis-mate-starter</artifactId>
  <version>1.0.8</version>
</dependency>

Annotation (entity sub‑package usage):

<dependency>
  <groupId>com.baomidou</groupId>
  <artifactId>mybatis-mate-annotation</artifactId>
  <version>1.0.8</version>
</dependency>

2.2 Field Data Binding (Dictionary Back‑fill)

Example: user_sex type sex dictionary result maps to sexText property.

@FieldDict(type = "user_sex", target = "sexText")
private Integer sex;

private String sexText;

Implementation requires an IDataDict bean:

@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);
    }
}

2.3 Field Encryption

Use @FieldEncrypt to encrypt storage and automatically decrypt on query. Custom algorithms can be injected via IEncryptor :

@FieldEncrypt(algorithm = Algorithm.PBEWithMD5AndDES)
private String password;

2.4 Field Masking

Apply @FieldSensitive to mask data according to predefined strategies (e.g., name, bank card, phone). Custom strategies can be added:

@FieldSensitive(type = "testStrategy")
private String username;

@FieldSensitive(type = SensitiveType.mobile)
private String mobile;

Custom strategy configuration example:

@Configuration
public class SensitiveStrategyConfig {
    @Bean
    public ISensitiveStrategy sensitiveStrategy() {
        return new SensitiveStrategy().addStrategy("testStrategy", t -> t + "***test***");
    }
}

2.5 DDL Automatic Maintenance

Supports MySQL and PostgreSQL for schema upgrades and versioned SQL execution.

@Component
public class PostgresDdl implements IDdl {
    @Override
    public List
getSqlFiles() {
        return Arrays.asList("db/tag-schema.sql", "D:\\db\\tag-data.sql");
    }
}

2.6 Dynamic Multi‑DataSource Switching

Use @Sharding on mapper methods to switch data sources freely:

@Mapper
@Sharding("mysql")
public interface UserMapper extends BaseMapper
{
    @Sharding("postgres")
    Long selectByUsername(String username);
}

A custom sharding strategy can be defined by extending RandomShardingStrategy and overriding determineDatasourceKey .

2.7 Distributed Transaction Logging

Performance interceptor logs SQL execution time, formats SQL, and can enforce a maximum execution time:

@Slf4j
@Component
@Intercepts({
    @Signature(type = StatementHandler.class, method = "query", args = {Statement.class, ResultHandler.class}),
    @Signature(type = StatementHandler.class, method = "update", args = {Statement.class}),
    @Signature(type = StatementHandler.class, method = "batch", args = {Statement.class})
})
public class PerformanceInterceptor implements Interceptor {
    private long maxTime = 0;
    private boolean format = false;
    private boolean writeInLog = false;
    // intercept method implementation omitted for brevity
}

2.8 Data Permissions

Annotate mapper methods with @DataScope and @DataColumn to add fine‑grained SQL where clauses based on custom logic.

@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 provider builds appropriate IN and LIKE expressions for the query.

Further Resources

More usage examples are available at mybatis‑mate‑examples .

Note: The article also contains promotional content unrelated to the technical material, such as offers for ChatGPT services and community invitations.

MyBatisSpringBootDataMaskingDataShardingFieldEncryption
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.