feat:增强kotlin的BaseMapper语法糖以及dsl写法#6880
Open
huicunjun wants to merge 3 commits into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
为 MyBatis-Plus 的 Kotlin 扩展增加 BaseMapper 的语法糖与 DSL 写法(基于 KtQueryWrapper / KtUpdateWrapper),通过 KProperty1 的中缀/扩展属性形式提升查询条件表达的可读性,同时补齐 KClass 构造入口以配合 reified 泛型构建 Wrapper。
Changes:
- 新增
CompareDsl/FuncDsl,并让AbstractKtWrapper/KtQueryChainWrapper实现,从而在 Kotlin lambda receiver 中支持User::id eq id、User::id.isNull等写法 - 新增
BaseMapperKotlin 扩展与 Wrapper 构建函数(buildKtQueryWrapper/buildKtUpdateWrapper、BaseMapper.selectOne { ... }等) - 为
KtQueryWrapper/KtUpdateWrapper增加KClass<T>构造函数,便于T::class直接传入
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 16 comments.
Show a summary per file
| File | Description |
|---|---|
| mybatis-plus-extension/src/main/kotlin/com/baomidou/mybatisplus/extension/kotlin/WrapperExt.kt | 新增基于 reified 的 Wrapper 构建入口 |
| mybatis-plus-extension/src/main/kotlin/com/baomidou/mybatisplus/extension/kotlin/KtUpdateWrapper.kt | 增加 KClass<T> 构造函数以支持 T::class |
| mybatis-plus-extension/src/main/kotlin/com/baomidou/mybatisplus/extension/kotlin/KtQueryWrapper.kt | 增加 KClass<T> 构造函数以支持 T::class |
| mybatis-plus-extension/src/main/kotlin/com/baomidou/mybatisplus/extension/kotlin/KtQueryChainWrapper.kt | 将 DSL 接口挂到 Chain Wrapper 上以支持链式 DSL |
| mybatis-plus-extension/src/main/kotlin/com/baomidou/mybatisplus/extension/kotlin/FuncDsl.kt | 新增 isNull/isIn/isNotIn 等函数型 DSL |
| mybatis-plus-extension/src/main/kotlin/com/baomidou/mybatisplus/extension/kotlin/DbExt.kt | 新增 ktQuery { ... } 顶层/KClass 扩展入口 |
| mybatis-plus-extension/src/main/kotlin/com/baomidou/mybatisplus/extension/kotlin/CompareDsl.kt | 新增 eq/ne/gt/.../between/like 等比较型 DSL |
| mybatis-plus-extension/src/main/kotlin/com/baomidou/mybatisplus/extension/kotlin/BaseMapperExt.kt | 新增 BaseMapper 的 Kotlin 语法糖调用入口 |
| mybatis-plus-extension/src/main/kotlin/com/baomidou/mybatisplus/extension/kotlin/AbstractKtWrapper.kt | 将 DSL 接口接入所有 Kotlin Wrapper 基类 |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+17
to
+21
| infix fun KProperty1<in T, *>.isIn(coll: Collection<*>?) = this@FuncDsl.`in`(this, coll) | ||
| infix fun KProperty1<in T, *>.isIn(array: Array<*>) = this@FuncDsl.`in`(this, array) | ||
|
|
||
| infix fun KProperty1<in T, *>.isNotIn(coll: Collection<*>?) = this@FuncDsl.notIn(this, coll) | ||
| infix fun KProperty1<in T, *>.isNotIn(array: Array<*>) = this@FuncDsl.notIn(this, array) |
Comment on lines
+1
to
+2
| package com.baomidou.mybatisplus.extension.kotlin | ||
|
|
Comment on lines
+1
to
+2
| package com.baomidou.mybatisplus.extension.kotlin | ||
|
|
Comment on lines
+1
to
+2
| package com.baomidou.mybatisplus.extension.kotlin | ||
|
|
Comment on lines
+1
to
+2
| package com.baomidou.mybatisplus.extension.kotlin | ||
|
|
Comment on lines
+62
to
+65
| inline fun <reified T : Any> BaseMapper<T>.selectMaps( | ||
| page: IPage<Map<String, Any>>? = null, | ||
| noinline build: (KtQueryWrapper<T>.() -> Unit)? = null, | ||
| ): List<Map<String, Any>> = this.selectMaps(page, build.toKtQueryWrapper()) |
Comment on lines
+67
to
+70
| inline fun <reified T : Any> BaseMapper<T>.selectMaps( | ||
| noinline build: (KtQueryWrapper<T>.() -> Unit)? = null, | ||
| resultHandler: ResultHandler<Map<String, Any>> | ||
| ) = this.selectMaps(build.toKtQueryWrapper(), resultHandler) |
Comment on lines
+72
to
+76
| inline fun <reified T : Any> BaseMapper<T>.selectMaps( | ||
| page: IPage<Map<String, Any>>? = null, | ||
| noinline build: (KtQueryWrapper<T>.() -> Unit)? = null, | ||
| resultHandler: ResultHandler<Map<String, Any>> | ||
| ) = this.selectMaps(page, build.toKtQueryWrapper(), resultHandler) |
Comment on lines
+78
to
+81
| inline fun <reified T : Any, P : IPage<Map<String, Any>>> BaseMapper<T>.selectMapsPage( | ||
| page: P? = null, | ||
| noinline build: (KtQueryWrapper<T>.() -> Unit)? = null, | ||
| ) = this.selectMapsPage(page, build.toKtQueryWrapper()) |
Comment on lines
+13
to
+29
| inline fun <reified T : Any> BaseMapper<T>.delete(noinline build: (KtQueryWrapper<T>.() -> Unit)? = null): Int = | ||
| this.delete(build.toKtQueryWrapper()) | ||
|
|
||
| inline fun <reified T : Any> BaseMapper<T>.update( | ||
| entity: T? = null, | ||
| noinline build: (KtUpdateWrapper<T>.() -> Unit)? = null | ||
| ): Int = | ||
| this.update(entity, build.toKtUpdateWrapper()) | ||
|
|
||
| inline fun <reified T : Any> BaseMapper<T>.update(noinline build: (KtUpdateWrapper<T>.() -> Unit)? = null): Int = | ||
| this.update(build.toKtUpdateWrapper()) | ||
|
|
||
| inline fun <reified T : Any> BaseMapper<T>.selectOne( | ||
| throwEx: Boolean = true, | ||
| noinline build: (KtQueryWrapper<T>.() -> Unit)? = null, | ||
| ): T? = | ||
| this.selectOne(build.toKtQueryWrapper(), throwEx) |
e24207f to
db0b3c4
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
增加BaseMapper 下所有AbstractWrapper 相关的方法的语法糖,如下,已有逻辑不改变。
新增类
BaseMapperExt.kt
CompareDsl.kt
DbExt.kt
FuncDsl.kt
WrapperExt.kt
`
val user: User? = selectOne {
eq(User::id, id)
`