diff --git a/mybatis-flex-kotlin/src/main/kotlin/com/mybatisflex/kotlin/entry/Entry.kt b/mybatis-flex-kotlin/src/main/kotlin/com/mybatisflex/kotlin/entry/Entry.kt
deleted file mode 100644
index baad83bd..00000000
--- a/mybatis-flex-kotlin/src/main/kotlin/com/mybatisflex/kotlin/entry/Entry.kt
+++ /dev/null
@@ -1,64 +0,0 @@
-/*
- * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.mybatisflex.kotlin.entry
-
-import com.mybatisflex.core.dialect.DialectFactory
-import com.mybatisflex.core.row.Db.*
-import com.mybatisflex.core.table.TableInfoFactory
-import com.mybatisflex.core.util.ArrayUtil
-import java.io.Serializable
-/**
- * 实体类父类,继承该类后将赋予实体简单增删改操作
- * @author 卡莫sama(yuanjiashuai)
- * @date 2023/8/7
- */
-open class Entry :Serializable{
-
- fun save(ignoreNulls: Boolean = true): Boolean {
- val tableInfo = TableInfoFactory.ofEntityClass(this.javaClass)
- //设置乐观锁版本字段的初始化数据
- tableInfo.initVersionValueIfNecessary(this)
- //设置租户ID
- tableInfo.initTenantIdIfNecessary(this)
- //设置逻辑删除字段的出初始化数据
- tableInfo.initLogicDeleteValueIfNecessary(this)
- //执行 onInsert 监听器
- tableInfo.invokeOnInsertListener(this)
- val values = tableInfo.buildInsertSqlArgs(this, ignoreNulls)
- val sql = DialectFactory.getDialect().forInsertEntity(tableInfo, this, ignoreNulls)
- return insertBySql(sql, *values) == 1
- }
-
- fun update(ignoreNulls: Boolean = true): Boolean {
- val tableInfo = TableInfoFactory.ofEntityClass(this.javaClass)
- //执行 onUpdate 监听器
- tableInfo.invokeOnUpdateListener(this)
- val updateValues = tableInfo.buildUpdateSqlArgs(this, ignoreNulls, false)
- val primaryValues = tableInfo.buildPkSqlArgs(this)
- val tenantIdArgs = tableInfo.buildTenantIdArgs()
- val sql = DialectFactory.getDialect().forUpdateEntity(tableInfo, this, ignoreNulls)
- return updateBySql(sql, *ArrayUtil.concat(updateValues, primaryValues, tenantIdArgs)) == 1
- }
-
- fun deleteById(): Boolean {
- val tableInfo = TableInfoFactory.ofEntityClass(this.javaClass)
- val primaryValues = tableInfo.buildPkSqlArgs(this)
- val allValues = ArrayUtil.concat(primaryValues, tableInfo.buildTenantIdArgs())
- val sql = DialectFactory.getDialect().forDeleteEntityById(tableInfo)
- return deleteBySql(sql, *allValues) == 1
- }
-
-}
diff --git a/mybatis-flex-kotlin/src/main/kotlin/com/mybatisflex/kotlin/extensions/db/DbExtensions.kt b/mybatis-flex-kotlin/src/main/kotlin/com/mybatisflex/kotlin/extensions/db/DbExtensions.kt
index 6ba8a771..ac0b3bbf 100644
--- a/mybatis-flex-kotlin/src/main/kotlin/com/mybatisflex/kotlin/extensions/db/DbExtensions.kt
+++ b/mybatis-flex-kotlin/src/main/kotlin/com/mybatisflex/kotlin/extensions/db/DbExtensions.kt
@@ -15,8 +15,7 @@
*/
package com.mybatisflex.kotlin.extensions.db
-import com.mybatisflex.core.BaseMapper
-import com.mybatisflex.core.MybatisFlexBootstrap
+import com.mybatisflex.core.mybatis.Mappers
import com.mybatisflex.core.query.QueryColumn
import com.mybatisflex.core.query.QueryCondition
import com.mybatisflex.core.row.Db.selectListByQuery
@@ -24,22 +23,18 @@ import com.mybatisflex.core.row.Db.selectOneByQuery
import com.mybatisflex.core.row.Row
import com.mybatisflex.core.table.TableDef
import com.mybatisflex.core.table.TableInfoFactory
-import com.mybatisflex.kotlin.extensions.entry.filter
-import com.mybatisflex.kotlin.extensions.entry.toEntities
+import com.mybatisflex.kotlin.extensions.model.filter
+import com.mybatisflex.kotlin.extensions.model.toEntities
import com.mybatisflex.kotlin.scope.QueryScope
import com.mybatisflex.kotlin.scope.queryScope
-/**
+/*
* 数据库简单操作扩展
* @author 卡莫sama(yuanjiashuai)
- * @date 2023/8/7
*/
-
-
-
-inline fun > mapper(): M = MybatisFlexBootstrap.getInstance().getMapper(M::class.java)
+inline fun mapper(): M = Mappers.ofMapperClass(M::class.java)
inline fun queryOne(
vararg columns: QueryColumn,
@@ -92,7 +87,7 @@ inline fun filter(
queryScope(*columns).where(queryCondition)
).toEntities()
-inline fun filter(
+inline fun filter(
vararg columns: QueryColumn?,
init: () -> QueryCondition
): List {
@@ -113,4 +108,3 @@ inline fun filter(
-
diff --git a/mybatis-flex-kotlin/src/main/kotlin/com/mybatisflex/kotlin/extensions/mapper/MapperExtensions.kt b/mybatis-flex-kotlin/src/main/kotlin/com/mybatisflex/kotlin/extensions/mapper/MapperExtensions.kt
index 342c4984..dd201a23 100644
--- a/mybatis-flex-kotlin/src/main/kotlin/com/mybatisflex/kotlin/extensions/mapper/MapperExtensions.kt
+++ b/mybatis-flex-kotlin/src/main/kotlin/com/mybatisflex/kotlin/extensions/mapper/MapperExtensions.kt
@@ -22,16 +22,14 @@ import com.mybatisflex.kotlin.scope.queryScope
/*
* 映射器操作扩展
* @author 卡莫sama(yuanjiashuai)
- * @date 2023/8/7
*/
-fun BaseMapper<*>.queryList(init: (QueryScope.() -> Unit)? = null): List =
+fun BaseMapper<*>.queryList(init: (QueryScope.() -> Unit)?): List =
this.selectListByQuery(queryScope(init = init)) as List
fun BaseMapper.update(entity: T, init: () -> QueryCondition): Int =
this.updateByCondition(entity, init())
-fun BaseMapper.delete(init: (QueryScope.() -> Unit)? = null): Int =
+fun BaseMapper.delete(init: (QueryScope.() -> Unit)?): Int =
this.deleteByQuery(queryScope(init = init))
-fun BaseMapper.delete1(init: () -> QueryCondition): Int =
- this.deleteByCondition(init())
+
diff --git a/mybatis-flex-kotlin/src/main/kotlin/com/mybatisflex/kotlin/extensions/entry/EntryExtensions.kt b/mybatis-flex-kotlin/src/main/kotlin/com/mybatisflex/kotlin/extensions/model/ModelExtensions.kt
similarity index 56%
rename from mybatis-flex-kotlin/src/main/kotlin/com/mybatisflex/kotlin/extensions/entry/EntryExtensions.kt
rename to mybatis-flex-kotlin/src/main/kotlin/com/mybatisflex/kotlin/extensions/model/ModelExtensions.kt
index e12cb259..42731404 100644
--- a/mybatis-flex-kotlin/src/main/kotlin/com/mybatisflex/kotlin/extensions/entry/EntryExtensions.kt
+++ b/mybatis-flex-kotlin/src/main/kotlin/com/mybatisflex/kotlin/extensions/model/ModelExtensions.kt
@@ -13,27 +13,25 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package com.mybatisflex.kotlin.extensions.entry
+package com.mybatisflex.kotlin.extensions.model
-import com.mybatisflex.core.FlexConsts
-import com.mybatisflex.core.dialect.DialectFactory
+import com.mybatisflex.core.activerecord.Model
+import com.mybatisflex.core.mybatis.Mappers
import com.mybatisflex.core.query.QueryColumn
import com.mybatisflex.core.query.QueryCondition
import com.mybatisflex.core.row.Db.*
-import com.mybatisflex.kotlin.extensions.db.*
import com.mybatisflex.core.row.Row
import com.mybatisflex.core.row.RowUtil
import com.mybatisflex.core.table.TableDef
import com.mybatisflex.core.table.TableInfoFactory
-import com.mybatisflex.core.util.ArrayUtil
-import com.mybatisflex.kotlin.entry.Entry
+import com.mybatisflex.core.util.SqlUtil
+import com.mybatisflex.kotlin.extensions.db.*
import com.mybatisflex.kotlin.scope.QueryScope
-import java.util.Arrays
+import java.io.Serializable
/*
* 实体操作扩展
* @author 卡莫sama(yuanjiashuai)
- * @date 2023/8/7
*/
infix fun Row.to(entryClass: Class): T {
@@ -69,31 +67,13 @@ inline fun TableDef.all(): List = selectAll(schema, tableName).to
inline fun Collection.toEntities() = map { it to E::class.java }.toList()
-inline fun List.batchInsert(): Boolean {
- val entities = this
- val tableInfo = TableInfoFactory.ofEntityClass(E::class.java)
- for (entity in entities) {
- tableInfo.initVersionValueIfNecessary(entity)
- tableInfo.initTenantIdIfNecessary(entity)
- tableInfo.initLogicDeleteValueIfNecessary(entity)
- //执行 onInsert 监听器
- tableInfo.invokeOnInsertListener(entity)
- }
- var allValues = FlexConsts.EMPTY_ARRAY
- for (entity in entities) {
- allValues = ArrayUtil.concat(allValues, tableInfo.buildInsertSqlArgs(entity, false))
- }
- val sql = DialectFactory.getDialect().forInsertEntityBatch(tableInfo, entities)
- return insertBySql(sql,*allValues) > 1
+inline fun> List.batchInsert(): Int = Mappers.ofEntityClass(E::class.java).insertBatch(this)
+
+fun< E:Model> List.batchUpdateById(): Boolean = all(Model::updateById)
+
+inline fun> List. batchDeleteById(): Boolean {
+ //拿到集合中所有实体的主键
+ val primaryValues = this.map { it.pkValues() }.flatMap(Array<*>::toMutableList).map { it as Serializable }
+ return SqlUtil.toBool(Mappers.ofEntityClass(E::class.java).deleteBatchByIds(primaryValues))
}
-
-fun< E:Entry> List.batchUpdate(): Boolean = all(Entry::update)
-
-inline fun List. batchDeleteById(): Boolean {
- val tableInfo = TableInfoFactory.ofEntityClass(E::class.java)
- val primaryValues = this.map { tableInfo.buildPkSqlArgs(it) }.stream().flatMap(Arrays::stream).toArray()
- val tenantIdArgs = tableInfo.buildTenantIdArgs()
- val sql = DialectFactory.getDialect().forDeleteEntityBatchByIds(tableInfo, primaryValues)
- return deleteBySql(sql,*ArrayUtil.concat(primaryValues, tenantIdArgs)) > 1
-}
diff --git a/mybatis-flex-kotlin/src/main/kotlin/com/mybatisflex/kotlin/extensions/sql/SqlExtensions.kt b/mybatis-flex-kotlin/src/main/kotlin/com/mybatisflex/kotlin/extensions/sql/SqlExtensions.kt
index c84e7005..35e7360d 100644
--- a/mybatis-flex-kotlin/src/main/kotlin/com/mybatisflex/kotlin/extensions/sql/SqlExtensions.kt
+++ b/mybatis-flex-kotlin/src/main/kotlin/com/mybatisflex/kotlin/extensions/sql/SqlExtensions.kt
@@ -15,11 +15,9 @@
*/
package com.mybatisflex.kotlin.extensions.sql
-import com.mybatisflex.core.query.Joiner
-import com.mybatisflex.core.query.QueryColumn
-import com.mybatisflex.core.query.QueryCondition
-import com.mybatisflex.core.query.QueryWrapper
+import com.mybatisflex.core.query.*
import java.util.function.Consumer
+
/*
* sql操作扩展
* @author 卡莫sama(yuanjiashuai)
@@ -29,26 +27,14 @@ infix fun QueryColumn.eq(value: Any?): QueryCondition {
return this.eq(value)
}
-inline fun QueryCondition.andIf(test: Boolean, block: () -> QueryCondition): QueryCondition {
- if (test) {
- this.and(block())
- }
- return this
-}
+inline fun QueryCondition.andIf(test: Boolean, block: () -> QueryCondition): QueryCondition =
+ if (test) this.and(block()) else this
-inline fun QueryCondition.orIf(test: Boolean, block: () -> QueryCondition): QueryCondition {
- if (test) {
- this.or(block())
- }
- return this
-}
+inline fun QueryCondition.orIf(test: Boolean, block: () -> QueryCondition): QueryCondition =
+ if (test) this.or(block()) else this
-inline fun `if`(test: Boolean, block: () -> QueryCondition): QueryCondition {
- if (test) {
- return block()
- }
- return QueryCondition.createEmpty()
-}
+inline fun `if`(test: Boolean, block: () -> QueryCondition): QueryCondition =
+ if (test) block() else QueryCondition.createEmpty()
infix fun QueryColumn.like(value: String): QueryCondition = this.like(value)
@@ -58,11 +44,29 @@ infix fun QueryCondition.or(other: QueryCondition): QueryCondition = this.or(oth
infix fun QueryColumn.`=`(value: Any?): QueryCondition = this.eq(value)
-infix fun QueryColumn.`in`(value: Collection): QueryCondition = this.`in`(value)
+infix fun QueryColumn.`!=`(value: Any?): QueryCondition = this.ne(value)
-fun QueryColumn.`in`(vararg values: Array): QueryCondition = this.`in`(values.asList())
+infix fun QueryColumn.gt(value: Any?): QueryCondition = this.gt(value)
-infix fun QueryWrapper.`as`(alias: String) = this.`as`(alias)
+infix fun QueryColumn.ge(value: Any?): QueryCondition = this.ge(value)
+
+infix fun QueryColumn.le(value: Any?): QueryCondition = this.le(value)
+
+infix fun QueryColumn.lt(value: Any?): QueryCondition = this.lt(value)
+
+infix fun QueryColumn.between(pair: Pair): QueryCondition = this.between(pair.first, pair.second)
+
+infix fun QueryColumn.notBetween(pair: Pair): QueryCondition = this.notBetween(pair.first, pair.second)
+
+infix fun QueryColumn.notIn(value: Collection): QueryCondition = this.notIn(value)
+
+infix fun QueryColumn.notIn(values: Array): QueryCondition = this.notIn(values)
+
+infix fun QueryColumn.`in`(value: Collection): QueryCondition = this.`in`(value)
+
+infix fun QueryColumn.`in`(values: Array): QueryCondition = this.`in`(values)
+
+infix fun QueryWrapper.`as`(alias: String?) = this.`as`(alias)
infix fun Joiner.`as`(alias: String?): Joiner = this.`as`(alias)
@@ -72,3 +76,8 @@ infix fun Joiner.on(on: QueryCondition?): M = this.on(on)
infix fun Joiner.on(consumer: Consumer): M = this.on(consumer)
+infix fun QueryWrapper.orderBy(orderBys: Collection): QueryWrapper = this.orderBy(*orderBys.toTypedArray())
+
+infix fun QueryWrapper.limit(rows: Number): QueryWrapper = this.limit(rows)
+
+infix fun QueryWrapper.limit(pair: Pair): QueryWrapper = this.limit(pair.first,pair.second)
diff --git a/mybatis-flex-kotlin/src/main/kotlin/com/mybatisflex/kotlin/scope/BootstrapScope.kt b/mybatis-flex-kotlin/src/main/kotlin/com/mybatisflex/kotlin/scope/BootstrapScope.kt
index 870ec4c3..85ed4f3e 100644
--- a/mybatis-flex-kotlin/src/main/kotlin/com/mybatisflex/kotlin/scope/BootstrapScope.kt
+++ b/mybatis-flex-kotlin/src/main/kotlin/com/mybatisflex/kotlin/scope/BootstrapScope.kt
@@ -41,9 +41,6 @@ class DataSourceScope(private val bootstrap: MybatisFlexBootstrap) {
fun dataSource(dataSourceKey: String, dataSource: DataSource) =
bootstrap.addDataSource(dataSourceKey, dataSource)
-// infix fun String.of(dataSource: DataSource) =
-// bootstrap.addDataSource(this, dataSource)
-
}
diff --git a/mybatis-flex-test/mybatis-flex-spring-kotlin-test/pom.xml b/mybatis-flex-test/mybatis-flex-spring-kotlin-test/pom.xml
index 45b4b173..2a152e5d 100755
--- a/mybatis-flex-test/mybatis-flex-spring-kotlin-test/pom.xml
+++ b/mybatis-flex-test/mybatis-flex-spring-kotlin-test/pom.xml
@@ -14,6 +14,7 @@
8
8
+ true
UTF-8
1.9.0
@@ -24,11 +25,6 @@
mybatis-flex-kotlin
${mybatis-flex.version}
-
- com.mybatis-flex
- mybatis-flex-core
- ${mybatis-flex.version}
-
com.mybatis-flex
@@ -40,44 +36,36 @@
org.springframework
spring-jdbc
-
- org.yaml
- snakeyaml
- 2.0
+ org.jetbrains.kotlin
+ kotlin-stdlib-jdk8
+ ${kotlin.version}
-
com.h2database
h2
2.1.214
-
+
org.springframework
spring-test
test
-
org.assertj
assertj-core
3.22.0
test
-
- org.jetbrains.kotlin
- kotlin-stdlib-jdk8
- ${kotlin.version}
-
org.jetbrains.kotlin
kotlin-test-junit
${kotlin.version}
test
-