MyBatis Plus: The Productivity Boost Your MyBatis Workflow Needs
If you've ever worked with MyBatis, you know it's powerful but verbose. XML mappers, boilerplate CRUD, manual pagination logic... it adds up fast. That's where MyBatis Plus steps in. It's not a replacement for MyBatis, it's a turbocharger.
It keeps all the control MyBatis gives you over SQL, but removes the repetitive grunt work. Think of it as the difference between writing SQL by hand and having a tool that writes the obvious 90% for you, while still letting you take the wheel for complex queries. For teams stuck in JPA vs MyBatis debates, this project is a serious argument for the "why not both" camp.
What It Does
MyBatis Plus is a powerful enhancement tool for MyBatis that provides out-of-the-box CRUD operations, pagination, and code generation. You get a richer, more convenient API for daily database operations without losing the flexibility of custom SQL.
Under the hood, it's an ORM that sits on top of MyBatis. It gives you a BaseMapper interface where you simply define your entity class, and you instantly have selectById, insert, updateById, deleteById, and batch operations available. No XML, no annotations, no SQL strings for basic operations.
Why It's Cool
Here's the part that makes it genuinely useful:
1. It Kills the Boilerplate
You write a POJO, extend BaseMapper<T>, and you're done. Your service layer doesn't need a UserMapper.xml with 20 lines of INSERT statements. This isn't just about saving keystrokes, it's about reducing the surface area for bugs. When you don't write repetitive SQL, you can't make typos in it.
2. Pagination That Doesn't Suck
Database pagination is always database-specific. LIMIT in MySQL, ROWNUM in Oracle, OFFSET FETCH in SQL Server. MyBatis Plus has a PaginationInnerInterceptor that handles this dialect detection for you. You just pass a Page object to your mapper method, and it injects the correct pagination SQL automatically. It also gets the COUNT(*) query right, which is harder than it sounds when you have joins.
3. The Wrapper API Is Genuinely Nice
You know how JPA has Specification and it feels clunky? MyBatis Plus has QueryWrapper and LambdaQueryWrapper. You can do something like this:
List<User> users = userMapper.selectList(
new LambdaQueryWrapper<User>()
.eq(User::getStatus, "ACTIVE")
.and(w -> w.like(User::getName, "John")
.or().like(User::getEmail, "john"))
.orderByDesc(User::getCreatedAt)
.last("LIMIT 10")
);
It's type-safe, readable, and you still have .last() to drop in raw SQL when you need that escape hatch. The lambda version means refactoring field names doesn't silently break your queries with strings.
4. Code Generator Included
It ships with MybatisPlusGenerator which can reverse-engineer your database tables into entity classes, mappers, services, and controllers in a single main method. It's not perfect out of the box, but it's a massive head start for a new module.
5. The Plugin Ecosystem
It's not just one thing. There are plugins for optimistic locking (@Version), logic deletion (@TableLogic, where you mark columns as deleted instead of actually deleting rows), and a schema-only TenantLineInnerInterceptor for multi-tenant SaaS apps. These are features you'd normally have to hand-roll in a BaseService class.
How to Try It
Getting started takes about five minutes. If you're using Spring Boot, add the dependency:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.7</version>
</dependency>
Then define a simple entity:
@TableName("user")
public class User {
@TableId(type = IdType.AUTO)
private Long id;
private String name;
private Integer age;
private String email;
}
Create a mapper interface:
public interface UserMapper extends BaseMapper<User> {
// Custom methods go here if needed
}
That's the entire boilerplate. You can now call userMapper.selectList(null) and get all users.
For a full walkthrough, the official documentation at baomidou.com has a quick-start guide with step-by-step examples. The repo itself also has a detailed README with configuration options for different databases.
A word of caution: the docs are mostly in Chinese, but the code examples are universal and the English translation is passable. The community is active and you'll find solutions on Stack Overflow for most common issues.
Final Thoughts
Look, MyBatis Plus isn't magic. It won't write your complex reporting queries, and if your team hates ORMs, it won't convert you. But it solves a very real problem: the "I just need to do CRUD on this table" tiresome work.
For projects where you're spending more time writing ResultMap and INSERT statements than business logic, give it a shot. It's production-ready, actively maintained, and used by a lot of Chinese tech companies that you've definitely heard of. If you're on the fence, try the code generator on a fake table for 15 minutes after lunch. You'll probably start a new project with it next week.
Looking for more useful tools and repos? Follow @githubprojects for daily picks.