依赖
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.User2s
import org.springframework.stereotype.Repository
import org.ufoss.kotysa.SqlClient
import org.ufoss.kotysa.spring.jdbc.transaction.SpringJdbcTransactionalOp
import java.util.UUID
@Repository
class User2Repository(
private val client: SqlClient,
private val operator: SpringJdbcTransactionalOp,
) {
fun findAll() = client selectAllFrom User2s
fun findOne(id: UUID) =
(client selectFrom User2s
where User2s.id eq id
).fetchOne()
}
biz
import com.example.koyosademo.repository.User2Repository
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
@RestController
@RequestMapping("u")
class DemoService(
val user2Repository: User2Repository
) {
@GetMapping
fun list() = user2Repository.findAll()
}
标签:jdbc,val,kotlin,kotysa,springframework,orm,org,import
From: https://www.cnblogs.com/poifa/p/17862637.html