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.
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<S extends IService<E>, 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<IDynamicService, Dynamic> {}
@RestController
@RequestMapping("/apprentice/blog")
@Api(tags = "Blog Management")
public class BlogController extends BaseController<IBlogService, Blog> {}With these steps, the application gains a fully functional, reusable CRUD layer without writing repetitive code for each entity.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Architect's Guide
Dedicated to sharing programmer-architect skills—Java backend, system, microservice, and distributed architectures—to help you become a senior architect.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
