Comprehensive Guide to MyBatis‑Plus CRUD, Annotations, Wrappers, Pagination and Advanced Features
This article provides a detailed tutorial on using MyBatis‑Plus with Spring Boot, covering quick setup, entity and mapper definitions, CRUD operations, condition wrappers (including lambda and chain wrappers), pagination, ActiveRecord mode, primary‑key strategies, configuration options, code generation, and advanced features such as logical deletion, auto‑fill, optimistic locking, performance analysis, multi‑tenant and dynamic table name handling.
This guide walks through integrating MyBatis‑Plus (MP) into a Spring Boot project, starting with Maven dependencies and a basic pom.xml configuration.
It shows how to define an entity class (e.g., User) with Lombok, @TableId, and @TableField annotations, and how to create a mapper interface that extends BaseMapper<User> as well as a service interface extending IService<User>.
CRUD operations are demonstrated using both the mapper and service layers, including insert, selectById, updateById, and deleteById. The article also explains the use of Wrapper classes ( QueryWrapper, UpdateWrapper) and their lambda equivalents ( LambdaQueryWrapper, LambdaUpdateWrapper) for building complex WHERE clauses, with examples of like, gt, or, nested, and custom SQL via apply.
Pagination is covered by configuring a MybatisPlusInterceptor with PaginationInnerInterceptor and using Page<User> together with a wrapper to retrieve paged results.
The ActiveRecord (AR) mode is introduced, showing how an entity can extend Model<User> to perform insert, selectById, updateById, and deleteById directly.
Primary‑key strategies ( IdType.AUTO, IdType.INPUT, IdType.ASSIGN_ID, etc.) are explained, with both global configuration in application.yml and per‑entity overrides using @TableId.
Configuration options such as mapUnderscoreToCamelCase, field strategies, table prefixes, and logical deletion ( @TableLogic or global logic-delete-field) are discussed.
Automatic field filling is demonstrated via a custom MetaObjectHandler that populates createTime and updateTime on insert or update.
Advanced plugins are presented: optimistic locking with OptimisticLockerInnerInterceptor and @Version, performance analysis using the P6Spy plugin, multi‑tenant support via TenantLineInnerInterceptor, and dynamic table name handling with DynamicTableNameInnerInterceptor.
Finally, the MP code generator is shown, illustrating how to generate entity, mapper, service, and controller classes automatically.
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.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.
