From 0271234225c588a3f9a13356ca55ccfb86afa204 Mon Sep 17 00:00:00 2001 From: kamosama <837080904@qq.com> Date: Sat, 12 Aug 2023 23:18:51 +0800 Subject: [PATCH] =?UTF-8?q?refactor/doc/test:=E5=BC=95=E5=85=A5Model?= =?UTF-8?q?=E9=87=8D=E6=9E=84=EF=BC=8C=E6=B7=BB=E5=8A=A0=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E5=AE=9E=E4=BE=8B=E6=B3=A8=E9=87=8A=EF=BC=8C=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=BF=AB=E9=80=9F=E5=BC=80=E5=A7=8Bdoc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/zh/kotlin/kt-getting-started.md | 115 ++++++++++++++++++ mybatis-flex-kotlin/pom.xml | 54 ++++---- .../com/mybatisflex/kotlin/entry/Entry.kt | 64 ---------- .../kotlin/extensions/db/DbExtensions.kt | 18 +-- .../extensions/mapper/MapperExtensions.kt | 8 +- .../ModelExtensions.kt} | 48 +++----- .../kotlin/extensions/sql/SqlExtensions.kt | 59 +++++---- .../kotlin/scope/BootstrapScope.kt | 3 - .../mybatis-flex-spring-kotlin-test/pom.xml | 45 ++++--- .../mybatisflex/test/mapper/AccountMapper.kt | 11 +- .../com/mybatisflex/test/model/Account.kt | 12 +- .../src/main/resources/data-kt.sql | 8 +- .../src/main/resources/schema.sql | 2 +- .../kotlin/com/mybatisflex/test/KotlinTest.kt | 79 ++++++++---- 14 files changed, 298 insertions(+), 228 deletions(-) create mode 100644 docs/zh/kotlin/kt-getting-started.md delete mode 100644 mybatis-flex-kotlin/src/main/kotlin/com/mybatisflex/kotlin/entry/Entry.kt rename mybatis-flex-kotlin/src/main/kotlin/com/mybatisflex/kotlin/extensions/{entry/EntryExtensions.kt => model/ModelExtensions.kt} (56%) diff --git a/docs/zh/kotlin/kt-getting-started.md b/docs/zh/kotlin/kt-getting-started.md new file mode 100644 index 00000000..37488ef3 --- /dev/null +++ b/docs/zh/kotlin/kt-getting-started.md @@ -0,0 +1,115 @@ +# 基于 Kotlin 扩展 Mybatis-Flex +## 快速开始 + +在开始之前,我们假定您已经: + +- 熟悉 Kotlin 环境配置及其开发 +- 熟悉 关系型 数据库,比如 MySQL +- 熟悉 Kotlin 构建工具,比如 Gradle、Maven + +> 当前章节涉及到的 [演示源码](https://gitee.com/mybatis-flex/mybatis-flex/tree/main/mybatis-flex-test/mybatis-flex-spring-kotlin-test) 已经全部上传 +> +> 在开始之前,您也可以先下载到本地,导入到 idea 开发工具后,在继续看文档。 + + +## 特点 + +- 本模块基于 Mybatis-Flex 核心库 ,只做扩展不做改变 +- 结合 Kotlin 特性、DSL让数据库操作更简单 + +## Hello World 文档 + +**第 1 步:创建 Kotlin 项目,并添加 Kotlin 的扩展依赖** + +>如何创建 Kotlin 项目可参考 [官方文档](https://www.kotlincn.net/docs/tutorials/jvm-get-started.html) + +需要添加的主要依赖: + +**【Kotlin】** + +```kotlin +dependencies { + implementation("com.mybatis-flex:mybatis-flex-kotlin:1.5.7") + compileOnly("com.mybatis-flex:mybatis-flex-processor:1.5.7") +} +``` + +**【Maven】** + +```xml + + com.mybatis-flex + mybatis-flex-kotlin + ${mybatis-flex.version} + +``` + +**第 2 步:创建数据库表与配置数据源** + +> 请参考 [快速开始](../intro/getting-started.md) 创建数据库表与配置数据源, +> 或者使用演示源码中的内嵌数据库快速体验 + +**第 3 步:编写实体类** + +```kotlin +@Table("tb_account") +class Account { + + @Id + var id: Long + var userName: String + var age: Integer + var birthday: Date + +} +``` + +- 使用 `@Table("tb_account")` 设置实体类与表名的映射关系 +- 使用 `@Id` 标识主键 + +**第 4 步:开始使用** + +添加测试类,进行功能测试: + +```kotlin +fun main() { + //加载数据源(为了方便演示这里使用了演示源码中的内嵌数据源) + val dataSource: DataSource = EmbeddedDatabaseBuilder() + .setType(EmbeddedDatabaseType.H2) + .addScript("schema.sql") + .addScript("data-kt.sql") + .build() + //启动并配入数据源 + buildBootstrap { +dataSource }.start() + //条件过滤查询并打印 + filter { + ACCOUNT.ID `=` 1 and + (ACCOUNT.AGE `in` listOf(18,19) or (ACCOUNT.BIRTHDAY between ("2020-01-10" to "2020-01-12")) ) + }.forEach(::println) + //查询全部数据并打印 + //ACCOUNT.all().forEach(::println) +} +``` +执行的SQL: +```sql +SELECT * FROM `tb_account` WHERE`id` = 1 AND (`age` IN (18, 19) OR `birthday`BETWEEN '2020-01-10' AND '2020-01-12' ) +``` +控制台输出: + +```txt +Account(id=1, userName=张三, age=18, birthday=Sat Jan 11 00:00:00 CST 2020) +``` + +> 以上的示例中, `ACCOUNT` 为 MyBatis-Flex 通过 APT +> 自动生成,只需通过静态导入即可,无需手动编码。更多查看 [在Kotlin中使用注解处理器](../others/kapt.md) +> +> 若觉得 APT 使用不习惯,也可以使用代码生成器来生成。点击 [代码生成器文档](../others/codegen.md) 了解。 + +## 更多使用 + +- 功能 1:[Bootstrap简化配置]() +- 功能 2:[简单查询]() +- 功能 3:[表实体扩展]() +- 功能 4:[SQL扩展/中缀]() +- 功能 5:[Mapper扩展]() +###### TODO ... diff --git a/mybatis-flex-kotlin/pom.xml b/mybatis-flex-kotlin/pom.xml index ddfac86d..1c0b6045 100755 --- a/mybatis-flex-kotlin/pom.xml +++ b/mybatis-flex-kotlin/pom.xml @@ -1,36 +1,37 @@ - 4.0.0 - - com.mybatis-flex - parent - 1.5.6 - + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + + com.mybatis-flex + parent + 1.5.6 + - mybatis-flex-kotlin + mybatis-flex-kotlin - - 8 - 8 - UTF-8 - 1.9.0 - + + 8 + 8 + 1.9.0 + true + UTF-8 + - - - com.mybatis-flex - mybatis-flex-core - ${mybatis-flex.version} - + + + com.mybatis-flex + mybatis-flex-core + ${mybatis-flex.version} + - - org.jetbrains.kotlin - kotlin-stdlib-jdk8 - ${kotlin.version} + + org.jetbrains.kotlin + kotlin-stdlib-jdk8 + ${kotlin.version} provided - + @@ -46,7 +47,6 @@ -Xjvm-default=all - 1.8 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 - + ${project.basedir}/src/main/kotlin ${project.basedir}/src/test/kotlin @@ -91,7 +79,26 @@ -Xjvm-default=all + + + no-arg + + + + + + + org.jetbrains.kotlin + kotlin-maven-sam-with-receiver + ${kotlin.version} + + + org.jetbrains.kotlin + kotlin-maven-noarg + ${kotlin.version} + + kapt @@ -99,7 +106,6 @@ kapt - ${project.basedir}/src/test/kotlin ${project.basedir}/src/main/kotlin @@ -116,6 +122,7 @@ compile + process-sources compile diff --git a/mybatis-flex-test/mybatis-flex-spring-kotlin-test/src/main/kotlin/com/mybatisflex/test/mapper/AccountMapper.kt b/mybatis-flex-test/mybatis-flex-spring-kotlin-test/src/main/kotlin/com/mybatisflex/test/mapper/AccountMapper.kt index da9f80d4..571f6cec 100755 --- a/mybatis-flex-test/mybatis-flex-spring-kotlin-test/src/main/kotlin/com/mybatisflex/test/mapper/AccountMapper.kt +++ b/mybatis-flex-test/mybatis-flex-spring-kotlin-test/src/main/kotlin/com/mybatisflex/test/mapper/AccountMapper.kt @@ -20,18 +20,17 @@ import com.mybatisflex.core.BaseMapper import com.mybatisflex.kotlin.extensions.mapper.queryList import com.mybatisflex.kotlin.extensions.sql.* import com.mybatisflex.test.model.Account -import com.mybatisflex.test.model.table.AccountTableDef.ACCOUNT @JvmDefaultWithCompatibility interface AccountMapper : BaseMapper { - fun findByAge(age: Int, vararg ids: Int): List = queryList { - select(ACCOUNT.ALL_COLUMNS) - from(ACCOUNT) - where(ACCOUNT) { - (AGE `=` age) and `if`(true) { + fun findByAge(age: Int, vararg ids: Int): List = queryList { + select(Account.ALL_COLUMNS) + from(Account) + where(Account) { + (AGE `=` age) and `if`(ids.isNotEmpty()) { ID `in` ids.asList() } } diff --git a/mybatis-flex-test/mybatis-flex-spring-kotlin-test/src/main/kotlin/com/mybatisflex/test/model/Account.kt b/mybatis-flex-test/mybatis-flex-spring-kotlin-test/src/main/kotlin/com/mybatisflex/test/model/Account.kt index 41d8872e..56cddc52 100755 --- a/mybatis-flex-test/mybatis-flex-spring-kotlin-test/src/main/kotlin/com/mybatisflex/test/model/Account.kt +++ b/mybatis-flex-test/mybatis-flex-spring-kotlin-test/src/main/kotlin/com/mybatisflex/test/model/Account.kt @@ -15,24 +15,26 @@ */ package com.mybatisflex.test.model -import com.mybatisflex.annotation.Column import com.mybatisflex.annotation.Id import com.mybatisflex.annotation.NoneListener import com.mybatisflex.annotation.Table -import com.mybatisflex.kotlin.entry.Entry +import com.mybatisflex.core.activerecord.Model +import com.mybatisflex.test.model.table.AccountTableDef import java.util.* /** - * 测试用数据类(最好不要写成data class,否则需要与数据库字段数据顺序一致) + * 测试用数据类(最好不要写成data class,否则没有无参构造需要与数据库字段数据顺序一致) * @author 卡莫sama(yuanjiashuai) * @date 2023/8/7 */ @Table(value = "tb_account", onUpdate = [NoneListener::class], onSet = [AccountOnSetListener::class]) data class Account( @Id var id: Int, - @Column("u_name") var userName: String?, + var userName: String?, var age: Int?, var birthday: Date?, -) : Entry(){ +) : Model(){ + companion object : AccountTableDef() + override fun toString(): String { return "Account(id=$id, userName=$userName, birthday=$birthday, age=$age)" } diff --git a/mybatis-flex-test/mybatis-flex-spring-kotlin-test/src/main/resources/data-kt.sql b/mybatis-flex-test/mybatis-flex-spring-kotlin-test/src/main/resources/data-kt.sql index 2ee98fe5..959cc26e 100755 --- a/mybatis-flex-test/mybatis-flex-spring-kotlin-test/src/main/resources/data-kt.sql +++ b/mybatis-flex-test/mybatis-flex-spring-kotlin-test/src/main/resources/data-kt.sql @@ -1,5 +1,3 @@ -INSERT INTO tb_account -VALUES (1, 'Michael Yang', 18, '2020-01-11'); - -INSERT INTO tb_account -VALUES (2, 'Michael Zhanng', 20, '2020-01-11'); +INSERT INTO tb_account(id, user_name, age, birthday) +VALUES (1, '张三', 18, '2020-01-11'), + (2, '李四', 19, '2021-03-21'); diff --git a/mybatis-flex-test/mybatis-flex-spring-kotlin-test/src/main/resources/schema.sql b/mybatis-flex-test/mybatis-flex-spring-kotlin-test/src/main/resources/schema.sql index f75d140c..161937d0 100755 --- a/mybatis-flex-test/mybatis-flex-spring-kotlin-test/src/main/resources/schema.sql +++ b/mybatis-flex-test/mybatis-flex-spring-kotlin-test/src/main/resources/schema.sql @@ -1,7 +1,7 @@ CREATE TABLE IF NOT EXISTS `tb_account` ( `id` INTEGER , - `u_name` VARCHAR(100) NOT NULL, + `user_name` VARCHAR(100) NOT NULL, `age` INTEGER, `birthday` DATETIME ); diff --git a/mybatis-flex-test/mybatis-flex-spring-kotlin-test/src/test/kotlin/com/mybatisflex/test/KotlinTest.kt b/mybatis-flex-test/mybatis-flex-spring-kotlin-test/src/test/kotlin/com/mybatisflex/test/KotlinTest.kt index 68bf7f2a..49a30161 100644 --- a/mybatis-flex-test/mybatis-flex-spring-kotlin-test/src/test/kotlin/com/mybatisflex/test/KotlinTest.kt +++ b/mybatis-flex-test/mybatis-flex-spring-kotlin-test/src/test/kotlin/com/mybatisflex/test/KotlinTest.kt @@ -16,13 +16,13 @@ package com.mybatisflex.test import com.mybatisflex.core.FlexConsts +import com.mybatisflex.core.activerecord.Model import com.mybatisflex.core.audit.AuditManager import com.mybatisflex.core.audit.ConsoleMessageCollector -import com.mybatisflex.kotlin.entry.Entry import com.mybatisflex.kotlin.extensions.db.filter import com.mybatisflex.kotlin.extensions.db.mapper import com.mybatisflex.kotlin.extensions.db.query -import com.mybatisflex.kotlin.extensions.entry.* +import com.mybatisflex.kotlin.extensions.model.* import com.mybatisflex.kotlin.extensions.sql.* import com.mybatisflex.kotlin.scope.buildBootstrap import com.mybatisflex.test.mapper.AccountMapper @@ -48,40 +48,75 @@ class KotlinTest { AuditManager.setMessageCollector(ConsoleMessageCollector()) buildBootstrap { +// 此方法体 it 是 MybatisFlexBootstrap 实例 +// 配置Mapper +// 1.通过+(重写自增)的方式 +AccountMapper::class.java - dataSources { -// dataSource(FlexConsts.NAME,dataSource) - FlexConsts.NAME of dataSource +// 2.通过原始的方式 +// it.addMapper(AccountMapper::class.java) + +// 配置单dataSource +// 1.通过+(重写自增)的方式 + +dataSource +// 2.通过原始的方式 +// it.setDataSource(dataSource) + +// 配置多dataSource +// 1.通过of(中缀)的方式 + FlexConsts.NAME of dataSource // "dataSource1" of dataSource // "dataSource2" of dataSource +// 2.通过dsl(中缀)的方式 + dataSources { +// dataSource(FlexConsts.NAME,dataSource) +// dataSource("dataSource1",dataSource) +// dataSource("dataSource2",dataSource) } -// + dataSource +// 3.通过原始的方式 +// it.addDataSource(FlexConsts.NAME,dataSource) }.start() -// - ACCOUNT.query {}.forEach(::println) + +// 查询表对象对应的所有实体数据 + ACCOUNT.all().forEach(::println) +// ACCOUNT.query {}.forEach(::println) +// a and (b or c) +// filter { +// ACCOUNT.AGE `=` 12 and +// (ACCOUNT.ID `in` listOf(1, 2) or (ACCOUNT.ID `in` listOf(1, 2))) +// } + filter { + ACCOUNT.ID `=` 1 and + (ACCOUNT.AGE `in` listOf(18,19) or (ACCOUNT.BIRTHDAY between ("2020-01-10" to "2020-01-12")) ) + }.forEach(::println) +// 查询表对象对应的实体数据并根据条件过滤 filter { ACCOUNT.AGE `=` 12 or - `if`(true) { ACCOUNT.ID `in` listOf(1, 2) } - }.stream().peek(::println).peek { it.id = it.id.plus(2) }.forEach(Entry::save) - //使用表对象filter或者DB对象有两个泛型的filter方法时方法体内this为表对象无需XXX.AA调用,直接AA -// ACCOUNT.filter { -// AGE `=` 12 or -// `if`(true) { ID `in` listOf(1, 2) } -// }.stream().peek(::println).peek { it.id = it.id.plus(6) }.forEach(Entry::save) + //if的第一个参数为true时则会调用花括号类的方法返回一个条件对象与上面那个条件对象相连接 + `if`(true) { ACCOUNT.ID between (1 to 2) } +// `if`(false) { ACCOUNT.ID `in` listOf(1, 2) } + }.stream().peek(::println) +// 过滤后修改id再次保存 + .peek { it.id = it.id.plus(2) }.forEach(Model<*>::save) +// 使用表对象filter或者DB对象有两个泛型的filter方法时方法体内this为表对象无需XXX.AA调用,直接AA +// ACCOUNT.filter { +// AGE `=` 12 or +// `if`(true) { ID `in` listOf(1, 2) } +// }.stream().peek(::println).peek { it.id = it.id.plus(6) }.forEach(Entry::save) println("保存后————————") - mapper().findByAge(18, 1, 2).stream().peek { println(it) }.forEach(Entry::deleteById) +// 获得mapper实例通过自定义的默认方法查,并将查到的删除 + mapper().findByAge(18, 1, 2).stream().peek { println(it) }.forEach{it.removeById()} println("删除后————————") - ACCOUNT.all().stream().peek { println(it) }.map { - it.userName = "sa" + Account.all().stream().peek { println(it) }.map { + it.userName = "kamo" it - }.forEach(Entry::update) + }.forEach{it.updateById()} println("更新后————————") ACCOUNT.all().stream().peek { println(it) }.map { it.id = it.id.plus(5) - it.userName = "423423" + it.userName = "akino" it }.toList().batchInsert() @@ -91,9 +126,9 @@ class KotlinTest { println("批量删除后————————") //直接使用函数查询时需指定from表 query { from(ACCOUNT) }.stream().peek { println(it) }.toList().filter { it.id.rem(3) == 0 }.map { - it.userName = "哈哈" + it.userName = "cloud-player" it - }.batchUpdate() + }.batchUpdateById() println("批量更新后————————") //使用表对象查询时无需指定from表