分页

在pom文件中加两个依赖jar包

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>4.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.jsqlparser/jsqlparser -->
<dependency>
    <groupId>com.github.jsqlparser</groupId>
    <artifactId>jsqlparser</artifactId>
    <version>0.9.5</version>
</dependency>

mybatis-config.xml 里配置插件:

<!-- 配置分页插件 -->
<plugins>
   <!-- com.github.pagehelper为PageHelper类所在包名 -->
   <plugin interceptor="com.github.pagehelper.PageHelper">
      <!-- 4.0.0以后版本可以不设置该参数 -->
      <property name="dialect" value="mysql"/>

   </plugin>
</plugins>

jsp页面:
注意:(
<c:forEach items="${pagelist.list}" var="s">  用页面list调用原本的list集合
<span>${pagelist.total}条记录当前显示</span><span>现在显示第${pagelist.pageNum}</span>

<a href="/stock/mhselect.action?pageNo=${pagelist.firstPage}&pageSize=${pagelist.pageSize}" >首页</a>

<c:choose>
    <c:when test="${pagelist.isFirstPage==true}">
        <a href="/stock/mhselect.action?pageNo=${pagelist.firstPage}&pageSize=${pagelist.pageSize}" >上一页</a>

    </c:when>

    <c:otherwise>
        <a href="/stock/mhselect.action?pageNo=${pagelist.prePage}&pageSize=${pagelist.pageSize}" >上一页</a>

    </c:otherwise>

</c:choose>

<c:choose>
    <c:when test="${pagelist.isLastPage==true}">
        <a href="/stock/mhselect.action?pageNo=${pagelist.lastPage}&pageSize=${pagelist.pageSize}" >下一页</a>

    </c:when>

    <c:otherwise>
        <a href="/stock/mhselect.action?pageNo=${pagelist.nextPage}&pageSize=${pagelist.pageSize}" >下一页</a>

    </c:otherwise>

</c:choose>



<a href="/stock/mhselect.action?pageNo=${pagelist.lastPage}&pageSize=${pagelist.pageSize}">尾页</a>



controller不分页的代码
@RequestMapping(value = "/mhselect.action", method = RequestMethod.GET)
public String toall(String stockName, Model model) {
    HashMap<String, Object> map = new HashMap<>();
    System.out.println(stockName);
    List<Stock> list = stockService.mhselect(stockName);
    System.out.println(list);

    model.addAttribute("list", list);

    return "/all";
}
controller分页后的代码:
//查询所有带分页
@RequestMapping(value = "/mhselect.action",method = RequestMethod.GET)
public  String findStockAll(Model model,String pageNo, String pageSize,String stockName){
    /**
     *  * @param pageNum      页码
     * @param pageSize     每页显示数量
     * @param count        是否进行count查询
     * @param reasonable   分页合理化,null时用默认配置
     * @param pageSizeZero true且pageSize=0时返回全部结果,false时分页,null时用默认配置
     */
    int num = 1;
    int size = 3;

    if(pageNo != null && !"".equals(pageNo)) {
        num = Integer.parseInt(pageNo);
    }
    if (pageSize != null && !"".equals(pageSize)) {
        size = Integer.parseInt(pageSize);
    }
    //开始分页
    PageHelper.startPage(num,size);

    //查询数据库信息

    List<Stock> list = stockService.mhselect(stockName);
    System.out.println(list);
    //将信息放入Pagelist进行分页
    PageInfo<Stock> pagelist = new PageInfo<Stock>(list);

    System.out.println(pagelist.getPageNum());
    System.out.println(pagelist.getPageSize());
    System.out.println(pagelist.getTotal());
    System.out.println(pagelist.getFirstPage());
    System.out.println(pagelist.getLastPage());
    System.out.println(pagelist.getPrePage());
    System.out.println(pagelist.getNextPage());

    model.addAttribute("pagelist",pagelist);
    return  "/all";
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值