Kotlin + Quarkus + MyBatis - Build REST CRUD API example

Hello everyone. Today we will learn how to develop REST-style CRUD APIs with Kotlin, Quarkus, MyBatis, and H2 Database. You can download the source code from our Github repository.

Quarkus is a Java framework designed to run within containers. Fixating on expeditious start-up times and low memory utilization makes it more felicitous to run within container orchestration platforms like Kubernetes.Quarkus supports many industry-standard libraries such as Hibernate, Kubernetes, RESTEasy,  Eclipse MicroProfile, and more...

MyBatis is an open-source persistence framework that simplifies the implementation of database access in Java applications. It provides support for custom SQL, stored procedures, and different types of mapping relations. Simply put, it's an alternative to JDBC and Hibernate.

After completing this tutorial what we will build? 

We will build REST API  CRUD features: 
  1. GET - Fetch all User       :     /api/v1/users
  2. GET - Get User by ID     :     /api/v1/users/{id} 
  3. POST - Create User         :     /api/v1/users 
  4. PUT - Edit User Details   :     /api/v1/users/
  5. DELETE - Delete User    :     /api/v1/users/{id}

Technologies used:

  • Quarkus 2.2.3.Final
  • MyBatis 0.0.9
  • Kotlin
  • Gradle
  • H2DB


Project Directory:

Gradle Build(build.gradle.kts):
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm") version "1.5.31"

repositories {

val quarkusPlatformGroupId: String by project
val quarkusPlatformArtifactId: String by project
val quarkusPlatformVersion: String by project

dependencies {

group = "com.knf.dev.demo"
version = "1.0.0-SNAPSHOT"

java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11

tasks.withType<JavaCompile> {
options.encoding = "UTF-8"
val compileKotlin: KotlinCompile by tasks
compileKotlin.kotlinOptions {
jvmTarget = "11"
val compileTestKotlin: KotlinCompile by tasks
compileTestKotlin.kotlinOptions {
jvmTarget = "11"

Database Setup [schema.sql]:

We will create a table called users with a few simple columns. We can initialize a schema by creating a schema.sql file in the resources.
create table users
id integer not null,
firstName varchar(255) not null,
lastName varchar(255) not null,
emailId varchar(255) not null,
primary key(id)


quarkus.datasource.db-kind = h2
quarkus.datasource.username = sa
# quarkus.datasource.password =
quarkus.datasource.jdbc.url = jdbc:h2:mem:test

Create User Model [User.kt]:
package com.knf.dev.demo.model

class User {
var id: Long = 0
var firstName: String? = null
var lastName: String? = null
var emailId: String? = null

constructor() {}
constructor(id: Long, firstName: String?,
lastName: String?, emailId: String?) : super() {
this.id = id
this.firstName = firstName
this.lastName = lastName
this.emailId = emailId

Create User Repository [UserRepository.kt]:
package com.knf.dev.demo.repository

import com.knf.dev.demo.model.User
import org.apache.ibatis.annotations.*

interface UserRepository {
@Select("select * from users")
fun findAll(): List<User?>?

@Select("SELECT * FROM users WHERE id = #{id}")
fun findById(id: Long): User?

@Delete("DELETE FROM users WHERE id = #{id}")
fun deleteById(id: Long): Int

"INSERT INTO users(id, firstName, lastName,emailId) " +
" VALUES (#{id}, #{firstName}, #{lastName}, #{emailId})"
fun insert(user: User?): Int

"Update users set firstName=#{firstName}, " +
" lastName=#{lastName}, emailId=#{emailId} where id=#{id}"
fun update(user: User?): Int

package com.knf.dev.demo.controller

import com.knf.dev.demo.model.User
import com.knf.dev.demo.repository.UserRepository
import javax.inject.Inject
import javax.ws.rs.*
import javax.ws.rs.core.MediaType

class UserController {
var userRepository: UserRepository? = null

fun getUsers(): kotlin.collections.List<User?>? {
return userRepository!!.findAll()

fun getUser(@PathParam("id") id: Long?): User? {
return userRepository!!.findById(id!!)

fun updateUser(user: User?) {

fun addUser(user: User?) {

fun deleteUser(@PathParam("id") id: Long?) {

Verify REST APIs

Build application jar file: gradle build

Start application: java -jar quarkus-run.jar

1. Add User

2. Get Single User

3. Fetch All Users

4. Update User

5. Delete User

Popular posts from this blog

Spring boot video streaming example-HTML5

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Spring Boot + Mockito simple application with 100% code coverage

Spring Boot + OpenCSV Export Data to CSV Example

Custom Exception Handling in Quarkus REST API

DataTable-Pagination example with Spring boot, jQuery and ajax

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Node JS mini projects with source code - free download

Spring boot web project free download:User Registration System

Java - Blowfish Encryption and decryption Example