Version v1.6 of the documentation is no longer actively maintained. The site that you are currently viewing is an archived snapshot. For up-to-date documentation, see the latest version.

Examples

Here are some examples

Overview

We provide several sample applications including JDBC access, R2DBC access, Spring Boot integration, Ktor integration.

Requirements

These are requirements for running the sample applications:

  • JDK 17 or later version
  • Docker (used by some applications)

Get the repository

Clone the komapper/komapper-examples repository.

$ git clone https://github.com/komapper/komapper-examples.git
$ cd komapper-examples

The repository has a multi-project configuration of Gradle. Each subproject is implemented as a sample application.

Sample applications

console-jdbc

This project is a console application that uses JDBC to access the database. To run it, execute the following command:

$ ./gradlew :console-jdbc:run

console-r2dbc

This project is a console application that uses R2DBC to access the database. To run it, execute the following command:

$ ./gradlew :console-r2dbc:run

quarkus-jdbc

This project is a Quarkus web application that uses JDBC to access PostgreSQL database.

SeeREADME for more details.

spring-boot-jdbc

This project is a Spring Boot web application that uses JDBC to access the database. To run it, execute the following command:

$ ./gradlew :spring-boot-jdbc:bootRun

Once the application is running, open http://localhost:8080 in your browser. The message returned from the database will be displayed in your browser.

To add a message to the database, pass it as a query parameter, like http://localhost:8080/?text=Hi. If you open http://localhost:8080 again, you will see the list with the added data.

spring-boot-r2dbc

This project is a Spring Boot web application that uses R2DBC to access the database. To run it, execute the following command:

$ ./gradlew :spring-boot-r2dbc:bootRun

Once the application is running, open http://localhost:8080 in your browser. The message returned from the database will be displayed in your browser.

To add a message to the database, pass it as a query parameter, like http://localhost:8080/?text=Hi. If you open http://localhost:8080 again, you will see the list with the added data.

repository-pattern-jdbc

This project contains an example implementation of the repository pattern.

To run a test that invokes the repository, execute the following command:

$ ./gradlew :repository-pattern-jdbc:check

codegen

This project uses the Gradle plugin. to generate the source code for entity classes from database metadata.

The configuration of the Komapper Gradle plugin is described in the komapper block in the build.gradle.kts file. In this example, we will generate code from MySQL and PostgreSQL.

To generate code from MySQL, run the following command:

$ ./gradlew :codegen:komapperMysqlGenerator

To generate code from PostgreSQL, issue the following command:

$ ./gradlew :codegen:komapperPostgresqlGenerator

To generate code from both MySQL and PostgreSQL at onece, execute the following command:

$ ./gradlew :codegen:komapperGenerator

The generated code will be output under codgen/src/main/kotlin.

comparison-with-exposed

This project is based on the JetBrains Exposed sample code. and rewritten for Komapper.

To run it, execute the following command:

$ ./gradlew :comparison-with-exposed:run

jpetstore

This project is a Spring Boot web application that uses JDBC to access the database.

The application is based on jpetstore-6, which was created by the MyBatis team.

To run it, execute the following command:

$ ./gradlew :jpetstore:bootRun

Once the application is running, open http://localhost:8080 in your browser. Where you are prompted to sign in, you can use the following username and password.

  • username: jpetstore
  • password: jpetstore

kweet

This project is a Spring Boot web application that uses R2DBC to access the database.

The application is based on Kweet, which was created by the Ktor team.

To run it, execute the following command:

$ ./gradlew :kweet:run

Once the application is running, open http://localhost:8080 in your browser.

Last modified December 8, 2022: Update examples page (718b107)