Komapper is an ORM library for server-side Kotlin.
For more documentation, go to our site:
- 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
- Quarkus integration
- Transaction management using context receivers
- Kotlin 1.7.22 or later
- JRE 11 or later
- Gradle 6.7.1 or later
Komapper is tested with the following databases:
Database | version | JDBC support | R2DBC support |
---|---|---|---|
H2 Database | 2.1.212 | v | v |
MariaDB | 10.6.3 | v | v |
MySQL | 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
Add the following code to the Gradle build script (gradle.build.kts).
plugins {
kotlin("jvm") version "1.9.0"
id("com.google.devtools.ksp") version "1.9.0-1.0.11"
}
val komapperVersion = "1.12.1"
dependencies {
platform("org.komapper:komapper-platform:$komapperVersion").let {
implementation(it)
ksp(it)
}
implementation("org.komapper:komapper-starter-jdbc")
implementation("org.komapper:komapper-dialect-h2-jdbc")
ksp("org.komapper:komapper-processor")
}
See also Quickstart for more details:
To get complete code, go to our example repository.
@KomapperEntity
data class Address(
@KomapperId
@KomapperAutoIncrement
@KomapperColumn(name = "ADDRESS_ID")
val id: Int = 0,
val street: String,
@KomapperVersion
val version: Int = 0,
@KomapperCreatedAt
val createdAt: LocalDateTime? = null,
@KomapperUpdatedAt
val updatedAt: LocalDateTime? = null,
)
fun main() {
// create a Database instance
val db = JdbcDatabase("jdbc:h2:mem:example;DB_CLOSE_DELAY=-1")
// get a metamodel
val a = Meta.address
// execute simple operations in a transaction
db.withTransaction {
// create a schema
db.runQuery {
QueryDsl.create(a)
}
// INSERT
val newAddress = db.runQuery {
QueryDsl.insert(a).single(Address(street = "street A"))
}
// SELECT
val address = db.runQuery {
QueryDsl.from(a).where { a.id eq newAddress.id }.first()
}
}
}
suspend fun main() {
// create a Database instance
val db = R2dbcDatabase("r2dbc:h2:mem:///example;DB_CLOSE_DELAY=-1")
// get a metamodel
val a = Meta.address
// execute simple operations in a transaction
db.withTransaction {
// create a schema
db.runQuery {
QueryDsl.create(a)
}
// INSERT
val newAddress = db.runQuery {
QueryDsl.insert(a).single(Address(street = "street A"))
}
// SELECT
val address = db.runQuery {
QueryDsl.from(a).where { a.id eq newAddress.id }.first()
}
}
}
See DESIGN_DOC for the design policy of this project.
Kotlin and KSP version | Komapper version | JRE min version | Gradle min version |
---|---|---|---|
1.7.22-1.0.8 | 1.3.x - 1.11.x | 11 | 6.7.1 |
1.8.22-1.0.11 | 1.3.x - 1.12.x | 11 | 6.8.3 |
1.9.0-1.0.11 | 1.3.x - 1.12.x | 11 | 6.8.3 |
Compatibility testing is performed in the komapper/compatibility-test repository.
If you use Komapper in your project or enterprise and would like to support ongoing development, please consider becoming a backer or a sponsor. Sponsor logos will show up here with a link to your website.