Skip to content

Commit b54e7da

Browse files
committed
增加Kotlin演示工程.
1 parent 3e1c936 commit b54e7da

File tree

11 files changed

+333
-1
lines changed

11 files changed

+333
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
- mybatis-plus-sample-association: 联表查询示例
3737
- mybatis-plus-sample-jsonb: 数据库 postgres 字段 jsonb 示例
3838
- mybatis-plus-startup-analysis : 启动性能测试示例
39-
39+
- mybatis-plus-sample-kotlin : Kotlin工程示例
4040

4141
![微信 wx153666](https://images.gitee.com/uploads/images/2021/0903/235825_2d017339_12260.jpeg)
4242

mybatis-plus-sample-kotlin/pom.xml

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>com.baomidou</groupId>
6+
<artifactId>mybatis-plus-samples</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
</parent>
9+
10+
<artifactId>mybatis-plus-sample-kotlin</artifactId>
11+
12+
<properties>
13+
<kotlin.version>2.1.0</kotlin.version>
14+
</properties>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.springframework.boot</groupId>
19+
<artifactId>spring-boot-starter</artifactId>
20+
</dependency>
21+
<dependency>
22+
<groupId>com.h2database</groupId>
23+
<artifactId>h2</artifactId>
24+
<scope>runtime</scope>
25+
</dependency>
26+
<dependency>
27+
<groupId>com.baomidou</groupId>
28+
<artifactId>${mybatisplus.artifactId}</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.jetbrains.kotlin</groupId>
32+
<artifactId>kotlin-reflect</artifactId>
33+
</dependency>
34+
</dependencies>
35+
36+
<profiles>
37+
<profile>
38+
<id>spring-boot2</id>
39+
<dependencies>
40+
<dependency>
41+
<groupId>org.jetbrains.kotlin</groupId>
42+
<artifactId>kotlin-stdlib-jdk8</artifactId>
43+
</dependency>
44+
</dependencies>
45+
<build>
46+
<plugins>
47+
<plugin>
48+
<groupId>org.jetbrains.kotlin</groupId>
49+
<artifactId>kotlin-maven-plugin</artifactId>
50+
<version>${kotlin.version}</version>
51+
<configuration>
52+
<args>
53+
<arg>-Xjsr305=strict</arg>
54+
<arg>-Xjvm-default=all</arg>
55+
</args>
56+
</configuration>
57+
</plugin>
58+
</plugins>
59+
</build>
60+
</profile>
61+
62+
<profile>
63+
<id>spring-boot3</id>
64+
<dependencies>
65+
<dependency>
66+
<groupId>org.jetbrains.kotlin</groupId>
67+
<artifactId>kotlin-stdlib</artifactId>
68+
</dependency>
69+
</dependencies>
70+
<build>
71+
<plugins>
72+
<plugin>
73+
<groupId>org.jetbrains.kotlin</groupId>
74+
<artifactId>kotlin-maven-plugin</artifactId>
75+
<version>${kotlin.version}</version>
76+
<configuration>
77+
<jvmTarget>17</jvmTarget>
78+
<args>
79+
<arg>-Xjsr305=strict</arg>
80+
<arg>-Xjvm-default=all</arg>
81+
</args>
82+
<compilerPlugins>
83+
<plugin>spring</plugin>
84+
</compilerPlugins>
85+
</configuration>
86+
<!-- <dependencies>-->
87+
<!-- <dependency>-->
88+
<!-- <groupId>org.jetbrains.kotlin</groupId>-->
89+
<!-- <artifactId>kotlin-maven-allopen</artifactId>-->
90+
<!-- <version>${kotlin.version}</version>-->
91+
<!-- </dependency>-->
92+
<!-- </dependencies>-->
93+
</plugin>
94+
</plugins>
95+
</build>
96+
</profile>
97+
</profiles>
98+
99+
<build>
100+
<plugins>
101+
<plugin>
102+
<groupId>org.springframework.boot</groupId>
103+
<artifactId>spring-boot-maven-plugin</artifactId>
104+
<configuration>
105+
<mainClass>com.baomidou.mybatisplus.samples.kotlin.Application</mainClass>
106+
</configuration>
107+
<executions>
108+
<execution>
109+
<id>repackage</id>
110+
<goals>
111+
<goal>repackage</goal>
112+
</goals>
113+
</execution>
114+
</executions>
115+
</plugin>
116+
</plugins>
117+
</build>
118+
119+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baomidou.mybatisplus.samples.kotlin.config
2+
3+
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler
4+
import org.apache.ibatis.reflection.MetaObject
5+
import org.slf4j.LoggerFactory
6+
import org.springframework.stereotype.Component
7+
import java.time.LocalDateTime
8+
9+
/**
10+
* @author nieqiurong
11+
*/
12+
@Component
13+
class MyMetaObjectHandler : MetaObjectHandler {
14+
15+
private val log = LoggerFactory.getLogger(MyMetaObjectHandler::class.java)
16+
17+
// 注意将kotlin类型转为java类型请使用 xxx::class.javaObjectType,防止部分类型使用xxx::class.java转换为基本类型导致类型不一致无法填充
18+
override fun insertFill(metaObject: MetaObject) {
19+
log.info("开始插入填充...");
20+
this.strictInsertFill(metaObject, "createUserId", Long::class.javaObjectType, 1111L)
21+
this.strictInsertFill(metaObject, "createTime", LocalDateTime::class.javaObjectType, LocalDateTime.now())
22+
}
23+
24+
override fun updateFill(metaObject: MetaObject) {
25+
log.info("开始更新填充...");
26+
this.strictUpdateFill(metaObject, "updateUserId", Long::class.javaObjectType, 6666L)
27+
this.strictUpdateFill(metaObject, "updateTime", LocalDateTime::class.javaObjectType, LocalDateTime.now())
28+
}
29+
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baomidou.mybatisplus.samples.kotlin.config
2+
3+
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor
4+
import org.mybatis.spring.annotation.MapperScan
5+
import org.springframework.context.annotation.Bean
6+
import org.springframework.context.annotation.Configuration
7+
8+
/**
9+
* @author nieqiurong
10+
*/
11+
@Configuration
12+
@MapperScan("com.baomidou.mybatisplus.samples.kotlin.mapper")
13+
open class MybatisPlusConfig {
14+
15+
@Bean
16+
open fun paginationInnerInterceptor(): PaginationInnerInterceptor {
17+
return PaginationInnerInterceptor();
18+
}
19+
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baomidou.mybatisplus.samples.kotlin.entity
2+
3+
import com.baomidou.mybatisplus.annotation.FieldFill
4+
import com.baomidou.mybatisplus.annotation.TableField
5+
import com.baomidou.mybatisplus.annotation.TableName
6+
import java.time.LocalDateTime
7+
8+
/**
9+
* @author nieqiurong
10+
*/
11+
@TableName("sys_user")
12+
class User {
13+
14+
var id: Long? = null
15+
16+
var name: String? = null
17+
18+
var age: Int? = null
19+
20+
var email: String? = null
21+
22+
@TableField(exist = false)
23+
var ignoreColumn = "ignoreColumn"
24+
25+
@TableField(fill = FieldFill.INSERT)
26+
var createUserId: Long? = null
27+
28+
@TableField(fill = FieldFill.INSERT)
29+
var createTime: LocalDateTime? = null
30+
31+
@TableField(fill = FieldFill.UPDATE)
32+
var updateUserId: Long? = null
33+
34+
@TableField(fill = FieldFill.UPDATE)
35+
var updateTime: LocalDateTime? = null
36+
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baomidou.mybatisplus.samples.kotlin.mapper
2+
3+
import com.baomidou.mybatisplus.core.mapper.BaseMapper
4+
import com.baomidou.mybatisplus.samples.kotlin.entity.User
5+
import org.apache.ibatis.annotations.Mapper
6+
7+
/**
8+
* @author nieqiurong
9+
*/
10+
@Mapper
11+
interface UserMapper : BaseMapper<User> {
12+
13+
fun user(id: Int): User {
14+
return selectById(id)
15+
}
16+
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
spring:
2+
datasource:
3+
url: jdbc:h2:mem:test
4+
username: root
5+
password: test
6+
sql:
7+
init:
8+
schema-locations: classpath:db/schema-h2.sql
9+
data-locations: classpath:db/data-h2.sql
10+
mode: always
11+
12+
logging:
13+
level:
14+
com.baomidou.mybatisplus.samples: debug
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
DELETE FROM sys_user;
2+
3+
INSERT INTO sys_user (id, name, age, email) VALUES
4+
(1, 'Jone', 18, '[email protected]'),
5+
(2, 'Jack', 20, '[email protected]'),
6+
(3, 'Tom', 28, '[email protected]'),
7+
(4, 'Sandy', 21, '[email protected]'),
8+
(5, 'Billie', 24, '[email protected]');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
DROP TABLE IF EXISTS sys_user;
2+
3+
CREATE TABLE sys_user
4+
(
5+
id BIGINT NOT NULL COMMENT '主键ID',
6+
name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
7+
age INT NULL DEFAULT NULL COMMENT '年龄',
8+
email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
9+
create_user_id BIGINT NULL DEFAULT NULL COMMENT '创建人ID',
10+
create_time TIMESTAMP NULL DEFAULT NULL COMMENT '创建时间',
11+
update_user_id BIGINT NULL DEFAULT NULL COMMENT '更新人ID',
12+
update_time TIMESTAMP NULL DEFAULT NULL COMMENT '更新时间',
13+
PRIMARY KEY (id)
14+
);
15+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.baomidou.mybatisplus.samples.kotlin
2+
3+
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
4+
import com.baomidou.mybatisplus.samples.kotlin.entity.User
5+
import com.baomidou.mybatisplus.samples.kotlin.mapper.UserMapper
6+
import org.junit.jupiter.api.Assertions
7+
import org.junit.jupiter.api.Test
8+
import org.slf4j.LoggerFactory
9+
import org.springframework.beans.factory.annotation.Autowired
10+
import org.springframework.boot.test.context.SpringBootTest
11+
12+
/**
13+
* @author nieqiurong
14+
*/
15+
@SpringBootTest
16+
class ApplicationTests {
17+
18+
private val logger = LoggerFactory.getLogger(ApplicationTests::class.java)
19+
20+
@Autowired
21+
private lateinit var userMapper: UserMapper
22+
23+
@Test
24+
fun test() {
25+
logger.info("--------------演示分页查询--------------------")
26+
val page = userMapper.selectPage(Page(1, 3), null)
27+
Assertions.assertEquals(5, page.total)
28+
Assertions.assertEquals(3, page.size)
29+
val insertUser = User()
30+
insertUser.name = "demo"
31+
insertUser.age = 10
32+
insertUser.email = "[email protected]"
33+
logger.info("--------------演示写入--------------------")
34+
Assertions.assertTrue(userMapper.insert(insertUser) > 0)
35+
Assertions.assertNotNull(insertUser.createUserId)
36+
Assertions.assertNotNull(insertUser.createTime)
37+
Assertions.assertNull(insertUser.updateTime)
38+
Assertions.assertNull(insertUser.updateTime)
39+
logger.info("--------------演示查询--------------------")
40+
val selectUser = userMapper.selectById(insertUser.id)
41+
Assertions.assertNotNull(selectUser)
42+
Assertions.assertEquals("demo", selectUser.name)
43+
Assertions.assertEquals("[email protected]", selectUser.email)
44+
Assertions.assertEquals(10, selectUser.age)
45+
logger.info("--------------演示更新--------------------")
46+
val updateUser = User()
47+
updateUser.id = insertUser.id
48+
updateUser.name = "test"
49+
Assertions.assertTrue(userMapper.updateById(updateUser) > 0)
50+
val selectUpdateUser = userMapper.selectById(insertUser.id)
51+
Assertions.assertNotNull(selectUpdateUser)
52+
Assertions.assertEquals("test", selectUpdateUser.name)
53+
Assertions.assertEquals("[email protected]", selectUpdateUser.email)
54+
Assertions.assertEquals(10, selectUser.age)
55+
Assertions.assertNotNull(selectUpdateUser.createUserId)
56+
Assertions.assertNotNull(selectUpdateUser.createTime)
57+
Assertions.assertNotNull(selectUpdateUser.updateTime)
58+
Assertions.assertNotNull(selectUpdateUser.updateTime)
59+
logger.info("--------------演示删除--------------------")
60+
Assertions.assertTrue(userMapper.deleteById(insertUser.id) > 0)
61+
Assertions.assertNull(userMapper.selectById(insertUser.id))
62+
logger.info("--------------演示default方法调用--------------------")
63+
val user = userMapper.user(1)
64+
Assertions.assertNotNull(user)
65+
}
66+
67+
}

pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
<module>mybatis-plus-sample-pagehelper</module>
4242
<module>mybatis-plus-sample-association</module>
4343
<module>mybatis-plus-sample-jsonb</module>
44+
<module>mybatis-plus-sample-kotlin</module>
4445
</modules>
4546

4647
<properties>
@@ -103,6 +104,8 @@
103104
<properties>
104105
<mybatisplus.artifactId>mybatis-plus-spring-boot3-starter</mybatisplus.artifactId>
105106
<java.version>17</java.version>
107+
<maven.compiler.source>17</maven.compiler.source>
108+
<maven.compiler.target>17</maven.compiler.target>
106109
</properties>
107110
<dependencies>
108111
<dependency>
@@ -142,6 +145,8 @@
142145
<properties>
143146
<mybatisplus.artifactId>mybatis-plus-boot-starter</mybatisplus.artifactId>
144147
<java.version>8</java.version>
148+
<maven.compiler.source>1.8</maven.compiler.source>
149+
<maven.compiler.target>1.8</maven.compiler.target>
145150
</properties>
146151
<dependencies>
147152
<dependency>

0 commit comments

Comments
 (0)