Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ import kotlin.reflect.KProperty1
* @since 2018-11-07
*/
@Suppress("serial")
abstract class AbstractKtWrapper<T, Children : AbstractKtWrapper<T, Children>> : AbstractWrapper<T, KProperty1<in T, *>, Children>() {
abstract class AbstractKtWrapper<T, Children : AbstractKtWrapper<T, Children>> :
AbstractWrapper<T, KProperty1<in T, *>, Children>(),
CompareDsl<Children, T>,
FuncDsl<Children, T> {

/**
* 列 Map
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.baomidou.mybatisplus.extension.kotlin

import com.baomidou.mybatisplus.core.mapper.BaseMapper
import com.baomidou.mybatisplus.core.metadata.IPage
import org.apache.ibatis.session.ResultHandler
import kotlin.apply

/**
* @Author lidiwei
* @CreateTime 2025/7/31 10:13
*/

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)

inline fun <reified T : Any> BaseMapper<T>.exists(noinline build: (KtQueryWrapper<T>.() -> Unit)? = null): Boolean =
this.exists(build.toKtQueryWrapper())

inline fun <reified T : Any> BaseMapper<T>.selectCount(noinline build: (KtQueryWrapper<T>.() -> Unit)? = null): Long =
this.selectCount(build.toKtQueryWrapper())

inline fun <reified T : Any> BaseMapper<T>.selectList(noinline build: (KtQueryWrapper<T>.() -> Unit)? = null): List<T> =
this.selectList(build.toKtQueryWrapper())


inline fun <reified T : Any> BaseMapper<T>.selectList(
page: IPage<T>,
noinline build: (KtQueryWrapper<T>.() -> Unit)? = null,
): List<T> =
this.selectList(page, build.toKtQueryWrapper())

inline fun <reified T : Any> BaseMapper<T>.selectList(
noinline build: (KtQueryWrapper<T>.() -> Unit)? = null,
resultHandler: ResultHandler<T>
) = this.selectList(build.toKtQueryWrapper(), resultHandler)

inline fun <reified T : Any> BaseMapper<T>.selectList(
page: IPage<T>,
noinline build: (KtQueryWrapper<T>.() -> Unit)? = null,
resultHandler: ResultHandler<T>
) = this.selectList(page, build.toKtQueryWrapper(), resultHandler)

inline fun <reified T : Any> BaseMapper<T>.selectMaps(
noinline build: (KtQueryWrapper<T>.() -> Unit)? = null,
): List<Map<String, Any>> = this.selectMaps(build.toKtQueryWrapper())

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())

inline fun <reified T : Any> BaseMapper<T>.selectMaps(
noinline build: (KtQueryWrapper<T>.() -> Unit)? = null,
resultHandler: ResultHandler<Map<String, Any>>
) = this.selectMaps(build.toKtQueryWrapper(), resultHandler)

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)

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())

inline fun <reified T : Any> BaseMapper<T>.selectPage(
page: IPage<T>,
noinline build: (KtQueryWrapper<T>.() -> Unit)? = null,
): IPage<T> =
this.selectPage(page, build.toKtQueryWrapper())

inline fun <reified T : Any> (KtQueryWrapper<T>.() -> Unit)?.toKtQueryWrapper() =
this?.let { buildKtQueryWrapper<T>().apply(it) }

inline fun <reified T : Any> (KtUpdateWrapper<T>.() -> Unit)?.toKtUpdateWrapper() =
this?.let { buildKtUpdateWrapper<T>().apply(it) }
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.baomidou.mybatisplus.extension.kotlin

import com.baomidou.mybatisplus.core.conditions.interfaces.Compare
import kotlin.reflect.KProperty1

/**
* @Author lidiwei
* @CreateTime 2025/7/31 16:18
*/
interface CompareDsl<Children, T> : Compare<Children, KProperty1<in T, *>> {

infix fun <V> KProperty1<in T, V>.eq(value: V?) = [email protected](this, value)
infix fun <V> KProperty1<in T, V>.ne(value: V?) = [email protected](this, value)
infix fun <V> KProperty1<in T, V>.gt(value: V?) = [email protected](this, value)
infix fun <V> KProperty1<in T, V>.ge(value: V?) = [email protected](this, value)
infix fun <V> KProperty1<in T, V>.lt(value: V?) = [email protected](this, value)
infix fun <V> KProperty1<in T, V>.le(value: V?) = [email protected](this, value)
infix fun <A, B> KProperty1<in T, *>.between(value: Pair<A, B>) =
[email protected](this, value.first, value.second)

infix fun <A, B> KProperty1<in T, *>.notBetween(value: Pair<A, B>) =
[email protected](this, value.first, value.second)

infix fun <V> KProperty1<in T, V>.like(value: V?) = [email protected](this, value)
infix fun <V> KProperty1<in T, V>.notLike(value: V?) = [email protected](this, value)
infix fun <V> KProperty1<in T, V>.likeLeft(value: V?) = [email protected](this, value)
infix fun <V> KProperty1<in T, V>.notLikeLeft(value: V?) = [email protected](this, value)
infix fun <V> KProperty1<in T, V>.likeRight(value: V?) = [email protected](this, value)
infix fun <V> KProperty1<in T, V>.notLikeRight(value: V?) = [email protected](this, value)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.baomidou.mybatisplus.extension.kotlin

import com.baomidou.mybatisplus.extension.toolkit.Db
import kotlin.reflect.KClass

/**
* @Author lidiwei
* @CreateTime 2025/7/31 16:15
*/

inline fun <reified T : Any> ktQuery(noinline query: (KtQueryChainWrapper<T>.() -> Unit)? = null): KtQueryChainWrapper<T> =
Db.ktQuery<T>(T::class.java).also { query?.invoke(it) }

inline fun <reified T : Any, C : KClass<T>> C.ktQuery(noinline query: (KtQueryChainWrapper<T>.() -> Unit)? = null): KtQueryChainWrapper<T> =
Db.ktQuery<T>(T::class.java).also { query?.invoke(it) }
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.baomidou.mybatisplus.extension.kotlin

import com.baomidou.mybatisplus.core.conditions.interfaces.Func
import kotlin.reflect.KProperty1

/**
* @Author lidiwei
* @CreateTime 2025/7/31 16:20
*/
interface FuncDsl<Children, T> : Func<Children, KProperty1<in T, *>> {
val KProperty1<in T, *>.isNull: Children?
get() = [email protected](this)

val KProperty1<in T, *>.isNotNull: Children?
get() = [email protected](this)

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<*>?) = [email protected](this, coll)
infix fun KProperty1<in T, *>.isNotIn(array: Array<*>) = [email protected](this, array)

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ import kotlin.reflect.KProperty1
open class KtQueryChainWrapper<T : Any>(
internal val baseMapper: BaseMapper<T>?
) : AbstractChainWrapper<T, KProperty1<in T, *>, KtQueryChainWrapper<T>, KtQueryWrapper<T>>(),
ChainQuery<T>, Query<KtQueryChainWrapper<T>, T, KProperty1<in T, *>> {
ChainQuery<T>, Query<KtQueryChainWrapper<T>, T, KProperty1<in T, *>>,
CompareDsl<KtQueryChainWrapper<T>, T>,
FuncDsl<KtQueryChainWrapper<T>, T> {

constructor(baseMapper: BaseMapper<T>, entityClass: Class<T>) : this(baseMapper) {
super.wrapperChildren = KtQueryWrapper(entityClass)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.baomidou.mybatisplus.core.toolkit.CollectionUtils
import com.baomidou.mybatisplus.core.toolkit.support.ColumnCache
import java.util.concurrent.atomic.AtomicInteger
import java.util.function.Predicate
import kotlin.reflect.KClass
import kotlin.reflect.KProperty1

/**
Expand All @@ -40,6 +41,11 @@ open class KtQueryWrapper<T : Any> : AbstractKtWrapper<T, KtQueryWrapper<T>>, Qu
*/
private var sqlSelect: SharedString = SharedString()

constructor(entity: KClass<T>) {
this.entityClass = entity.java
super.initNeed()
}

constructor(entity: T) {
this.entity = entity
super.initNeed()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import com.baomidou.mybatisplus.core.toolkit.support.ColumnCache
import java.math.BigDecimal
import java.util.concurrent.atomic.AtomicInteger
import java.util.stream.Collectors.joining
import kotlin.reflect.KClass
import kotlin.reflect.KProperty1

/**
Expand All @@ -42,6 +43,11 @@ open class KtUpdateWrapper<T : Any> : AbstractKtWrapper<T, KtUpdateWrapper<T>>,
*/
private val sqlSet = ArrayList<String>()

constructor(entity: KClass<T>) {
this.entityClass = entity.java
super.initNeed()
}

constructor(entity: T) {
this.entity = entity
super.initNeed()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.baomidou.mybatisplus.extension.kotlin

/**
* @Author lidiwei
* @CreateTime 2025/7/31 18:09
*/

inline fun <reified T : Any> buildKtQueryWrapper(): KtQueryWrapper<T> = KtQueryWrapper(T::class)

inline fun <reified T : Any> buildKtUpdateWrapper(): KtUpdateWrapper<T> = KtUpdateWrapper(T::class)