kotlin orm kotysa笔记
依赖
implementation("org.ufoss.kotysa:kotysa-spring-jdbc:3.2.1")
implementation("org.springframework.data:spring-data-jdbc")
implementation("com.alibaba:druid:1.2.20")
runtimeOnly("org.postgresql:postgresql")
yaml配置
spring:
application:
name: jimmer-demo
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://localhost:5432/t_214
username: postgres
password: postgres
threads:
virtual:
enabled: true
model
import org.ufoss.kotysa.postgresql.PostgresqlTable
import java.util.UUID
data class User2(
val id: UUID,
val username: String,
val password: String,
)
object User2s: PostgresqlTable<User2>("user2") {
val id = uuid(User2::id).primaryKey()
val username = text(User2::username)
val password = text(User2::password)
}
jdbc配置
import com.alibaba.druid.pool.DruidDataSource
import com.example.koyosademo.model.User2s
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.core.env.Environment
import org.springframework.data.jdbc.repository.config.AbstractJdbcConfiguration
import org.springframework.jdbc.core.JdbcOperations
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate
import org.springframework.transaction.support.TransactionTemplate
import org.ufoss.kotysa.spring.jdbc.sqlClient
import org.ufoss.kotysa.spring.jdbc.transaction.transactionalOp
import org.ufoss.kotysa.tables
import javax.sql.DataSource
private val tables = tables().postgresql(User2s)
@Configuration
class KotysaConfig(
val env: Environment
) : AbstractJdbcConfiguration() {
@Bean
fun dataSource(): DataSource {
val dataSource = DruidDataSource()
dataSource.url = env.getProperty("spring.datasource.url")
dataSource.username = env.getProperty("spring.datasource.username")
dataSource.password = env.getProperty("spring.datasource.password")
return dataSource
}
@Bean
fun namedParameterJdbcOperations(dataSource: DataSource): NamedParameterJdbcOperations {
return NamedParameterJdbcTemplate(dataSource)
}
@Bean
fun sqlClient(dbClient: JdbcOperations) = dbClient.sqlClient(tables)
@Bean
fun operator(op: TransactionTemplate) = op.transactionalOp()
}
repository
import com.example.koyosademo.model.User2
import com.example.koyosademo.model.User2s
import org.springframework.stereotype.Repository
import org.ufoss.kotysa.SqlClient
import org.ufoss.kotysa.spring.jdbc.transaction.SpringJdbcTransactionalOp
import java.util.*
@Repository
class User2Repository(
private val client: SqlClient,
private val operator: SpringJdbcTransactionalOp,
) {
fun count() = client selectCountAllFrom User2s
fun findAll() = client selectAllFrom User2s
fun findOne(id: UUID) =
(client selectFrom User2s
where User2s.id eq id
).fetchOne()
fun addOne(user: User2) = operator.transactional {
client insert user
}
fun updateOne(user: User2) = operator.transactional {
(client update User2s
set User2s.username eq user.username
set User2s.password eq user.password
where User2s.id eq user.id
).execute()
}
fun delete(id: UUID) =
(client deleteFrom User2s
where User2s.id eq id
).execute()
}
biz
import com.example.koyosademo.model.User2
import com.example.koyosademo.repository.User2Repository
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
import java.util.*
@RestController
@RequestMapping("u")
class DemoService(
val user2Repository: User2Repository
) {
@GetMapping
fun list() = user2Repository.findAll()
@GetMapping("{id}")
fun getOne(@PathVariable id: String)
= user2Repository.findOne(UUID.fromString(id))
@PostMapping
fun addOne(@RequestBody user: UserDto)
= user2Repository.addOne(User2(UUID.randomUUID(), user.username, user.password))
@PutMapping
fun updateOne(@RequestBody user: User2)
= user2Repository.updateOne(user)
@DeleteMapping("{id}")
fun removeOne(@PathVariable id: String)
= user2Repository.delete(UUID.fromString(id))
@GetMapping("count")
fun count() = user2Repository.count()
}
data class UserDto(
val username: String,
val password: String
)
reference
本文作者:七つ一旋桜
本文链接:https://www.cnblogs.com/poifa/p/17862637.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步