springboot+mybatis整合分页插件

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_36984017/article/details/100032091

1:先引入jar包

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.1.6.RELEASE</version>
        </dependency>
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.1.6.RELEASE</version>
        </dependency>
<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>6.0.6</version>
        </dependency>
<dependency>
    <!--注意这个一定要引入spring-boot相关的pagehelper,不要引入网上乱七八糟的pagehelper包,还有注意版本号-->
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.7</version>
</dependency>

2:dao层(和以往一样编写代码,毫无违和感)

    List<GoodsEnitity> selectGoodsBySortId(@Param("sortId") Integer sortId, @Param("merchantId") String merchantId);

3:xml文件(和以往一样编写代码,毫无违和感)

<select id="selectGoodsBySortId" resultMap="goods">
    select
    *
    from goods
    where merchant_id = #{merchantId} and sort_id = #{sortId}
  </select>

4:service层

public PageInfo sortGoods(Map<String,Object> req,Map<String,String> msgMap) throws Exception{
        int pageNum=Integer.parseInt(req.get("page")+"");
        int pageSize=Integer.parseInt(req.get("size")+"");
        int sortId=Integer.parseInt(req.get("sortId")+"");
        String merchantId=req.get("merchantId")+"";
        if(EmptyChecker.isAnyOneEmpty(pageNum,pageSize,sortId,merchantId )){
            msgMap.put("msg","参数校验失败");
            throw new Exception("参数校验失败");
        }
        //就这一句代码重要,实现分页,一定要放在你要分页的sql语句的前一句,就像现在这样
        PageHelper.startPage(pageNum, pageSize);
        List<GoodsEnitity> goodsSortList=goodsDao.selectGoodsBySortId(sortId,merchantId);
        PageInfo<GoodsEnitity> pageInfo=new PageInfo<>(goodsSortList);
        return pageInfo;
    }
    
返回的数据形式:
{
    "result": "SUCCESS",
    "code": 200,
    "data": {
        //总条数
        "total": 6,
        //分页出来的数据列表
        "list": [
            {
                "id": 1,
                "goodsKey": "aaa",
                "sortId": 1,
            },
            {
                "id": 2,
                "goodsKey": "aaa1",
                "sortId": 1,
            }
        ],
        //当前页
        "pageNum": 1,
        //给出数据的条数
        "pageSize": 2,
        "size": 2,
        "startRow": 1,
        "endRow": 2,
        "pages": 3,
        "prePage": 0,
        "nextPage": 2,
        "isFirstPage": true,
        "isLastPage": false,
        "hasPreviousPage": false,
        "hasNextPage": true,
        "navigatePages": 8,
        "navigatepageNums": [
            1,
            2,
            3
        ],
        "navigateFirstPage": 1,
        "navigateLastPage": 3
    }
}


还有一种简单的只是单纯分页,没多余的数据形式
 public List<GoodsEnitity> sortGoods(Map<String,Object> req,Map<String,String> msgMap) throws Exception{
        int pageNum=Integer.parseInt(req.get("page")+"");
        int pageSize=Integer.parseInt(req.get("size")+"");
        int sortId=Integer.parseInt(req.get("sortId")+"");
        String merchantId=req.get("merchantId")+"";
        if(EmptyChecker.isAnyOneEmpty(pageNum,pageSize,sortId,merchantId )){
            msgMap.put("msg","参数校验失败");
            throw new Exception("参数校验失败");
        }
        //就这一句代码重要,实现分页,一定要放在你要分页的sql语句的前一句,就像现在这样
        PageHelper.startPage(pageNum, pageSize);
        List<GoodsEnitity> goodsSortList=goodsDao.selectGoodsBySortId(sortId,merchantId);
        //PageInfo<GoodsEnitity> pageInfo=new PageInfo<>(goodsSortList);
        return goodsSortList;
    }
返回的数据形式(只是分页的两条数据,没其他多余的数据):
{
    "result": "SUCCESS",
    "code": 200,
    "data": [
        {
            "id": 1,
            "goodsKey": "aaa",
            "sortId": 1
        },
        {
            "id": 2,
            "goodsKey": "aaa1",
            "sortId": 1,
        }
    ]
}

猜你喜欢

转载自blog.csdn.net/qq_36984017/article/details/100032091