When to Use MyBatis‑Plus: Benefits, Drawbacks, and Best Practices
This article examines the ongoing debate around MyBatis‑Plus, highlighting its convenience for simple CRUD operations, the maintenance challenges it introduces, and practical guidelines—such as using LambdaQueryWrapper and pagination plugins—to decide when the framework is appropriate for Java backend projects.
Preface
Since its introduction, MyBatis‑Plus (MP) has sparked controversy, with users divided between its convenience and its drawbacks.
Like: It allows rapid SQL composition through functions, eliminating the need for XML and enabling one‑second query creation.
Dislike: It intrudes into the Service layer, reduces readability, increases coupling, and makes SQL optimization difficult.
Advantages
Simple Operations
MP provides straightforward methods for common CRUD actions:
nodeMapper.selectById(1);
nodeMapper.deleteById(2);
nodeMapper.updateById(new Node());
nodeMapper.insert(new Node());However, these default methods often select all columns, violating the best practice of avoiding SELECT *.
To improve maintainability, use QueryWrapper or LambdaQueryWrapper to specify fields explicitly:
nodeMapper.selectOne(new QueryWrapper<Node>().eq("id",1).select("id"));Using lambda expressions further reduces the risk of "magic values" and improves refactoring safety:
// Avoid magic strings
if ("Star".equals(node.getName())) {
System.out.println("Magic value");
}Prefer constants or enums instead of hard‑coded strings such as "id".
Example with LambdaQueryWrapper that ties the field directly to the entity:
Node node = nodeMapper.selectOne(new LambdaQueryWrapper<Node>().eq(Node::getId,1).select(Node::getId));Pagination
// Conditional query
LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(UserInfo::getAge,20);
// Pagination object
Page<UserInfo> queryPage = new Page<>(page, limit);
// Execute paginated query
IPage<UserInfo> iPage = userInfoMapper.selectPage(queryPage, queryWrapper);
Long total = iPage.getTotal();
List<UserInfo> list = iPage.getRecords();Simple Summary
MP is suitable for simple single‑table queries but should be avoided for complex SQL operations.
Separate MP queries into a dedicated package to prevent Service‑layer intrusion.
Prefer LambdaQueryWrapper for better maintainability.
Leverage MP's built‑in primary‑key generation and pagination plugins.
Drawbacks
The biggest limitation is handling complex SQL, such as multi‑table joins, which often require manual XML or @Select annotations with custom SQL segments, making the code hard to read and maintain.
Final Recommendations
Use MP for straightforward CRUD; avoid it for intricate queries.
Adopt LambdaQueryWrapper wherever possible.
Avoid default methods like selectById that fetch all columns.
Utilize MP's pagination plugin for effortless paging.
Java High-Performance Architecture
Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.
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.
