diff --git a/mybatis-flex-kotlin/pom.xml b/mybatis-flex-kotlin/pom.xml index ec43b3be..ddfac86d 100755 --- a/mybatis-flex-kotlin/pom.xml +++ b/mybatis-flex-kotlin/pom.xml @@ -6,7 +6,7 @@ com.mybatis-flex parent - 1.4.8 + 1.5.6 mybatis-flex-kotlin @@ -22,73 +22,33 @@ com.mybatis-flex mybatis-flex-core - 1.5.6 - - - org.jetbrains.kotlin - kotlin-test - ${kotlin.version} - test + ${mybatis-flex.version} + org.jetbrains.kotlin kotlin-stdlib-jdk8 ${kotlin.version} - compile + provided - - org.testng - testng - 7.7.1 - test - - - org.springframework - spring-jdbc - test - - - com.h2database - h2 - 2.1.214 - test - + ${project.basedir}/src/main/kotlin + ${project.basedir}/src/test/kotlin org.jetbrains.kotlin kotlin-maven-plugin ${kotlin.version} - 1.8 -Xjvm-default=all + 1.8 - - kapt - - kapt - - - - src/main/kotlin - src/main/java - - - - - com.google.dagger - dagger-compiler - 2.9 - - - - compile process-sources @@ -96,6 +56,7 @@ compile + test-compile test-compile @@ -108,5 +69,4 @@ - diff --git a/mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/db/DbConfig.kt b/mybatis-flex-kotlin/src/main/kotlin/com/mybatisflex/kotlin/db/DbConfig.kt similarity index 100% rename from mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/db/DbConfig.kt rename to mybatis-flex-kotlin/src/main/kotlin/com/mybatisflex/kotlin/db/DbConfig.kt diff --git a/mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/entry/Entry.kt b/mybatis-flex-kotlin/src/main/kotlin/com/mybatisflex/kotlin/entry/Entry.kt similarity index 100% rename from mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/entry/Entry.kt rename to mybatis-flex-kotlin/src/main/kotlin/com/mybatisflex/kotlin/entry/Entry.kt diff --git a/mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/extensions/db/DbExtensions.kt b/mybatis-flex-kotlin/src/main/kotlin/com/mybatisflex/kotlin/extensions/db/DbExtensions.kt similarity index 100% rename from mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/extensions/db/DbExtensions.kt rename to mybatis-flex-kotlin/src/main/kotlin/com/mybatisflex/kotlin/extensions/db/DbExtensions.kt diff --git a/mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/extensions/entry/EntryExtensions.kt b/mybatis-flex-kotlin/src/main/kotlin/com/mybatisflex/kotlin/extensions/entry/EntryExtensions.kt similarity index 100% rename from mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/extensions/entry/EntryExtensions.kt rename to mybatis-flex-kotlin/src/main/kotlin/com/mybatisflex/kotlin/extensions/entry/EntryExtensions.kt diff --git a/mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/extensions/mapper/MaaperExtensions.kt b/mybatis-flex-kotlin/src/main/kotlin/com/mybatisflex/kotlin/extensions/mapper/MaaperExtensions.kt similarity index 100% rename from mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/extensions/mapper/MaaperExtensions.kt rename to mybatis-flex-kotlin/src/main/kotlin/com/mybatisflex/kotlin/extensions/mapper/MaaperExtensions.kt diff --git a/mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/extensions/sql/SqlExtensions.kt b/mybatis-flex-kotlin/src/main/kotlin/com/mybatisflex/kotlin/extensions/sql/SqlExtensions.kt similarity index 100% rename from mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/extensions/sql/SqlExtensions.kt rename to mybatis-flex-kotlin/src/main/kotlin/com/mybatisflex/kotlin/extensions/sql/SqlExtensions.kt diff --git a/mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/scope/BootstrapScope.kt b/mybatis-flex-kotlin/src/main/kotlin/com/mybatisflex/kotlin/scope/BootstrapScope.kt similarity index 100% rename from mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/scope/BootstrapScope.kt rename to mybatis-flex-kotlin/src/main/kotlin/com/mybatisflex/kotlin/scope/BootstrapScope.kt diff --git a/mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/scope/QueryScope.kt b/mybatis-flex-kotlin/src/main/kotlin/com/mybatisflex/kotlin/scope/QueryScope.kt similarity index 100% rename from mybatis-flex-kotlin/src/main/java/com/mybatisflex/kotlin/scope/QueryScope.kt rename to mybatis-flex-kotlin/src/main/kotlin/com/mybatisflex/kotlin/scope/QueryScope.kt diff --git a/mybatis-flex-kotlin/src/test/java/com/mybatisflex/kotlintest/ExtensionTest.kt b/mybatis-flex-kotlin/src/test/java/com/mybatisflex/kotlintest/ExtensionTest.kt deleted file mode 100755 index d3fc59a4..00000000 --- a/mybatis-flex-kotlin/src/test/java/com/mybatisflex/kotlintest/ExtensionTest.kt +++ /dev/null @@ -1,111 +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.kotlintest - -import com.mybatisflex.core.BaseMapper -import com.mybatisflex.core.FlexConsts -import com.mybatisflex.core.audit.AuditManager -import com.mybatisflex.core.audit.ConsoleMessageCollector -import com.mybatisflex.kotlin.entry.Entry -import com.mybatisflex.kotlin.extensions.db.* -import com.mybatisflex.kotlin.extensions.entry.* -import com.mybatisflex.kotlin.extensions.mapper.queryList -import com.mybatisflex.kotlin.extensions.sql.* -import com.mybatisflex.kotlin.scope.buildBootstrap -import com.mybatisflex.kotlintest.entry.Account -import com.mybatisflex.kotlintest.entry.table.AccountTableDef.ACCOUNT -import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder - -import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType -import javax.sql.DataSource -import kotlin.streams.toList - - -fun main() { - val dataSource: DataSource = EmbeddedDatabaseBuilder() - .setType(EmbeddedDatabaseType.H2) - .addScript("schema.sql") - .addScript("data.sql") - .build() - - AuditManager.setAuditEnable(true) - AuditManager.setMessageCollector(ConsoleMessageCollector()) - - buildBootstrap { - + AccountMapper::class.java - dataSources { -// dataSource(FlexConsts.NAME,dataSource) - FlexConsts.NAME of dataSource -// "dataSource1" of dataSource -// "dataSource2" of dataSource - } -// + dataSource - }.start() - - 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) - - println("保存后————————") - mapper().findByAge(18,1,2).stream().peek { println(it) }.forEach(Entry::deleteById) - - println("删除后————————") - ACCOUNT.all().stream().peek { println(it) }.map { it.userName = "sa" - it }.forEach(Entry::update) - println("更新后————————") - - ACCOUNT.all().stream().peek { println(it) }.map { - it.id = it.id.plus(5) - it.userName = "423423" - it }.toList().batchInsert() - - println("批量插入后————————") - ACCOUNT.all().stream().peek { println(it) }.toList().filter { it.id.rem(2) == 0 }.batchDeleteById() - - println("批量删除后————————") - //直接使用函数查询时需指定from表 - query {from(ACCOUNT)}.stream().peek { println(it) }.toList().filter { it.id.rem(3) == 0 }.map { it.userName = "哈哈" - it }.batchUpdate() - - println("批量更新后————————") - //使用表对象查询时无需指定from表 - ACCOUNT.query {}.forEach(::println) -} - - - -//接口里面写方法时打这个注解才能生成Default方法否则会单独生成一个类导致报错 -@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) { - ID `in` ids.asList() - } - } - } - -} - diff --git a/mybatis-flex-kotlin/src/test/resources/schema.sql b/mybatis-flex-kotlin/src/test/resources/schema.sql deleted file mode 100755 index 40881a74..00000000 --- a/mybatis-flex-kotlin/src/test/resources/schema.sql +++ /dev/null @@ -1,15 +0,0 @@ -CREATE TABLE IF NOT EXISTS `tb_account` -( - `id` INTEGER PRIMARY KEY, - `u_name` VARCHAR(100) NOT NULL, - `age` INTEGER, - `birthday` DATETIME -); - -CREATE TABLE IF NOT EXISTS `tb_account1` -( - `id` INTEGER PRIMARY KEY, - `u_name` VARCHAR(100) NOT NULL, - `age` INTEGER, - `birthday` DATETIME -); diff --git a/mybatis-flex-test/mybatis-flex-spring-kotlin-test/.gitignore b/mybatis-flex-test/mybatis-flex-spring-kotlin-test/.gitignore new file mode 100755 index 00000000..5ff6309b --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-kotlin-test/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/mybatis-flex-test/mybatis-flex-spring-kotlin-test/pom.xml b/mybatis-flex-test/mybatis-flex-spring-kotlin-test/pom.xml new file mode 100755 index 00000000..45b4b173 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-kotlin-test/pom.xml @@ -0,0 +1,134 @@ + + + 4.0.0 + + com.mybatis-flex + mybatis-flex-test + 1.5.6 + + + mybatis-flex-spring-kotlin-test + + + 8 + 8 + UTF-8 + 1.9.0 + + + + + com.mybatis-flex + mybatis-flex-kotlin + ${mybatis-flex.version} + + + com.mybatis-flex + mybatis-flex-core + ${mybatis-flex.version} + + + + com.mybatis-flex + mybatis-flex-spring + ${mybatis-flex.version} + + + + org.springframework + spring-jdbc + + + + org.yaml + snakeyaml + 2.0 + + + + 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 + + + org.jetbrains.kotlin + kotlin-maven-plugin + ${kotlin.version} + + + + -Xjvm-default=all + + + + + kapt + + kapt + + + ${project.basedir}/src/test/kotlin + + ${project.basedir}/src/main/kotlin + + + + + com.mybatis-flex + mybatis-flex-processor + ${mybatis-flex.version} + + + + + + + compile + + compile + + + + + test-compile + + test-compile + + + + + + + diff --git a/mybatis-flex-test/mybatis-flex-spring-kotlin-test/src/main/kotlin/com/mybatisflex/test/AppConfig.kt b/mybatis-flex-test/mybatis-flex-spring-kotlin-test/src/main/kotlin/com/mybatisflex/test/AppConfig.kt new file mode 100755 index 00000000..3e71a6c2 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-kotlin-test/src/main/kotlin/com/mybatisflex/test/AppConfig.kt @@ -0,0 +1,65 @@ +/* + * 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.test + +import com.mybatisflex.core.mybatis.FlexConfiguration +import com.mybatisflex.spring.FlexSqlSessionFactoryBean +import org.apache.ibatis.logging.stdout.StdOutImpl +import org.apache.ibatis.session.SqlSessionFactory +import org.mybatis.spring.SqlSessionFactoryBean +import org.mybatis.spring.annotation.MapperScan +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.context.event.ContextStartedEvent +import org.springframework.context.event.EventListener +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType +import javax.sql.DataSource + +@Configuration +@MapperScan("com.mybatisflex.test.mapper") +open class AppConfig { + + + @Bean + open fun dataSource(): DataSource? { + return EmbeddedDatabaseBuilder() + .setType(EmbeddedDatabaseType.H2) + .addScript("schema.sql") + .addScript("data-kt.sql") + .build() + } + + @Bean + open fun sqlSessionFactory(dataSource: DataSource): SqlSessionFactory? { + val factoryBean: SqlSessionFactoryBean = FlexSqlSessionFactoryBean() + factoryBean.setDataSource(dataSource) + val configuration = FlexConfiguration() + configuration.logImpl = StdOutImpl::class.java + factoryBean.setConfiguration(configuration) + return factoryBean.getObject() + } + + @EventListener(classes = [ContextStartedEvent::class]) + open fun handleContextStartedEvent() { + println("handleContextStartedEvent listener invoked!") + } + + + +} + + 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 new file mode 100755 index 00000000..da9f80d4 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-kotlin-test/src/main/kotlin/com/mybatisflex/test/mapper/AccountMapper.kt @@ -0,0 +1,41 @@ +/* + * 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.test.mapper + +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) { + ID `in` ids.asList() + } + } + } + +} + diff --git a/mybatis-flex-kotlin/src/test/java/com/mybatisflex/kotlintest/entry/Account.kt b/mybatis-flex-test/mybatis-flex-spring-kotlin-test/src/main/kotlin/com/mybatisflex/test/model/Account.kt similarity index 89% rename from mybatis-flex-kotlin/src/test/java/com/mybatisflex/kotlintest/entry/Account.kt rename to mybatis-flex-test/mybatis-flex-spring-kotlin-test/src/main/kotlin/com/mybatisflex/test/model/Account.kt index 8380f0ce..41d8872e 100755 --- a/mybatis-flex-kotlin/src/test/java/com/mybatisflex/kotlintest/entry/Account.kt +++ b/mybatis-flex-test/mybatis-flex-spring-kotlin-test/src/main/kotlin/com/mybatisflex/test/model/Account.kt @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.mybatisflex.kotlintest.entry +package com.mybatisflex.test.model import com.mybatisflex.annotation.Column import com.mybatisflex.annotation.Id @@ -21,20 +21,20 @@ import com.mybatisflex.annotation.NoneListener import com.mybatisflex.annotation.Table import com.mybatisflex.kotlin.entry.Entry import java.util.* - /** * 测试用数据类(最好不要写成data class,否则需要与数据库字段数据顺序一致) * @author 卡莫sama(yuanjiashuai) * @date 2023/8/7 */ -@Table(value = "tb_account", onUpdate = [NoneListener::class]) +@Table(value = "tb_account", onUpdate = [NoneListener::class], onSet = [AccountOnSetListener::class]) data class Account( @Id var id: Int, @Column("u_name") var userName: String?, var age: Int?, var birthday: Date?, - ) : Entry(){ +) : Entry(){ 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/kotlin/com/mybatisflex/test/model/AccountOnSetListener.kt b/mybatis-flex-test/mybatis-flex-spring-kotlin-test/src/main/kotlin/com/mybatisflex/test/model/AccountOnSetListener.kt new file mode 100755 index 00000000..606dbac1 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-kotlin-test/src/main/kotlin/com/mybatisflex/test/model/AccountOnSetListener.kt @@ -0,0 +1,27 @@ +/* + * 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.test.model + +import com.mybatisflex.annotation.SetListener + + +class AccountOnSetListener : SetListener { + override fun onSet(entity: Any, property: String, value: Any): Any { + println(">>>>>>> property: $property value:$value") + return value + } +} + diff --git a/mybatis-flex-kotlin/src/test/resources/data.sql b/mybatis-flex-test/mybatis-flex-spring-kotlin-test/src/main/resources/data-kt.sql similarity index 99% rename from mybatis-flex-kotlin/src/test/resources/data.sql rename to mybatis-flex-test/mybatis-flex-spring-kotlin-test/src/main/resources/data-kt.sql index 228b8319..2ee98fe5 100755 --- a/mybatis-flex-kotlin/src/test/resources/data.sql +++ b/mybatis-flex-test/mybatis-flex-spring-kotlin-test/src/main/resources/data-kt.sql @@ -1,4 +1,5 @@ INSERT INTO tb_account VALUES (1, 'Michael Yang', 18, '2020-01-11'); + INSERT INTO tb_account VALUES (2, 'Michael Zhanng', 20, '2020-01-11'); diff --git a/mybatis-flex-kotlin/src/test/resources/mybatis-flex.properties b/mybatis-flex-test/mybatis-flex-spring-kotlin-test/src/main/resources/mybatis-flex.properties similarity index 72% rename from mybatis-flex-kotlin/src/test/resources/mybatis-flex.properties rename to mybatis-flex-test/mybatis-flex-spring-kotlin-test/src/main/resources/mybatis-flex.properties index b25cdd95..929568bf 100755 --- a/mybatis-flex-kotlin/src/test/resources/mybatis-flex.properties +++ b/mybatis-flex-test/mybatis-flex-spring-kotlin-test/src/main/resources/mybatis-flex.properties @@ -1,4 +1,4 @@ processor.mappersGenerateEnable=false -processor.tablesNameStyle=upCase +processor.tablesNameStyle=lowerCase processor.tablesDefSuffix=Def processor.allInTables=true 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 new file mode 100755 index 00000000..f75d140c --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-kotlin-test/src/main/resources/schema.sql @@ -0,0 +1,7 @@ +CREATE TABLE IF NOT EXISTS `tb_account` +( + `id` INTEGER , + `u_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/KotlinSpringTest.kt b/mybatis-flex-test/mybatis-flex-spring-kotlin-test/src/test/kotlin/com/mybatisflex/test/KotlinSpringTest.kt new file mode 100755 index 00000000..1ba41622 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-kotlin-test/src/test/kotlin/com/mybatisflex/test/KotlinSpringTest.kt @@ -0,0 +1,41 @@ +/* + * 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.test + +import com.mybatisflex.test.mapper.AccountMapper +import org.assertj.core.api.WithAssertions +import org.junit.runner.RunWith + +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.test.context.ContextConfiguration +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner +import kotlin.test.Test + + +@RunWith(SpringJUnit4ClassRunner::class) +@ContextConfiguration(classes = [AppConfig::class]) +class KotlinSpringTest : WithAssertions { + @Autowired + lateinit var accountMapper: AccountMapper + + @Test + fun testSelectByQuery() { + val accounts = accountMapper.findByAge(18,2) + accounts.forEach(::println) + } + + +} 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 new file mode 100644 index 00000000..68bf7f2a --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-kotlin-test/src/test/kotlin/com/mybatisflex/test/KotlinTest.kt @@ -0,0 +1,103 @@ +/* + * 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.test + +import com.mybatisflex.core.FlexConsts +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.sql.* +import com.mybatisflex.kotlin.scope.buildBootstrap +import com.mybatisflex.test.mapper.AccountMapper +import com.mybatisflex.test.model.Account +import com.mybatisflex.test.model.table.AccountTableDef.ACCOUNT +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType +import javax.sql.DataSource +import kotlin.streams.toList +import kotlin.test.Test + + +class KotlinTest { + @Test + fun testDb() { + val dataSource: DataSource = EmbeddedDatabaseBuilder() + .setType(EmbeddedDatabaseType.H2) + .addScript("schema.sql") + .addScript("data-kt.sql") + .build() + + AuditManager.setAuditEnable(true) + AuditManager.setMessageCollector(ConsoleMessageCollector()) + + buildBootstrap { + +AccountMapper::class.java + dataSources { +// dataSource(FlexConsts.NAME,dataSource) + FlexConsts.NAME of dataSource +// "dataSource1" of dataSource +// "dataSource2" of dataSource + } +// + dataSource + }.start() +// + ACCOUNT.query {}.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) + + println("保存后————————") + mapper().findByAge(18, 1, 2).stream().peek { println(it) }.forEach(Entry::deleteById) + + println("删除后————————") + ACCOUNT.all().stream().peek { println(it) }.map { + it.userName = "sa" + it + }.forEach(Entry::update) + println("更新后————————") + + ACCOUNT.all().stream().peek { println(it) }.map { + it.id = it.id.plus(5) + it.userName = "423423" + it + }.toList().batchInsert() + + println("批量插入后————————") + ACCOUNT.all().stream().peek { println(it) }.toList().filter { it.id.rem(2) == 0 }.batchDeleteById() + + println("批量删除后————————") + //直接使用函数查询时需指定from表 + query { from(ACCOUNT) }.stream().peek { println(it) }.toList().filter { it.id.rem(3) == 0 }.map { + it.userName = "哈哈" + it + }.batchUpdate() + + println("批量更新后————————") + //使用表对象查询时无需指定from表 + ACCOUNT.query {}.forEach(::println) + } + +} diff --git a/mybatis-flex-test/pom.xml b/mybatis-flex-test/pom.xml index 0643868e..2a7c88c0 100644 --- a/mybatis-flex-test/pom.xml +++ b/mybatis-flex-test/pom.xml @@ -19,6 +19,7 @@ mybatis-flex-spring-test mybatis-flex-spring-boot-test mybatis-flex-spring-cloud-test + mybatis-flex-spring-kotlin-test