pagehelper使用记录

1. controller层

pageNum代表当前多少页,pageSize代表当前每页有多少条记录
你传进去service返回一个pageinfo对象
然后把这个pageinfo对象的getlist存到model里面

 /*分页列表*/
    @RequestMapping("/goods/goodsList")
    public String goodsList(ModelMap modelMap,
                            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                            @RequestParam(value = "pageSize", defaultValue = "2") int pageSize){

        PageInfo<Product> pageInfo = productsService.findAllProduct(pageNum, pageSize);
        //查询商品分类
        List<Category> categoryList = productsService.slectCategory();
        modelMap.addAttribute("categoryList",categoryList);
        System.out.println(pageInfo.getList());
        modelMap.addAttribute("productList",pageInfo.getList());
        modelMap.addAttribute("productPageInfo",pageInfo);
        modelMap.addAttribute("totalNum",pageInfo.getTotal());//查询回显
        return "/manager/goods/goods-list";
    }

2.service层

其他步骤和原来一样,你只需要把查出来的对象集合,加入到pageinfo里面就可以自动算出来了。
PageInfo pageInfo = new PageInfo<>(allProduct);

  @Override
    public PageInfo<Product> findAllProduct(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
       List<Product> allProduct = productMapper.selectProductAndProductImage();
       // List<Product> allProduct = productMapper.findAllProduct(categoryId,reviewCount,searchStr);
        //pagehelper内部计算总共有多少页
        System.out.println("我是计算出来的"+allProduct);
        PageInfo<Product> pageInfo = new PageInfo<>(allProduct);
        return pageInfo;
    }

3.前端

<tr th:each="x:${productList}" rowspan="2">
            <td >
              <input th:value="${x.id}"  id="orderId" type="checkbox" name="" lay-skin="primary"></td>
            <td  th:text="${x.id}"></td>
            <td th:each="y:${x.productImages}">
              <div th:if="${y.type}==0">
                <img height="50" width="50"  th:src="@{${y.url}}" alt="">
              </div>
            </td>
            <td colspan="3">
              <div style="text-align: center">
                <span  th:text="${x.name}"></span>
              </div >
              <div style="text-align: center">
                <span  th:text="${x.seller}"></span>
              </div></td>
            <td colspan="3" th:text="${x.subTitle}"></td>
            <td th:text="${x.originalPrice}"></td>
            <td th:text="${x.number}"></td>
            <td th:text="${x.stockCount}"></td>
            <td th:text="${x.saleCount}"></td>
            <td class="td-status" th:if="${x.reviewCount==0}">
              <span class="layui-btn layui-btn-normal layui-btn-mini">已上架</span></td>
            <td class="td-status" th:if="${x.reviewCount==1}">
              <span class="layui-btn layui-btn-normal layui-btn-mini layui-btn-disabled">已下架</span></td>
            <td th:text="${#dates.format(x.createDate, 'yyyy-MM-dd')}"></td>
            <td class="td-manage" th:if="${x.reviewCount==0}">
              <a onclick="member_stop(this,'10001')" href="javascript:;"  title="下架">
                <i class="layui-icon">&#xe601;</i>
              </a>
              <a title="编辑"  onclick="update(this)" href="javascript:;">
                <i class="layui-icon">&#xe642;</i>
              </a>
              <a title="删除" onclick="member_del(this,'要删除的id')" href="javascript:;">
                <i class="layui-icon">&#xe640;</i>
              </a>
            </td>
            <td class="td-manage" th:if="${x.reviewCount==1}">
              <a onclick="member_stop(this,'10001')" href="javascript:;"  title="上架">
                <i class="layui-icon">&#xe601;</i>
              </a>
              <a title="编辑"  onclick="update(this)" href="javascript:;">
                <i class="layui-icon">&#xe642;</i>
              </a>
              <a title="删除" onclick="member_del(this,'要删除的id')" href="javascript:;">
                <i class="layui-icon">&#xe640;</i>
              </a>
            </td>
          </tr>
        </tbody>
      </table>
      <div class="layui-card-body ">
        <div class="page">
          <!--上一页-->
          <a class="prev" th:if="${productPageInfo.pageNum}>1" th:href="@{'/manager/goods/goodsList?pageNum='+${productPageInfo.prePage}}">&lt;&lt;</a>
          <!--当前页,选中为红色,未选中为黑色-->
          <th:block th:each="num:${productPageInfo.navigatepageNums}" >
            <a th:class="${num==productPageInfo.pageNum}" th:href="@{'/manager/goods/goodsList?pageNum='+${num}}" th:text="${num}" th:if="${num}==${productPageInfo.pageNum}" style="color: red" ></a>
            <a th:class="${num==productPageInfo.pageNum}" th:href="@{'/manager/goods/goodsList?pageNum='+${num}}" th:text="${num}" th:if="${num}!=${productPageInfo.pageNum}" style="color: black" ></a>
          </th:block>
          <!--下一页-->
          <a class="next" th:if="${productPageInfo.pageNum}<${productPageInfo.pages}" th:href="@{'/manager/goods/goodsList?pageNum='+${productPageInfo.nextPage}}">&gt;&gt;</a>
        </div>
      </div>

4.关于搜索分页

前端我是用的form表单
注意就是name需要和你后端接受的值相互对应

<form class="layui-form layui-col-md12 x-so" action="/manager/goods/sreachList">
          <div class="layui-input-inline">
            <select name="categoryId" id="categoryId">
              <div th:each="y:${categoryList}">
                <option th:text="${y.name}" th:value="${y.id}" ></option>
              </div>
            </select>
          </div>
          <div class="layui-input-inline">
            <select name="reviewCount" id="reviewCount">
              <option value="">上架状态</option>
              <option value="0">上架</option>
              <option value="1">下架</option>
            </select>
          </div>
          <input type="text" name="searchStr" id="searchStr"  placeholder="请输入" autocomplete="off" class="layui-input">
          <button class="layui-btn"  lay-submit="" lay-filter="sreach"><i class="layui-icon">&#xe615;</i></button>
        </form>

这是控制层
需要注意的是,我这的categoryId,reviewCount,searchStr都是自定义的

    /*搜索*/
    @RequestMapping("/goods/sreachList")
    public String sreachList(ModelMap modelMap,
                            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                            @RequestParam(value = "pageSize", defaultValue = "2") int pageSize,
                            @RequestParam(value = "categoryId", defaultValue= "1") int categoryId,
                            @RequestParam(value = "reviewCount", defaultValue = "0") int reviewCount,
                            @RequestParam(value = "searchStr", defaultValue =" ") String searchStr){
        //查询商品分类
        List<Category> categoryList = productsService.slectCategory();
        modelMap.addAttribute("categoryList",categoryList);
        System.out.println("进来的搜索内容"+categoryId+" " +reviewCount +" " +searchStr);
        PageInfo<Product> pageInfo = productsService.sreachProduct(pageNum, pageSize,categoryId,reviewCount,searchStr);
        System.out.println("我是搜索出啦的"+pageInfo.getList());
        modelMap.addAttribute("productList",pageInfo.getList());
        modelMap.addAttribute("productPageInfo",pageInfo);
        modelMap.addAttribute("inputName",searchStr);//查询回显
        modelMap.addAttribute("totalNum",pageInfo.getTotal());//查询回显
        return "/manager/goods/goods-list";
    }

逻辑代码其实是差不多的,唯一的区别就是,这个去查的时候多了些条件,然后我们照样把他加入到里面就行了。

下面是我mapper文件

  <!--分页查询-->
  <select id="findAllProduct" resultMap="BaseResultMap">
    SELECT
    p.*,pi.product_id,pi.type,pi.url,pi.alt
    FROM
    product as p
    left join
    product_image as pi
    on
    p.id=pi.product_id
    <where>
      pi.type=0
      <if test="categoryId != null and categoryId != '' ">
       and   p.category_id =#{categoryId}
      </if>

      <if test="reviewCount != null and reviewCount != '' ">
        and p.review_count=#{reviewCount}
      </if>
      <if test="searchStr!=null and searchStr!='' ">
        AND
        CONCAT(p.name,p.sub_title,p.original_price,p.stock_count,p.sale_count,p.number,p.seller) LIKE CONCAT ('%', #{searchStr},'%')
      </if>
    </where>
  </select>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

轩成笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值