首页 > 其他分享 >kotlin orm kotysa笔记

kotlin orm kotysa笔记

时间:2023-11-28 18:25:07浏览次数:35  
标签:jdbc val kotlin kotysa springframework orm org import

依赖

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

相关文章

  • C#winform备件管理系统项目
    1,该项目开发环境介绍:该项目采用visualstudio 2019 , 控件DevExpress  ,数据库SQLserver2019。2:项目截图展示3:该项目包含以下功能该项目主要包含以下功能:备件新增,入库,出库,信息维护,领用,部门管理,人员管理,权限设置,设备管理,数据库备份,系统日志,备件盘点。界面皮肤自由切换,数据导入......
  • Meta对Transformer架构下手了:新注意力机制更懂推理
    前言 作者表示,这种全新注意力机制(Sytem2Attention)或许你也需要呢。本文转载自机器之心仅用于学术分享,若侵权请联系删除欢迎关注公众号CV技术指南,专注于计算机视觉的技术总结、最新技术跟踪、经典论文解读、CV招聘信息。CV各大方向专栏与各个部署框架最全教程整理【CV技术......
  • hackthebox format medium walkthrough
    walkthough 1.Wemustbrowsethewebsiteandlookupthebusinesspointforthewebpage.atthisboxwecanfindthecoderepository.codeauditinganddiscoveringtheprivilegeescalatedthroughtheRedisUnixsockvulnerability.2.Afterprivilegeescalat......
  • Kotlin Notes - 5
    InKotlin,thetypesystemdistinguishesbetweenreferencesthatcanholdnull(nullablereferences)andthosethatcannot(non-nullablereferences).Forexample,aregularvariableoftypeStringcannotholdnull:vara:String="abc"//Regul......
  • DBV-00107: Unknown header format 故障处理---惜分飞
    联系:手机/微信(+8617813235971)QQ(107644445)标题:DBV-00107:Unknownheaderformat故障处理作者:惜分飞©版权所有[未经本人同意,不得以任何形式转载,否则有进一步追究法律责任的权利.]客户linux平台被勒索病毒加密,其中有oracle数据库.客户联系黑客进行解密【勒索解密oracl......
  • 字符串格式化站位 ——format
    s='helloworld'print('{0:*<20}'.format(s))#0是format的索引并且format的元素只有一个,输出字符串左对齐,右边补充以20为单位的*#结果为:helloworld**********print('{0:*>20}'.format(s))#结果为:**********helloworldprint('{0:*^20}'.format(s))#输出结果为:****......
  • 【Python】Formatter预定义的字段有哪些
    字段描述namelogger名字levelno日志级别数字levelname日志级别字符串pathname打印日志文件路径filename打印日志文件名module模块名lineno日志调用行数funcName日志调用所在函数名created消息创建时的时间对象(time.time())asctime消......
  • MySQL8.x 中 performance_schema 下 processlist表的说明
    MySQL8.x中performance_schema下processlist表的说明最近在研究一个MySQL数据库的监控相关功能的系统的实现,因此专门研究了一下processlist表。processlist表为MySQL的核心表之一。MySQLprocesslist表示当前由服务器内执行的线程集执行的操作。进程列表表是进程信息的来......
  • Kotlin协程系列(二)
    在进行业务开发时,我们通常会基于官方的协程框架(kotlinx.coroutines)来运用Kotlin协程优化异步逻辑,不过这个框架过于庞大和复杂,如果直接接触它容易被劝退。所以,为了我们在后续的学习中游刃有余,在使用官方给出的复合协程时能够胸有成竹,我们暂且抛开它,按照它的思路实现一个轻量......
  • Golang GORM 数据库操作
    一.初始化连接1packagemodel23import(4"fmt"56"gorm.io/driver/mysql"7"gorm.io/gorm"8)910/***11Navicat数据库可视化工具:https://www.navicat.com/en/12GORM操作数据库的Golang库:https://gorm.io/......