Backend Development 9 min read

Automating CRUD Operations with MyBatisPlus in Spring Boot

This article demonstrates how to automate CRUD operations in a Spring Boot application by leveraging MyBatisPlus, creating utility methods, a generic BaseController, pagination configuration, and extending it in concrete controllers, providing reusable code for rapid backend development.

Architect's Guide
Architect's Guide
Architect's Guide
Automating CRUD Operations with MyBatisPlus in Spring Boot

Even for simple CRUD, extracting a reusable component is straightforward; this guide shows how to automate CRUD using MyBatisPlus in a Spring Boot project.

First, add the MyBatisPlus starter dependency:

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.2</version>
</dependency>

Next, create a utility class ApprenticeUtil that provides methods for converting between camelCase and snake_case, building a QueryWrapper from an entity, and retrieving field values via reflection.

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Apprentice system utilities
 */
public class ApprenticeUtil {
    private static Pattern humpPattern = Pattern.compile("[A-Z]");
    private static Pattern linePattern = Pattern.compile("_(\\w)");
    // ... (methods humpToLine, lineToHump, getQueryWrapper, getValueForClass)
}

Then define a generic BaseController that, through Spring MVC annotations, offers common RESTful endpoints for insert, delete, update, getById, save, list, page, and count operations. The controller uses the generic service S extends IService<E> and the entity type E .

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.wangfugui.apprentice.common.util.ApprenticeUtil;
import com.wangfugui.apprentice.common.util.ResponseUtils;
import com.wangfugui.apprentice.dao.dto.PageParamDto;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

public class BaseController
, E> {
    @Autowired
    protected S baseService;
    // CRUD methods annotated with @PostMapping/@GetMapping
    // ... (insert, delete, updateById, getById, save, list, page, count)
}

Because MyBatisPlus does not enable pagination by default, add a configuration class that registers a PaginationInnerInterceptor for MySQL.

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MybatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

Finally, create concrete controllers that extend BaseController , supplying the specific service and entity types. For example, a DynamicController and a BlogController can be defined with only the class declaration.

@RestController
@RequestMapping("/apprentice/dynamic")
@Api("Dynamic Management")
public class DynamicController extends BaseController
{}

@RestController
@RequestMapping("/apprentice/blog")
@Api(tags = "Blog Management")
public class BlogController extends BaseController
{}

With these steps, the application gains a fully functional, reusable CRUD layer without writing repetitive code for each entity.

JavaCode GenerationSpring BootCRUDRESTful APIMyBatisPlus
Architect's Guide
Written by

Architect's Guide

Dedicated to sharing programmer-architect skills—Java backend, system, microservice, and distributed architectures—to help you become a senior architect.

0 followers
Reader feedback

How this landed with the community

login 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.