中间件:MyBatis-Plus(ORM 框架)
2026/7/10大约 2 分钟约 581 字
中间件:MyBatis-Plus(ORM 框架)
1. 简介
MyBatis-Plus 是对 MyBatis 的增强 ORM 框架,在本项目中作为所有数据库访问的基础层,屏蔽 JDBC 样板代码,提供通用的 BaseMapper CRUD、类型安全的 LambdaQueryWrapper 查询构造器,以及分页插件。项目使用 mybatis-plus-spring-boot3-starter 3.5.16。
2. 引入方式
pom.xml 引入:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
<version>${mybatis-plus.version}</version> <!-- 3.5.16 -->
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-jsqlparser</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>3. 配置位置
- 启动扫描:
BackendApplication.java上的@MapperScan("com.yang.backend.mapper")扫描所有 Mapper 接口。 - 拦截器/分页:
config/MybatisPlusConfig.java注册MybatisPlusInterceptor并添加针对 PostgreSQL 的PaginationInnerInterceptor。 - 命名与 banner:
application.yml中mybatis-plus.configuration.map-underscore-to-camel-case: true(下划线列名自动映射为 Java 驼峰属性),global-config.banner: false(关闭启动 banner)。
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.POSTGRE_SQL));
return interceptor;
}4. 在哪里应用
| 层级 | 位置 | 说明 |
|---|---|---|
| 实体 | entity/*.java | 使用 @TableName、@TableId、@TableField 映射数据表与字段 |
| 数据访问 | mapper/*.java | 接口继承 BaseMapper<T>,获得通用 CRUD |
| 业务层 | service/*.java | 使用 LambdaQueryWrapper、分页、增删改查 |
覆盖全部 13 张表对应的 13 个实体与 12 个 Mapper(如 AccountMapper、OrderMapper、ProductMapper 等)。
5. 怎么应用(示例)
① 实体映射(entity) —— 通过注解声明表名、主键、字段:
@TableName("account")
public class Account {
@TableId private Long id;
private Long userId;
private BigDecimal balance;
// ...
}② Mapper 继承通用接口(mapper):
@Mapper
public interface AccountMapper extends BaseMapper<Account> { }③ 业务层查询与分页(service) —— 以 WalletService 为例:
// 单条查询
Account account = accountMapper.selectOne(
new LambdaQueryWrapper<Account>().eq(Account::getUserId, userId));
// 分页 + 复杂条件
LambdaQueryWrapper<TransactionLog> wrapper = new LambdaQueryWrapper<TransactionLog>()
.and(q -> q.eq(TransactionLog::getFromUserId, userId)
.or().eq(TransactionLog::getToUserId, userId))
.orderByDesc(TransactionLog::getCreatedAt);
Page<TransactionLog> result =
transactionLogMapper.selectPage(Page.of(current, pageSize), wrapper);
// 写入与更新
accountMapper.insert(account);
accountMapper.updateById(account);6. 作用是什么
- 简化 CRUD:
BaseMapper提供selectOne/selectList/selectPage/insert/updateById/deleteById等开箱即用方法,无需手写 SQL。 - 类型安全查询:
LambdaQueryWrapper以方法引用引用属性,编译期即可发现字段拼写错误,且自动处理下划线→驼峰映射。 - 分页增强:
PaginationInnerInterceptor在底层自动改写 SQL 追加LIMIT/OFFSET,业务层只需传入Page对象即可获得总记录数与分页数据。 - 与 PostgreSQL 适配:指定
DbType.POSTGRE_SQL,生成符合 PostgreSQL 语法的分页 SQL。
7. 相关文件清单
pom.xml(MyBatis-Plus 依赖)src/main/java/com/yang/backend/BackendApplication.java(@MapperScan)src/main/java/com/yang/backend/config/MybatisPlusConfig.javasrc/main/resources/application.yml(mybatis-plus)src/main/java/com/yang/backend/entity/*src/main/java/com/yang/backend/mapper/*src/main/java/com/yang/backend/service/*