添加依赖
plugins {
// 第一步: 添加ksp插件
id("com.google.devtools.ksp") version "1.7.10-1.0.6"
...ommit other plugins...
}
depdencies {
// 第二步: 添加jimmer-sql-kotlin
implementation("org.babyfish.jimmer:jimmer-sql-kotlin:0.1.30")
// 第三步: 应用ksp插件
ksp("org.babyfish.jimmer:jimmer-ksp:0.1.30")
runtimeOnly("com.h2database:h2:2.1.212")
...ommit other dependency...
}
// 第四步: 讲生成的代码添加到编译目录中。
// 没有这个配置,gradle命令仍然可以正常执行,
// 但是, Intellij无法找到生成的源码。
kotlin {
sourceSets.main {
kotlin.srcDir("build/generated/ksp/main/kotlin")
}
}
配置
yaml配置
spring:
application:
name: jimmer-demo
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost/sbfzl?serverTimezone=GMT%2B8
username: root
password: root
server:
port: 8080
配置类
-
json序列化配置
import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.databind.ObjectWriter import org.babyfish.jimmer.jackson.ImmutableModule import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration @Configuration class JacksonConfig { @Bean fun prettyWriter(): ObjectWriter { return ObjectMapper() .registerModule(ImmutableModule()) .writerWithDefaultPrettyPrinter() } // 这个必不可少 @Bean fun setImmutableMode(): ObjectMapper { return ObjectMapper() .registerModule(ImmutableModule()) } }
-
SqlClient配置
import org.babyfish.jimmer.sql.dialect.MySqlDialect import org.babyfish.jimmer.sql.kt.KSqlClient import org.babyfish.jimmer.sql.kt.newKSqlClient import org.slf4j.LoggerFactory import org.springframework.beans.factory.annotation.Value import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration import org.springframework.jdbc.datasource.DataSourceUtils import java.sql.Connection import javax.sql.DataSource @Configuration class SqlClientConfig { private val LOGGER = LoggerFactory.getLogger(SqlClientConfig::class.java) @Bean fun sqlClient( dataSource: DataSource, @Value("\${spring.datasource.url}") jdbcUrl: String, ): KSqlClient = newKSqlClient { setConnectionManager { /* * It's very important to use * "org.springframework.jdbc.datasource.DataSourceUtils"! * This is spring transaction aware ConnectionManager */ val con: Connection = DataSourceUtils.getConnection(dataSource) try { proceed(con) } finally { DataSourceUtils.releaseConnection(con, dataSource) } } setExecutor { /* * Log SQL and variables */ LOGGER.info("Execute sql : \"{}\", with variables: {}", sql, variables) proceed() } setDialect(MySqlDialect()) } }
业务代码
通用返回结果集
data class R (
val code: Int,
val message: String,
val data: Any?,
) {
companion object {
fun ok() = R(200, "success", null)
fun fail() = R(500, "failure", null)
}
fun body(elem: Any?) = this.copy(data = elem)
}
数据实体类
import org.babyfish.jimmer.sql.Entity
import org.babyfish.jimmer.sql.GeneratedValue
import org.babyfish.jimmer.sql.Id
import org.babyfish.jimmer.sql.Table
import org.babyfish.jimmer.sql.meta.IdentityIdGenerator
@Entity
@Table(name = "t_user")
interface TUser {
@GeneratedValue(generatorType = IdentityIdGenerator::class)
@Id
val id: Long
val username: String
val password: String
}
基础CRUD
-
controller
import com.example.dfgfdafgadgdg.service.UserService import com.example.dfgfdafgadgdg.common.R import com.example.dfgfdafgadgdg.model.TUser import org.springframework.web.bind.annotation.DeleteMapping import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.PathVariable import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.PutMapping import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RestController @RestController @RequestMapping("user") class UserController( private val userService: UserService ) { @GetMapping fun get() = R.ok().body(userService.selectAll()) @GetMapping("{id}") fun getById(@PathVariable("id") id: Int) = R.ok().body(userService.selectById(id)) @PostMapping fun add(@RequestBody user: TUser) = R.ok().body(userService.insert(user)) @PutMapping fun update(@RequestBody user: TUser) = R.ok().body(userService.update(user)) @DeleteMapping("{id}") fun remove(@PathVariable("id") id: Int) = R.ok().body(userService.delete(id)) }
-
service
package com.example.dfgfdafgadgdg.service import com.example.dfgfdafgadgdg.model.TUser import org.babyfish.jimmer.sql.kt.KSqlClient import org.springframework.stereotype.Service @Service class UserService( private val sqlClient: KSqlClient ) { fun selectAll() = sqlClient .createQuery(TUser::class) { select(table) }.execute() fun selectById(id: Int) = sqlClient .entities.findById(TUser::class, id) fun insert(user: TUser) = sqlClient .entities .save(user) fun update(user: TUser) = sqlClient .entities .save(user) fun delete(id: Int) = sqlClient .entities.delete(TUser::class, id) }