专注于 JetBrains IDEA 全家桶,永久激活,教程
持续更新 PyCharm,IDEA,WebStorm,PhpStorm,DataGrip,RubyMine,CLion,AppCode 永久激活教程

Springboot使用Mybatis插件PageHelper 总结

前言

最近项目中用到pagehelper插件,这里总结一下,虽然简单,但怕以后忘记。本文参考意义不大,大牛请忽视。

使用步骤

pom

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

yaml

pagehelper:
    helper-dialect: mysql  #指定DBMS
    reasonable: true
    support-methods-arguments: true
    params: count=countSql

示例

用下面这张表category为例

54_1.png

controller层

   /**
     * 分页查询category
     * @return
     */
    @GetMapping("/list/{page}/{size}")
    public QueryResult<Category> getCategoryByPage(@PathVariable(value = "page") int page,
                                                   @PathVariable("size") int size) {
        return categoryService.getCategoryByPage(page, size); 
    }

service层

 /**
     * 分页查询category
     *
     * @return
     */
    public QueryResult<Category> getCategoryByPage(int page, int size) {
        PageHelper.startPage(page, size);  
        Page<Category> categoryPage = categoryMapper.getCategoryByPage(); // 调用dao,返回类型是 Page<>
        List<Category> result = categoryPage.getResult();  // 得到result,返回类型是 List<>
        // 返回类型封装
        QueryResult<Category> queryResult = new QueryResult<>(); 
        queryResult.setList(result);
        queryResult.setTotal(result.size());
        return queryResult;
    }

dao层

返回类型要用Page<分页的模型>

   /**
     * 分页查询category
     * @return
     */
    public Page<Category> getCategoryByPage();

Mapper.xml

<select id="getCategoryByPage" resultType="com.xuecheng.framework.domain.course.Category">
        select *
        from category
</select>

注意

select *
        from category    后面没有加 ;  结尾,加了会报错(如下)

54_2.png

后面的limit就是PageHelper作用上去的,加了 ; 号就会报语法错误 /(ㄒoㄒ)/

以上就是一个比较完整的PageHelper使用流程,官网教程也很详细,可以查阅

文章永久链接:https://tech.souyunku.com/29466

未经允许不得转载:搜云库技术团队 » Springboot使用Mybatis插件PageHelper 总结

JetBrains 全家桶,激活、破解、教程

提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,支持 IntelliJ IDEA、PyCharm、WebStorm 等工具的永久激活。无论是破解教程,还是最新激活码,均可免费获得,帮助开发者解决常见激活问题,确保轻松破解并快速使用 JetBrains 软件。获取免费的破解补丁和激活码,快速解决激活难题,全面覆盖 2024/2025 版本!

联系我们联系我们