在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"; }
4297

被折叠的 条评论
为什么被折叠?



