项目作者: komapper

项目描述 :
Kotlin SQL Mapper
高级语言: Kotlin
项目地址: git://github.com/komapper/komapper.git
创建时间: 2021-03-21T05:35:15Z
项目社区:https://github.com/komapper/komapper

开源协议:Apache License 2.0

下载


Komapper: Kotlin ORM for JDBC and R2DBC

Build
Twitter
Slack
Maven Central

Komapper is an ORM library for server-side Kotlin.

For more documentation, go to our site:

Features

Highlighted

  • Support for both JDBC and R2DBC
  • Code generation at compile-time using Kotlin Symbol Processing API
  • Immutable and composable queries
  • Support for Kotlin value classes
  • Easy Spring Boot integration

Experimental

  • Quarkus integration
  • Transaction management using context receivers

Prerequisite

  • Kotlin 1.9.24 or later
  • JRE 11 or later
  • Gradle 6.8.3 or later

Supported Databases

Komapper is tested with the following databases:

Database version JDBC support R2DBC support
H2 Database 2.3.232 v v
MariaDB 10.6.3 v v
MySQL v5.x 5.7.44 v v
MySQL v8.x 8.0.25 v v
Oracle Database XE 18.4.0 v v
PostgreSQL 12.9 v v
SQL Server 2019 v (unstable)

Supported connectivity types:

  • JDBC 4.3
  • R2DBC 1.0.0

Installation

Add the following code to the Gradle build script (gradle.build.kts).

  1. plugins {
  2. kotlin("jvm") version "2.1.10"
  3. id("com.google.devtools.ksp") version "2.1.10-1.0.31"
  4. }
  5. val komapperVersion = "5.2.1"
  6. dependencies {
  7. platform("org.komapper:komapper-platform:$komapperVersion").let {
  8. implementation(it)
  9. ksp(it)
  10. }
  11. implementation("org.komapper:komapper-starter-jdbc")
  12. implementation("org.komapper:komapper-dialect-h2-jdbc")
  13. ksp("org.komapper:komapper-processor")
  14. }

See also Quickstart for more details:

Sample code

To get complete code, go to our example repository.

Entity class definition

  1. @KomapperEntity
  2. data class Address(
  3. @KomapperId
  4. @KomapperAutoIncrement
  5. @KomapperColumn(name = "ADDRESS_ID")
  6. val id: Int = 0,
  7. val street: String,
  8. @KomapperVersion
  9. val version: Int = 0,
  10. @KomapperCreatedAt
  11. val createdAt: LocalDateTime? = null,
  12. @KomapperUpdatedAt
  13. val updatedAt: LocalDateTime? = null,
  14. )

Connecting with JDBC

  1. fun main() {
  2. // create a Database instance
  3. val db = JdbcDatabase("jdbc:h2:mem:example;DB_CLOSE_DELAY=-1")
  4. // get a metamodel
  5. val a = Meta.address
  6. // execute simple operations in a transaction
  7. db.withTransaction {
  8. // create a schema
  9. db.runQuery {
  10. QueryDsl.create(a)
  11. }
  12. // INSERT
  13. val newAddress = db.runQuery {
  14. QueryDsl.insert(a).single(Address(street = "street A"))
  15. }
  16. // SELECT
  17. val address = db.runQuery {
  18. QueryDsl.from(a).where { a.id eq newAddress.id }.first()
  19. }
  20. }
  21. }

Connecting with R2DBC

  1. suspend fun main() {
  2. // create a Database instance
  3. val db = R2dbcDatabase("r2dbc:h2:mem:///example;DB_CLOSE_DELAY=-1")
  4. // get a metamodel
  5. val a = Meta.address
  6. // execute simple operations in a transaction
  7. db.withTransaction {
  8. // create a schema
  9. db.runQuery {
  10. QueryDsl.create(a)
  11. }
  12. // INSERT
  13. val newAddress = db.runQuery {
  14. QueryDsl.insert(a).single(Address(street = "street A"))
  15. }
  16. // SELECT
  17. val address = db.runQuery {
  18. QueryDsl.from(a).where { a.id eq newAddress.id }.first()
  19. }
  20. }
  21. }

Design Policy

See DESIGN_DOC for the design policy of this project.

Compatibility Matrix

Kotlin and KSP version Komapper version KSP2 support? JRE min version Gradle min version
1.9.24-1.0.20 1.18.1 - 2.x No 11 6.8.3
2.0.21-1.0.28 2.x - 5.x Yes 11 7.6.4
2.1.10-1.0.31 5.x Yes 11 7.6.4

Compatibility testing is performed in the komapper/compatibility-test repository.

Backers

If you use Komapper in your project or enterprise and would like to support ongoing development,
please consider becoming a backer. [Become a backer]

Our top backers are shown below!