《课次25:条件构造器 & 分页插件》学习笔记

《课次25:条件构造器 & 分页插件》学习笔记

一、教学目标

  1. 使用 LambdaQueryWrapper 构建查询条件。
  2. 配置 MyBatis-Plus 分页插件,实现新闻分页查询。

二、核心知识点

知识点 说明
LambdaQueryWrapper 类型安全的查询条件构造器,通过 Lambda 表达式引用实体类字段,避免硬编码字段名
分页插件 自动拦截 SQL 语句,添加 LIMIT 分页语法,并查询总记录数
Page 分页参数对象,用于封装页码、每页大小等分页信息

三、操作步骤

本课次是在课次24的工程基础上继续操作。
1. 创建分页配置类

  • com.weitoutiao 包下新建 config.MyBatisPlusConfig
  • 代码如下:
package com.weitoutiao.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyBatisPlusConfig {  
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 创建分页插件并指定数据库类型(根据实际使用的数据库选择)
        PaginationInnerInterceptor paginationInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
        // 可选:超出最大页码时是否处理(false表示返回空数据,true表示返回最后一页)
        paginationInterceptor.setOverflow(false);
        // 将分页插件添加到拦截器容器
        interceptor.addInnerInterceptor(paginationInterceptor);
        return interceptor;
    }
}

2. 创建 Service 层

  • 使用 MyBatis-Plus 的 IService 接口
  • com.weitoutiao 下创建 service.NewsService 接口:
package com.weitoutiao.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.weitoutiao.entity.News;
public interface NewsService extends IService<News> {
}
  • service 包下创建 impl.NewsServiceImpl 实现类:
package com.weitoutiao.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weitoutiao.entity.News;
import com.weitoutiao.mapper.NewsMapper;
import com.weitoutiao.service.NewsService;
import org.springframework.stereotype.Service;
@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements NewsService {
}

说明ServiceImpl<M extends BaseMapper<T>, T> 是 MyBatis-Plus 提供的 Service 实现基类,已封装了常用的 CRUD 和分页方法。
3. 测试分页查询

  • test 文件夹中创建测试类 NewsServiceTest
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weitoutiao.entity.News;
import com.weitoutiao.service.NewsService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class NewsServiceTest {  
    @Autowired
    private NewsService newsService;
    @Test
    void testPage() {
        Page<News> page = new Page<>(1, 5);  // 第1页,每页5条
        LambdaQueryWrapper<News> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(News::getPublishTime);  // 按发布时间降序排列
        Page<News> result = newsService.page(page, wrapper);
        System.out.println("总记录数:" + result.getTotal());
        System.out.println("当前页数据:" + result.getRecords());
    }
}

提示:代码中报红时,将鼠标悬停在错误位置,选择导入对应的类即可解决。
4. 解决测试报错

  • 运行 testPage 测试方法时,会报错:MyBatis-Plus 开启了逻辑删除功能,但 news 表中没有 deleted 字段。
  • 打开 SQLyog,执行以下语句添加 deleted 字段:
ALTER TABLE `news` ADD COLUMN `deleted` TINYINT DEFAULT 0 COMMENT '逻辑删除(0未删除,1已删除)';
  • 再次运行测试,还会报缺少 update_time 字段的错误。继续在 SQLyog 中执行:
ALTER TABLE `news` ADD COLUMN `update_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间';
  • 回到 IDEA,再次运行 testPage 测试,控制台成功打印分页结果。

四、笔记总结

步骤 核心内容
配置分页插件 创建 MyBatisPlusConfig,注册 PaginationInnerInterceptor
创建 Service 层 接口继承 IService<T>,实现类继承 ServiceImpl<M,T>
构建查询条件 使用 LambdaQueryWrapper 通过 News::getPublishTime 引用字段
分页查询 Page 对象 + service.page(page, wrapper)
字段补充 需在 news 表中添加 deletedupdate_time 字段
本课次完成了 MyBatis-Plus 分页插件的配置和 Service 层的搭建,通过 LambdaQueryWrapper 实现了类型安全的条件构造,并演示了分页查询的完整流程。
posted @ 2026-06-29 13:01  WJX-nb666  阅读(0)  评论(0)    收藏  举报