Build REST CRUD API with Quarkus and MyBatis
Hello everyone. today we will learn how to develop REST-style CRUD APIs with 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 making 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:
- GET - Fetch all User : /api/v1/users
- GET - Get User by ID : /api/v1/users/{id}
- POST - Create User : /api/v1/users
- PUT - Edit User Details : /api/v1/users/
- DELETE - Delete User : /api/v1/users/{id}
Technologies used:
- Quarkus 2.2.3.Final
- MyBatis 0.0.9
- Java 11
- Maven 3
- H2DB
More Quarkus Related topics,
- Quarkus + Angular 10 + MongoDB CRUD Example
- Quarkus + React JS + MongoDB CRUD Example
- Quarkus + Vue.js + MongoDB CRUD Example
- Build Reactive CRUD APIs With Quarkus, MongoDB, and Panache
- QUARKUS + Hibernate CRUD example - Creating a CRUD REST API/Service
- Build REST CRUD APIs with Quarkus and MyBatis
- Build Rest CRUD API with Mongodb, Quarkus, and Mongodb Client
- Build Rest CRUD API with Quarkus and MongoDB Panache
- Create Quarkus Project With code.quarkus.io: Hello world example
- Quarkus + FreeMarker + Hibernate - CRUD example
- Quarkus File Upload Example
- Quarkus - How to send email via SMTP - Quickstart
- Quarkus - Qute - Hello World Example
- Quarkus + Maven Hello World Example
- Configuring the HTTP Port on Quarkus Applications
- Quarkus + Angular 10 + MongoDB CRUD Example
- Quarkus + React JS + MongoDB CRUD Example
- Quarkus + Vue.js + MongoDB CRUD Example
- Build Reactive CRUD APIs With Quarkus, MongoDB, and Panache
- QUARKUS + Hibernate CRUD example - Creating a CRUD REST API/Service
- Build REST CRUD APIs with Quarkus and MyBatis
- Build Rest CRUD API with Mongodb, Quarkus, and Mongodb Client
- Build Rest CRUD API with Quarkus and MongoDB Panache
- Create Quarkus Project With code.quarkus.io: Hello world example
- Quarkus + FreeMarker + Hibernate - CRUD example
- Quarkus File Upload Example
- Quarkus - How to send email via SMTP - Quickstart
- Quarkus - Qute - Hello World Example
- Quarkus + Maven Hello World Example
- Configuring the HTTP Port on Quarkus Applications
Project Directory:
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.knf.dev.demo</groupId>
<artifactId>quarkus-mybatis-crud</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<compiler-plugin.version>3.8.1</compiler-plugin.version>
<maven.compiler.parameters>true</maven.compiler.parameters>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8
</project.reporting.outputEncoding>
<quarkus-plugin.version>2.2.3.Final</quarkus-plugin.version>
<quarkus.platform.artifact-id>quarkus-universe-bom
</quarkus.platform.artifact-id>
<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
<quarkus.platform.version>2.2.3.Final</quarkus.platform.version>
<surefire-plugin.version>2.22.1</surefire-plugin.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>${quarkus.platform.group-id}</groupId>
<artifactId>${quarkus.platform.artifact-id}</artifactId>
<version>${quarkus.platform.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jsonb</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-h2</artifactId>
</dependency>
<dependency>
<groupId>io.quarkiverse.mybatis</groupId>
<artifactId>quarkus-mybatis</artifactId>
<version>0.0.9</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-agroal</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<version>${quarkus-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compiler-plugin.version}</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<configuration>
<systemProperties>
<java.util.logging.manager>
org.jboss.logmanager.LogManager
</java.util.logging.manager>
</systemProperties>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>native</id>
<activation>
<property>
<name>native</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<systemProperties>
<native.image.path>
${project.build.directory}/${project.build.finalName}-runner
</native.image.path>
</systemProperties>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<quarkus.package.type>native</quarkus.package.type>
</properties>
</profile>
</profiles>
</project>
Database Setup
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)
);
application.properties
quarkus.datasource.db-kind = h2
quarkus.datasource.username = sa
# quarkus.datasource.password =
quarkus.datasource.jdbc.url = jdbc:h2:mem:test
quarkus.mybatis.initial-sql=schema.sql
Create User Model
package com.knf.dev.demo.model;
public class User {
private long id;
private String firstName;
private String lastName;
private String emailId;
public User() {
}
public User(long id,String firstName, String lastName, String emailId) {
super();
this.id=id;
this.firstName = firstName;
this.lastName = lastName;
this.emailId = emailId;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
}
Create User MyBatis Repository
package com.knf.dev.demo.repository;
import com.knf.dev.demo.model.User;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper
public interface UserRepository {
@Select("select * from users")
public List<User> findAll();
@Select("SELECT * FROM users WHERE id = #{id}")
public User findById(long id);
@Delete("DELETE FROM users WHERE id = #{id}")
public int deleteById(long id);
@Insert("INSERT INTO users(id, firstName, lastName,emailId) " +
" VALUES (#{id}, #{firstName}, #{lastName}, #{emailId})")
public int insert(User user);
@Update("Update users set firstName=#{firstName}, " +
" lastName=#{lastName}, emailId=#{emailId} where id=#{id}")
public int update(User user);
}
Create UserEndpoint
package com.knf.dev.demo.endpoint;
import com.knf.dev.demo.model.User;
import com.knf.dev.demo.repository.UserRepository;
import java.util.List;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/api/users")
public class UserEndpoint {
@Inject
UserRepository userResource;
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<User> getUsers() {
return userResource.findAll();
}
@GET
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public User getUser(@PathParam("id") Long id) {
return userResource.findById(id);
}
@PUT
@Consumes(MediaType.APPLICATION_JSON)
public void updateUser(User user) {
userResource.update(user);
}
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public void addUser(User user) {
userResource.insert(user);
}
@DELETE
@Path("/{id}")
public void deleteUser(@PathParam("id") Long id) {
userResource.deleteById(id);
}
}
Run the Application & Verify REST APIs
Build application jar file: mvn clean package
Start application: java -jar quarkus-run.jar
Download the source code: