mybatis-plus分页查询的3种方法

今天小编给大家分享的是mybatis-plus分页查询的3种方法,相信很多人都不太了解,为了让大家更加了解,所以给大家总结了以下内容,一起往下看吧。一定会有所收获的哦。

一、前期准备表

CREATE TABLE `school_student` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `sex` varchar(255) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;
INSERT INTO `avlicy`.`school_student`(`id`, `name`, `sex`, `age`) VALUES (1, 'av峰峰', '男', 1);
INSERT INTO `avlicy`.`school_student`(`id`, `name`, `sex`, `age`) VALUES (2, '卢本伟', '男', 12);
INSERT INTO `avlicy`.`school_student`(`id`, `name`, `sex`, `age`) VALUES (3, '小米粥', '女', 13);
INSERT INTO `avlicy`.`school_student`(`id`, `name`, `sex`, `age`) VALUES (4, '黄米粥', '女', 15);
INSERT INTO `avlicy`.`school_student`(`id`, `name`, `sex`, `age`) VALUES (5, '蓝米粥', '女', 11);
INSERT INTO `avlicy`.`school_student`(`id`, `name`, `sex`, `age`) VALUES (6, '白米粥', '女', 17);
INSERT INTO `avlicy`.`school_student`(`id`, `name`, `sex`, `age`) VALUES (7, '红米粥', '女', 15);
INSERT INTO `avlicy`.`school_student`(`id`, `name`, `sex`, `age`) VALUES (8, '橙米粥', '女', 16);
INSERT INTO `avlicy`.`school_student`(`id`, `name`, `sex`, `age`) VALUES (9, '青米粥', '女', 13);
INSERT INTO `avlicy`.`school_student`(`id`, `name`, `sex`, `age`) VALUES (10, '紫米粥', '女', 12);

1、配置类

@Configuration
//@MapperScan("com.example.demo.mapper")
public class MybatisPlusConfig {
    /**
     * 新增分页拦截器,并设置数据库类型为mysql
     * @return
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

二、使用selectPage

1、Service

        //分页参数
        Page<SchoolStudent> rowPage = new Page(page, pageSize);
        //queryWrapper组装查询where条件
        LambdaQueryWrapper<SchoolStudent> queryWrapper = new LambdaQueryWrapper<>();
        rowPage = this.baseMapper.selectPage(rowPage, queryWrapper);
        return rowPage;

2、结果

mybatis-plus分页查询的3种方法  mybatis-plus 第1张

mybatis-plus分页查询的3种方法  mybatis-plus 第2张

三、使用2种分页查询的写法

1、xml

    <select id="getPageStudentTwo" resultType="com.example.demo.entity.base.SchoolStudent">
        select * from school_student
    </select>

2、Mapper

说明:

  • 1、mybatis-plus中分页接口需要包含一个IPage类型的参数。

  • 2、多个实体参数,需要添加@Param参数注解,方便在xml中配置sql时获取参数值。 注意这里我虽然加了@Param但是我并没有使用

Page<SchoolStudent> getPageStudentTwo(Page<SchoolStudent> rowPage,@Param("schoolStudent") SchoolStudent schoolStudent);

3、第一种写法

    @Override
    public IPage<SchoolStudent> getPageStudentTwo(Integer current, Integer size) {
        Page<SchoolStudent> rowPage = new Page(current, size);
        SchoolStudent schoolStudent = new SchoolStudent();
        rowPage = this.baseMapper.getPageStudentTwo(rowPage, schoolStudent);
        return rowPage;
    }

4、第一种结果

mybatis-plus分页查询的3种方法  mybatis-plus 第3张

5、第二种写法

    @Override
    public IPage<SchoolStudent> getPageStudentThree(Integer current, Integer size) {
        SchoolStudent schoolStudent = new SchoolStudent();
        Page pageStudentTwo = this.baseMapper.getPageStudentTwo(new Page(current, size), schoolStudent);
        return pageStudentTwo;
    }

6、第二种结果

mybatis-plus分页查询的3种方法  mybatis-plus 第4张

四、使用PageHelper插件分页查询

1、依赖

        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.4.5</version>
        </dependency>

2、代码

    @Override
    public PageInfo<SchoolStudent> getPageStudentFour(Integer current, Integer size) {
        //获取第1页,10条内容,默认查询总数count
        PageHelper.startPage(current, size);
        List<SchoolStudent> list = this.list();
        //用PageInfo对结果进行包装
        PageInfo page = new PageInfo(list);
        return page;
    }

3、结果

这是控制台打印的查询语句,大家发现最后的LIMIT 函数没,正常来说mybatis-plus里是没有写的,是pagehelper加上去。我顿时觉得,对于一个初级程序员的我来说,还有好多要学的。

PageHelper.startPage(pageNum, pageSize)这个地方设置的两个值,pagehelper会在你执行查询语句的时候帮你加上去,也就是LIMIT 的两个参数,第一个参数是LIMIT 的起始下标,pagehelper会根据pageNum和pageSize自动给你算出;第二个参数是LIMIT的 数据量,也就是pageSize。而且我发现,pagehelper会执行两遍你写的查询语句,第一遍会进行count(0),查出总条数,第二遍就会利用你设置的参数帮你分页查询出pageSize条数据。

我之前想先进行树排序后再进行分页的想法,在使用pagehelper时是行不通的,因为会影响pagehelper的自动分页。因此我得出在进行pagehelper分页的时候不可以给查询出的数据进行其他排序操作(查询语句中写order by是可以的),这可能就是pagehelper的局限之处,不过我相信应该有解决办法,等我找到了再分享出来。

mybatis-plus分页查询的3种方法  mybatis-plus 第5张

关于mybatis-plus分页查询的3种方法就分享到这里了,希望以上内容可以对大家有一定的参考价值,可以学以致用。如果喜欢本篇文章,不妨把它分享出去让更多的人看到。

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:niceseo6@gmail.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

评论

有免费节点资源,我们会通知你!加入纸飞机订阅群

×
天气预报查看日历分享网页手机扫码留言评论电报频道链接