MyBatis-Flex 是一个优雅的 MyBatis 增强框架,具有轻量、灵活、强大的特点。
| 场景 | 文档 | 核心关键词 |
|---|---|---|
| ------ | ------ | ----------- |
| Entity定义、注解配置 | entity-annotations.md | @Table, @Column, @Id, 监听器 |
| BaseMapper、Db+Row、链式操作 | db-row-and-basemapper.md | insert, update, delete, QueryChain |
| QueryWrapper高级用法 | querywrapper-advanced.md | select, where, join, 子查询, CTE |
| 关联查询 | relations-query.md | @RelationOneToOne, Join Query |
| Service层 | service-layer.md | save, remove, list, page |
| 逻辑删除、乐观锁、多数据源、多租户 | advanced-features.md | isLogicDelete, version, DataSourceKey |
| 代码生成器、APT配置 | codegen-and-apt.md | Generator, processor |
| Kotlin开发、DSL查询 | kotlin-guide.md | Kotlin, KSP, 中缀表达式 |
| 框架对比、SQL日志、缓存 | other-features.md | p6spy, 缓存, 枚举 |
> 各模块详细文档见 references/ 目录
<!-- 2.x: starter, 3.x: boot3-starter, 4.x: boot4-starter + jdbc -->
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-spring-boot-starter</artifactId>
<version>1.11.7</version>
</dependency>
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-processor</artifactId>
<version>1.11.7</version>
<scope>provided</scope>
</dependency>
@SpringBootApplication
@MapperScan("com.your.package.mapper")
public class Application {}
> 详见 codegen-and-apt.md
@Table(value = "tb_account", dataSource = "ds2")
public class Account {
@Id(keyType = KeyType.Auto)
private Long id;
@Column(value = "user_name")
private String userName;
@Column(isLogicDelete = true)
private Boolean isDelete;
@Column(version = true)
private Integer version;
@Column(tenantId = true)
private Long tenantId;
@ColumnMask(Masks.MOBILE_PHONE)
private String phone;
}
import static com.your.package.entity.table.AccountTableDef.ACCOUNT;
QueryWrapper query = QueryWrapper.create()
.select(ACCOUNT.ID, ACCOUNT.USER_NAME)
.from(ACCOUNT)
.where(ACCOUNT.AGE.ge(18))
.and(ACCOUNT.USER_NAME.like("张"))
.orderBy(ACCOUNT.ID.desc());
// 动态条件
query.where(ACCOUNT.AGE.ge(18).when(age != null))
.and(ACCOUNT.USER_NAME.like(name, If::hasText));
public interface AccountMapper extends BaseMapper<Account> {}
accountMapper.insert(account);
accountMapper.selectOneById(1L);
accountMapper.selectListByQuery(query);
accountMapper.paginate(1, 10, query);
accountMapper.deleteById(1L);
// 部分字段更新
Account entity = UpdateEntity.of(Account.class, 100L);
entity.setAge(20);
accountMapper.update(entity);
@RelationOneToOne(selfField = "id", targetField = "accountId")
private IDCard idCard;
@RelationOneToMany(selfField = "id", targetField = "accountId")
private List<Book> books;
@RelationManyToMany(joinTable = "tb_role_mapping",
joinSelfColumn = "account_id", joinTargetColumn = "role_id")
private List<Role> roles;
> 详见 relations-query.md
public interface IAccountService extends IService<Account> {}
@Component
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account>
implements IAccountService {}
accountService.save(account);
accountService.removeById(1L);
accountService.list(ACCOUNT.AGE.ge(18));
accountService.page(new Page<>(1, 10), query);
> 详见 service-layer.md
| 特性 | 注解/配置 | 说明 |
|---|---|---|
| ------ | ---------- | ------ |
| 逻辑删除 | @Column(isLogicDelete=true) | Integer/Boolean/DateTime |
| 乐观锁 | @Column(version=true) | 版本+1并检测 |
| 多租户 | @Column(tenantId=true) | 自动租户条件 |
| 数据脱敏 | @ColumnMask(Masks.MOBILE_PHONE) | 查询自动脱敏 |
| 多数据源 | @Table(dataSource="ds2") | 或 DataSourceKey.use() |
| 字段加密 | @Column(typeHandler=AesTypeHandler.class) | 存储加密 |
> 详见 advanced-features.md
推荐: Lambda语法、APT表定义类、分页传totalRow、when()动态条件
避免: setRaw()字符串拼接(SQL注入)、Java项目引入Kotlin模块
共 2 个版本