MyBatis-Plus分页查询方式

分页查询基本方式

java 复制代码
@SpringBootTest(classes = LearningApplication.class)
public class MPTest {
    @Autowired
    ILearningLessonService lessonService;

    @Test
    public void test(){
        /**
         * Page<LearningLesson>:MyBatisPlus提供的分页对象
         * 1:当前页数
         * 2:每页显示的记录数
         */
        Page<LearningLesson> page = new Page<>(1, 2);

        /**
         * wrapper:使用Lambda表达式构造的查询条件。
         */
        LambdaQueryWrapper<LearningLesson> wrapper = new LambdaQueryWrapper<>();
        //用户 id 等于 2 的记录
        wrapper.eq(LearningLesson::getUserId, 2);
        
        //业务层的服务类调用分页查询方法   (page, wrapper) -> (分页对象,查询条件)
        lessonService.page(page, wrapper);
    }

按照某一字段升序或者降序排序

java 复制代码
	@Test
    public void test1(){
        Page<LearningLesson> page = new Page<>(1, 2);

		/**
		*构建按照什么字段进行排序
		*/
        List<OrderItem> itemList = new ArrayList<>();
        //指定排序的对象
        OrderItem item = new OrderItem();
        item.setColumn("latest_learn_time");//按什么latest_learn_time字段排序
        item.setAsc(false);//降序排序
        itemList.add(item);
        //排序条件添加到分页对象中
        page.addOrder(itemList);

        LambdaQueryWrapper<LearningLesson> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LearningLesson::getUserId, 2);
        lessonService.page(page, wrapper);
    }

简化写法

java 复制代码
    @Test
    public void test2(){
    	//分页对象
        Page<LearningLesson> page = new Page<>(1, 2);
		
		//构建分页查询条件
        List<OrderItem> itemList = new ArrayList<>();
        OrderItem item = new OrderItem();
        item.setColumn("latest_learn_time");
        item.setAsc(false);
        itemList.add(item);
        page.addOrder(itemList);
        
        //构造查询条件和调用分页查询方法一起。
        lessonService.lambdaQuery()
                .eq(LearningLesson::getUserId,2)
                .page(page);
    }
相关推荐
码视野3 分钟前
基于 Spring Boot + Vue3 的【城市地下燃气管网微泄漏感知与相邻地下空间燃爆预警中台】设计与实现(含PRD/三端高保真源码/大屏)
java·前端·人工智能·spring boot·后端
2601_9620715724 分钟前
数据库系统架构与DBMS功能探微:现代信息时代数据管理的关键
java·开发语言·数据库
梦想的旅途226 分钟前
如何高效调用企业微信通讯录API管理组织架构
java·开发语言·企业微信
2601_962065251 小时前
2.启动mysql容器
java·数据库·mysql
2601_962065491 小时前
PHP For 循环
android·java·php
2601_962201721 小时前
Java进阶,集合,Colllection,常见数据结构
java·数据结构·windows
彷徨而立1 小时前
【C/C++】多线程读写普通 int 变量的一些问题
java·c语言·c++
卓怡学长1 小时前
w175基于springboot校园二手物品交易平台
java·spring boot·spring·maven·intellij-idea
chen<>1 小时前
malloc 和 free 背后发生了什么?
java·linux·服务器·操作系统
sunzy_泽阳1 小时前
Java + Spring 实现 Hermes Agent:从源码看多模型接入、子代理、人审与沙箱
java