Boost CRUD Development Efficiency with MyBatisPlus Pro
MyBatisPlus Pro extends MyBatisPlus by providing a BaseController that auto‑generates CRUD, pagination, and conditional queries, dramatically cutting repetitive code; the article walks through its architecture, quick‑start steps, deep technical mechanisms, pros and cons, and practical usage guidelines.
MyBatis is a lightweight persistence framework, but ordinary single‑table CRUD often suffers from repetitive XML and boilerplate code, leading to low development efficiency. MyBatisPlus improves this by offering BaseMapper, yet developers still need to write Service and Controller layers for each entity.
Why MyBatisPlus Pro?
MyBatisPlus Pro builds on MyBatisPlus and introduces a BaseController that provides CRUD, pagination, list, sorting, and conditional query capabilities out of the box. By inheriting this controller, developers can focus solely on business logic.
Quick Start
Add the MyBatisPlus starter dependency (version 3.5.15, compatible with SpringBoot 4.0.0 and Jackson 3.0).
Create a utility class ApprenticeUtil that implements three core functions:
Camel‑case ↔ snake‑case conversion.
Reflection‑based extraction of entity field values.
Automatic generation of a QueryWrapper<E> from non‑null fields.
Implement a generic BaseController that extends IService<T> and provides methods such as add, update, delete, detail, list, and page. The list method uses ApprenticeUtil.getQueryWrapper(entity) to build query conditions automatically.
Example Usage
Define an entity Product, a service interface extending IService<Product>, and a controller that simply extends BaseController<ProductService, Product>. No additional CRUD code is required.
public class Product {
@TableId(type = IdType.AUTO)
private Long id;
private String name;
private BigDecimal price;
private Integer stock;
private LocalDateTime createTime;
}
public interface ProductService extends IService<Product> {}
@RestController
@RequestMapping("/product")
public class ProductController extends BaseController<ProductService, Product> {}Deep Technical Analysis
The article explains the full SQL execution chain from Controller to database, covering:
Dynamic proxy mechanism: Spring creates JDK proxies for all interfaces extending BaseMapper. Method calls are intercepted by MapperProxy, which determines the corresponding SQL operation.
Automatic MappedStatement generation: MyBatisPlus’s SqlInjector registers MappedStatements for all generic methods during startup.
Interceptor chain: MybatisPlusInterceptor injects pagination, optimistic lock, multi‑tenant, and other enhancements before the Executor runs.
Wrapper construction: The utility class builds a QueryWrapper by reflecting over entity fields, converting camel‑case names to snake‑case column names, and adding eq conditions for non‑null values.
Emphasis on type‑safe LambdaQueryWrapper over string‑based wrappers to catch field‑name errors at compile time.
Pros
Exponential increase in development efficiency – a module’s CRUD can be ready with a few lines of code, reducing effort by over 80% compared to raw MyBatis.
Uniform code style and lower maintenance cost.
Zero XML configuration for both MyBatisPlus and Pro layers.
Built‑in pagination and rich condition builder (eq, like, between, orderBy).
Non‑intrusive – developers can still write native MyBatis SQL when needed.
Cons
Complex multi‑table joins become verbose; Wrapper is designed for single‑table queries.
Default select* may cause performance issues on large tables.
String‑based field names in wrappers are error‑prone; Lambda version is preferred.
Over‑encapsulation can hinder highly customized Service logic.
Generated SQL can be harder to read and tune compared to hand‑written XML.
Recommended Scenarios
Small‑to‑medium projects with fast iteration cycles.
Backend admin systems where CRUD dominates the workload.
Projects where >80% of operations are simple single‑table CRUD.
When to Avoid
High‑performance transaction systems that require fine‑grained SQL tuning.
Complex reporting or analytics requiring extensive joins, sub‑queries, and aggregations.
Security‑sensitive applications that need manual SQL review.
Pitfall‑Avoidance Guide
Do not overuse generic selectList with SELECT *; specify required columns.
Prefer LambdaQueryWrapper for type safety.
Always use MyBatisPlus’s pagination plugin instead of manual LIMIT.
For complex joins, write raw SQL in Mapper XML.
Follow camelCase naming for entity fields to enable automatic mapping.
Conclusion
MyBatisPlus Pro solves the core pain point of repetitive CRUD code by introducing a BaseController and reflection‑based query generation. It dramatically speeds up development while preserving MyBatis’s flexibility. However, it is not a silver bullet; complex queries and performance‑critical paths still benefit from hand‑crafted SQL.
For more details, refer to the official MyBatisPlus documentation (https://baomidou.com) and the MyBatisPlusPro repository (https://gitee.com/nirui-gitee/mybatis-plus-pro).
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 Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
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.
