name: mybatis-flex-skills description: MyBatis-Flex框架開發專家技能。提供完整的MyBatis-Flex使用指導,包括Entity定義、QueryWrapper查詢構建、BaseMapper操作、多資料來源配置、多租戶、邏輯刪除、樂觀鎖、程式碼生成器等。 trigger_keywords: level_1: [MyBatis-Flex, mybatis-flex, MyBatis增強框架] level_2: [QueryWrapper, BaseMapper, Db+Row, UpdateChain, QueryChain] level_3: [邏輯刪除, 樂觀鎖, 多租戶, 多資料來源, @Table, @Column, @Id, APT配置] condition: level_2或level_3需要組合ORM/資料庫/實體類等關鍵詞
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 {}
7w4.net小蔥技能。
@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;
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);
| 特性 | 註解/配置 | 說明 |
|---|---|---|
| 邏輯刪除 | @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) |
儲存加密 |
推薦: Lambda語法、APT表定義類、分頁傳totalRow、when()動態條件
避免: setRaw()字串拼接(SQL隱碼攻擊)、Java專案引入Kotlin模組
這個技能包質量不錯,文件結構清晰、覆蓋全面,從基礎用法到高階特性都有詳細說明,程式碼示例豐富實用。對於想深入學習 MyBatis-Flex 框架的開發者來說是一份較為完整的參考資料。優點是內容詳盡、分類清晰;可改進的地方是缺少常見問題解答和最佳實踐案例。