Automate CRUD with a Generic BaseController in Spring Boot
This guide shows how to create a reusable BaseController using MyBatis‑Plus, reflection utilities, and Spring Boot annotations to provide automatic CRUD, list, pagination and count endpoints for any entity with minimal code.
Introduction
The article demonstrates how to build a generic BaseController in a Spring Boot project that automatically supplies common CRUD operations for any entity, leveraging MyBatis‑Plus and Java reflection.
Step 1 – Add MyBatis‑Plus Dependency
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>Include this Maven dependency in pom.xml to enable MyBatis‑Plus features.
Step 2 – Create ApprenticeUtil
This utility class provides reflection‑based helpers: humpToLine(String str) – converts camelCase to snake_case. lineToHump(String str) – converts snake_case to camelCase. <E> QueryWrapper<E> getQueryWrapper(E entity) – builds a QueryWrapper by iterating over non‑null fields of the given entity. <E> Object getValueForClass(E entity, String value) – obtains a field value via PropertyDescriptor and Spring’s ReflectionUtils.
public class ApprenticeUtil {
private static Pattern humpPattern = Pattern.compile("[A-Z]");
private static Pattern linePattern = Pattern.compile("_(\\w)");
// ... (methods humpToLine, lineToHump, getQueryWrapper, getValueForClass)
}Step 3 – Implement the Generic BaseController
The controller is defined with two generic parameters: S extends IService<E> for the service layer and E for the entity type. It provides the following endpoints: @PostMapping("/insert") –
insert(E entity) @PostMapping("/deleteById")–
delete(List<Integer> ids) @PostMapping("/updateById")–
updateById(E entity) @GetMapping("/getById")–
getById(Integer id) @PostMapping("/save")–
save(E entity) @PostMapping("/list")–
list(E entity) @PostMapping("/page")–
page(PageParamDto<E> dto) @PostMapping("/count")–
count(E entity) public class BaseController<S extends IService<E>, E> {
@Autowired
protected S baseService;
@PostMapping("/insert")
public ResponseUtils insert(@RequestBody E entity) {
baseService.save(entity);
return ResponseUtils.success("添加成功");
}
// ... other CRUD methods as shown above
}Step 4 – Configure Pagination Support
MyBatis‑Plus does not enable pagination by default, so a configuration class adds the PaginationInnerInterceptor for MySQL.
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}Step 5 – Extend BaseController in Concrete Controllers
For each domain object, create a controller that extends the generic base class, supplying the specific service interface and entity class.
@RestController
@RequestMapping("/apprentice/dynamic")
@Api("动态管理")
public class DynamicController extends BaseController<IDynamicService, Dynamic> {}
@RestController
@RequestMapping("/apprentice/blog")
@Api(tags = "博客管理")
public class BlogController extends BaseController<IBlogService, Blog> {}These concrete controllers inherit all CRUD endpoints without additional code.
Conclusion
By combining MyBatis‑Plus, a reflection‑based utility, and a generic Spring MVC controller, developers can rapidly scaffold RESTful CRUD APIs for any entity, reduce boilerplate, and maintain a consistent API surface across the application.
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.
Java Tech Enthusiast
Sharing computer programming language knowledge, focusing on Java fundamentals, data structures, related tools, Spring Cloud, IntelliJ IDEA... Book giveaways, red‑packet rewards and other perks await!
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.
