Build REST CRUD APIs with Spring Boot and MyBatis

Hello every one, here we will learn how to develop REST-style CRUD APIs with Spring Boot, MyBatis, and H2 Database with Annotation mapping. You can download the source code from our Github repository.

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/{id} 
  5. DELETE - Delete User    :     /api/v1/users/{id}

Project Directory:

Maven dependencies(pom.xml)

Puts mybatis-spring-boot-starter, h2 dependencies.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=""
<relativePath/> <!-- lookup parent from repository -->
<description>Demo project for Spring Boot</description>



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)

Create User 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) {
this.firstName = firstName;
this.lastName = lastName;
this.emailId = emailId;

public long getId() {
return id;

public void setId(long 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 

The MyBatis-Spring-Boot-Starter will search, by default, for mappers marked with the @Mapper annotation. In this example, we are not using XML mappings.

import org.apache.ibatis.annotations.*;
import java.util.List;

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 User Rest Controller


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserController {
private UserRepository userRepository;

// get all users
public List<User> getAllUsers()
return userRepository.findAll();

// create user rest API
public User createUser(@RequestBody User user) {
if(userRepository.findById(user.getId())==null) {
int id = userRepository.insert(user);
return userRepository.findById(user.getId());
throw new UserIdAlreadyExistException();


// get user by id rest api
public ResponseEntity<User> getUserById(@PathVariable Long id) {
User user = userRepository.findById(id);
throw new UserIdNotFoundException();
return ResponseEntity.ok(user);

// update user rest api
public ResponseEntity<User> updateUser(@PathVariable Long id,
@RequestBody User userDetails) {
if(userRepository.update(new User(id, userDetails.getFirstName(),
         userDetails.getLastName(), userDetails.getEmailId()))==0)
throw new UserIdNotFoundException();

return ResponseEntity.ok(userRepository.findById(id));

// delete user rest api
public ResponseEntity<Map<String, Boolean>> deleteUser
(@PathVariable Long id) {
Map<String, Boolean> response = new HashMap<>();
response.put("deleted", Boolean.TRUE);
return ResponseEntity.ok(response);

Create Custom Exception



public class UserIdNotFoundException extends RuntimeException{
public UserIdNotFoundException()
super("User Id Not Found");



public class UserIdAlreadyExistException extends RuntimeException{
public UserIdAlreadyExistException() {
super("User Id Already Exist");


Spring supports exception handling by a global Exception Handler (@ExceptionHandler) with Controller Advice (@ControllerAdvice). This enables a mechanism that makes ResponseEntity work with the type safety and flexibility of @ExceptionHandler.

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import java.time.LocalDateTime;

public class GlobalExceptionHandler {

public ResponseEntity<CustomErrorResponse>
        globalExceptionHandler(Exception ex, WebRequest request) {
CustomErrorResponse errors = new CustomErrorResponse();
return new ResponseEntity<>(errors, HttpStatus.NOT_FOUND);

public ResponseEntity<CustomErrorResponse>
        globalExceptionHandler2(Exception ex, WebRequest request) {

CustomErrorResponse errors = new CustomErrorResponse();
return new ResponseEntity<>
                 (errors, HttpStatus.INTERNAL_SERVER_ERROR);



import com.fasterxml.jackson.annotation.JsonFormat;
import java.time.LocalDateTime;

public class CustomErrorResponse {

@JsonFormat(shape = JsonFormat.Shape.STRING,
                       pattern = "yyyy-MM-dd hh:mm:ss")
private LocalDateTime timestamp;
private int status;
private String error;
public LocalDateTime getTimestamp()
return timestamp;
public void setTimestamp(LocalDateTime timestamp)
this.timestamp = timestamp;
public int getStatus()
return status;
public void setStatus(int status)
this.status = status;
public String getError()
return error;
public void setError(String error)
this.error = error;

Spring Boot Main Class


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class SpringbootMybatisCrudExampleApplication {

public static void main(String[] args) {
         run(SpringbootMybatisCrudExampleApplication.class, args);
The Spring Boot application's main class contains a public static void main() method that starts up the Spring ApplicationContext.

Local Setup

Step 1: Download or clone the source code to a local machine.

Step 2mvn clean install

Step 3: Run the Spring Boot application
mvn spring-boot:run

Testing API's using Postman

Create a User:

Fetch all User:

Get User by ID:

Update User:

Delete User by ID:

More related topics,

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