资讯动态

MyBatis-Plus 实战指南:从基础 CRUD 到条件构造器与自定义 SQL(一)

发布时间:2026/10/16 3:03:56 来源:尧图企业网站定制
目录​编辑1. MyBatis-Plus 介绍2. 快速上手2.1 项目依赖与配置Spring Boot 2Spring Boot 3Spring Boot 4application.ymlapplication.properties2.2 实体类与 Mapper2.4 基础 CRUD3. 常见注解与日志3.1 TableName3.2 TableField3.3 TableId3.4 打印日志1. MyBatis-Plus 介绍MyBatis-Plus简称 MP是一个 MyBatis 的增强工具。它在 MyBatis 的基础上只做增强不做改变目的是简化开发、提高效率。核心特性润物无声引入后不会对现有工程产生影响。效率至上简单配置即可快速完成单表 CRUD。丰富功能代码生成、自动分页、逻辑删除、自动填充、拦截器等。广泛认可连续多年获得开源中国年度最佳开源项目GitHub Star 数较高。支持数据库PostgreSQL、MySQL、MariaDB、Oracle、SQL Server、OceanBase、H2、DB2 等。只要数据库能通过 MyBatis 进行增删改查并支持标准 SQL基本都在支持范围内。官网地址MyBatis-Plus 为简化开发而生。2. 快速上手MyBatis-Plus 操作数据库的整体步骤准备工作数据准备、项目准备、引入依赖、配置数据库连接。编码编写数据库表对应的实体类以及 Mapper 接口。测试编写单元测试验证 CRUD。2.1 项目依赖与配置创建 Spring Boot 工程并添加 MyBatis-Plus 和 MySQL 依赖。Spring Boot 2dependency groupIdcom.baomidou/groupId artifactIdmybatis-plus-boot-starter/artifactId version3.5.17/version /dependencySpring Boot 3dependency groupIdcom.baomidou/groupId artifactIdmybatis-plus-spring-boot3-starter/artifactId version3.5.17/version /dependencySpring Boot 4dependency groupIdcom.baomidou/groupId artifactIdmybatis-plus-spring-boot4-starter/artifactId version3.5.17/version /dependencyapplication.ymlspring: datasource: url: jdbc:mysql://127.0.0.1:3306/你的数据库?characterEncodingutf8useSSLfalse username: 你的账号 password: 你的密码 driver-class-name: com.mysql.cj.jdbc.Driverapplication.propertiesspring.datasource.driver-class-namecom.mysql.cj.jdbc.Driver spring.datasource.urljdbc:mysql://127.0.0.1:3306/你的数据库名字?characterEncodingutf8useSSLfalse spring.datasource.username你的mysql账号 spring.datasource.password你的mysql密码2.2 实体类与 Mapper实体类属性名与表中字段名一一对应import lombok.Data; import java.util.Date; Data public class UserInfo { private Integer id; private String username; private String password; private Integer age; private Integer gender; private String phone; private Integer deleteFlag; private Date createTime; private Date updateTime; }2.4 基础 CRUD使用方法和 mybatis 一致都是创建一个 interface 接口然后使用 Mapper 注解你可以使用XML 或者是 注解的方式来编写 SQL。3. 常见注解与日志MyBatis-Plus 如何知道要操作哪张表、哪些字段默认规则表名实体类驼峰命名转为蛇行命名例如 UserInfo - user_info字段属性名转蛇形命名例如 deleteFlag - delete_flag主键默认为 id如果实体类与数据库表规则不一致可以使用注解标识。3.1 TableName当实体类名不是默认转换后的表名时使用 TableName 指定表名Data TableName(user_info) public class Userinfo { private Integer id; private String username; private String password; private Integer age; private Integer gender; private String phone; private Integer deleteFlag; private Date createTime; private Date updateTime; }3.2 TableField当属性名与字段名不一致时使用 TableField 指定字段名Data TableName(user_info) public class Userinfo { private Integer id; private String username; private String password; private Integer age; private Integer gender; private String phone; TableField(delete_flag) private Integer deleteflag; private Date createTime; private Date updateTime; }3.3 TableId当主键属性名与字段名不一致时使用 TableId 指定主键Data TableName(user_info) public class Userinfo { TableId(id) private Integer userId; private String username; private String password; private Integer age; private Integer gender; private String phone; TableField(delete_flag) private Integer deleteflag; private Date createTime; private Date updateTime; }如果属性名和字段名一致直接加 TableId 即可。3.4 打印日志为了查看 MyBatis-Plus 执行的 SQL、参数和结果可以配置日志mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价 →
↑