Core Capabilities
- Seamless Enhancement: Augments MyBatis without altering its original mechanics, ensuring frictionless adoption.
- Minimal Overhead: Basic CRUD operations are automatically injected upon startup, allowing direct object-oriented manipulation with negligible performance cost.
- Robust CRUD: Provides built-in generic Mapper and Service interfaces, enabling single-table operations with minimal setup, alongside versatile conditional wrappers.
- Lambda Expressions: Enables type-safe query formulation through Lambda expressions, preventing field name mismatches.
- Key Generation: Offers multiple primary key strategies, including distributed unique ID generators.
- ActiveRecord: Supports ActiveRecord patterns where entities inherit the Model class to execute CRUD operations directly.
- Global Method Injection: Allows defining custom universal methods that are injected globally.
- Code Scaffolding: Includes a code generator to rapidly produce Mapper, Model, Service, and Controller layers.
- Physical Pagination: Features a built-in physical pagination plugin supporting numerous databases (MySQL, Oracle, PostgreSQL, etc.), making paginated queries identical to standard list queries.
- Performance Analysis: Incorporates a plugin to log SQL execution times, facilitating the identification of slow queries.
- Preventative Interception: Smart blocker for full-table delete or update operations to prevent accidental data destruction.
Database Initialization
DROP TABLE IF EXISTS `app_user`;CREATE TABLE `app_user`( user_id BIGINT NOT NULL COMMENT 'Primary identifier', full_name VARCHAR(30) NULL DEFAULT NULL COMMENT 'Full name', user_age INT NULL DEFAULT NULL COMMENT 'Age', contact_email VARCHAR(50) NULL DEFAULT NULL COMMENT 'Email address', PRIMARY KEY (user_id));DELETE FROM `app_user`;INSERT INTO `app_user` (user_id, full_name, user_age, contact_email) VALUES(1, 'Alice', 28, 'alice@example.org'),(2, 'Bob', 35, 'bob@example.org'),(3, 'Charlie', 22, 'charlie@example.org');Maven Dependencies
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.7</version></dependency>Application Configuration
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/app_db?serverTimezone=UTC username: admin password: secretPasswordmybatis-plus: mapper-locations: classpath*:mapper/**/*.xml configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImplEntity Definition
@Data@TableName("app_user")public class AppUser { @TableId("user_id") private Long userId; @TableField("full_name") private String fullName; @TableField("user_age") private Integer userAge; @TableField("contact_email") private String contactEmail;}Mapper Interface
public interface AppUserMapper extends BaseMapper<AppUser> {}Proxy Configuration
Ensure the application main class is annotated to scan for mapper interfaces.
@SpringBootApplication@MapperScan("com.example.mapper")public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}CRUD Verification
Fetching by Identifier
@SpringBootTestclass MybatisPlusIntegrationTests { @Autowired private AppUserMapper appUserMapper; @Test void fetchById() { AppUser record = appUserMapper.selectById(1); System.out.println(record); }}Inserting or Updating Records
@Testvoid addOrUpdateRecord() { AppUser newUser = new AppUser(); newUser.setUserId(4L); newUser.setFullName("David"); newUser.setUserAge(40); newUser.setContactEmail("david@example.org"); appUserMapper.insertOrUpdate(newUser);}