Skip to content

feat:增强kotlin的BaseMapper语法糖以及dsl写法#6880

Open
huicunjun wants to merge 3 commits into
baomidou:3.0from
huicunjun:master
Open

feat:增强kotlin的BaseMapper语法糖以及dsl写法#6880
huicunjun wants to merge 3 commits into
baomidou:3.0from
huicunjun:master

Conversation

@huicunjun

@huicunjun huicunjun commented Jul 31, 2025

Copy link
Copy Markdown
Contributor

增加BaseMapper 下所有AbstractWrapper 相关的方法的语法糖,如下,已有逻辑不改变。

新增类
BaseMapperExt.kt
CompareDsl.kt
DbExt.kt
FuncDsl.kt
WrapperExt.kt

`

val user: User? = selectOne {
eq(User::id, id)

        User::id eq id
        User::id ne id
        User::id le id
        User::id lt id
        User::id ge id
        User::id gt id

        User::id between ("2025-01-01 00:00:00" to "2025-01-01 23:23:59")

        User::name like "baomidou"
        User::name notLike "baomidou"
        User::name likeLeft "baomidou"
        User::name notLikeLeft "baomidou"
        User::name notLikeRight "baomidou"

        User::id.isNull
        User::id.isNotNull
        User::name isIn arrayOf("baomidou")
        User::name isIn listOf("baomidou")
        User::name isNotIn arrayOf("baomidou")
        User::name isNotIn listOf("baomidou")
    }

    val user2: User? =User::class.ktQuery {
        User::id eq id
        User::id ne id
    }.one()

    val user3: User? = ktQuery<User> {
        User::id eq id
        User::id ne id
        User::id le id
        User::id lt id
        User::id ge id
        User::id gt id

        User::id between ("2025-01-01 00:00:00" to "2025-01-01 23:23:59")

        User::name like "%baomidou%"
        User::name notLike "%baomidou%"
        User::name likeLeft "%baomidou%"
        User::name notLikeLeft "%baomidou%"
        User::name notLikeRight "%baomidou%"

        User::id.isNull
        User::id.isNotNull
        User::name isIn arrayOf("%baomidou%")
        User::name isIn listOf("%baomidou%")
        User::name isNotIn arrayOf("%baomidou%")
        User::name isNotIn listOf("%baomidou%")
    }.one()   

`

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 idUser::id.isNull 等写法
  • 新增 BaseMapper Kotlin 扩展与 Wrapper 构建函数(buildKtQueryWrapper/buildKtUpdateWrapperBaseMapper.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)
@qmdx qmdx force-pushed the 3.0 branch 5 times, most recently from e24207f to db0b3c4 Compare July 8, 2026 15:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants