Using MyBatis‑Plus to Simplify CRUD Operations in Java Spring Boot

This article introduces MyBatis‑Plus, a powerful Java ORM that eliminates the need for manual XML mappings by providing ready‑made CRUD APIs, demonstrates how to configure Maven dependencies, define entity and mapper classes, and implement common database operations such as insert, delete, update, select, pagination, and complex conditional queries within a Spring Boot controller, all while showing the generated SQL statements.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Using MyBatis‑Plus to Simplify CRUD Operations in Java Spring Boot

MyBatis‑Plus is a framework built on top of MyBatis that automatically generates simple SQL statements and offers a rich set of APIs for common CRUD operations, greatly reducing the boilerplate code required in traditional MyBatis development.

To start using MyBatis‑Plus, add the following Maven dependencies to your pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" ...>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.sise</groupId>
    <artifactId>mybatis-plus</artifactId>
    <version>1.0‑SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatisplus-spring-boot-starter</artifactId>
            <version>1.0.5</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>2.3</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

Create the database table (e.g., teacher) and the corresponding Java entity class:

package com.sise.model;

import com.baomidou.mybatisplus.annotations.TableName;

@TableName(value = "teacher")
public class Teacher {
    private int id;
    private String teacherName;
    private String teacherPwd;
    // getters, setters, constructors, toString() omitted for brevity
}

Define a mapper interface that extends BaseMapper<Teacher> to inherit all CRUD methods:

package com.sise.dao;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.sise.model.Teacher;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface TeacherMapper extends BaseMapper<Teacher> { }

In a Spring Boot controller you can call the generated methods directly. Example insert method:

@GetMapping(value = "/insert")
public void insert() {
    Teacher teacher = new Teacher();
    teacher.setTeacherName(createRandomStr(6));
    teacher.setTeacherPwd(createRandomStr(6));
    teacherMapper.insert(teacher);
}

private static String createRandomStr(int length) {
    String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    Random random = new Random();
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < length; i++) {
        sb.append(chars.charAt(random.nextInt(chars.length())));
    }
    return sb.toString();
}

Delete, update, and select operations are similarly concise:

@GetMapping(value = "/delete")
public void delete() {
    Teacher teacher = new Teacher();
    teacher.setId(11);
    EntityWrapper<Teacher> wrapper = new EntityWrapper<>(teacher);
    teacherMapper.delete(wrapper);
}

@GetMapping(value = "/update")
public void update() {
    EntityWrapper<Teacher> wrapper = new EntityWrapper<>(new Teacher(1));
    Teacher teacher = new Teacher();
    teacher.setTeacherPwd("new-pwd");
    teacherMapper.update(teacher, wrapper);
}

@GetMapping(value = "/selectById")
public Teacher selectById(int id) {
    return teacherMapper.selectOne(new Teacher(id));
}

MyBatis‑Plus also supports batch operations, pagination, and complex conditional queries using EntityWrapper (or the newer QueryWrapper). Example of a paginated query:

@GetMapping(value = "/selectAllInPage")
public List<Teacher> selectAllInPage(int pageNumber, int pageSize) {
    Page<Teacher> page = new Page<>(pageNumber, pageSize);
    EntityWrapper<Teacher> wrapper = new EntityWrapper<>();
    wrapper.ge("id", 1);
    return teacherMapper.selectPage(page, wrapper);
}

For more advanced scenarios you can use methods such as selectByMap, selectBatchIds, selectCount, and combine multiple conditions with allEq, ne, like, orderBy, groupBy, and having. The framework also provides the IService interface, which offers a higher‑level service layer with the same CRUD capabilities.

Overall, MyBatis‑Plus merges the simplicity of Hibernate‑style CRUD with the flexibility of MyBatis, allowing developers to write far less boilerplate code while retaining full control over SQL when needed.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaSpring BootORMmybatis-plusCRUD
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.