SSM实现分页查询
程序员文章站
2024-03-14 20:08:05
...
前言
分页基本上是我们项目中所必须的功能,当数据量过大时,可能会导致各种各样的问题发生,例如:服务器资源被耗尽,因数据传输量过大而使处理超时,等等。最终都会导致查询无法完成。解决这个问题的一个策略就是“分页查询”,也就是说不要一次性查询所有的数据,每次只查询一“页“的数据。这样分批次地进行处理,可以呈现出很好的用户体验,对服务器资源的消耗也不大。本文主要是使用limit来实现分页的查询。
实现
1、分页工具类
package com.inchlifc.util;
import java.util.List;
/**
* @author 小小渔夫
* @description 分页实体类
*/
public class PageBean<T> {
private int currPage;//当前页数
private int pageSize;//每页显示的记录数
private int totalCount;//总记录数
private int totalPage;//总页数
private List<T> lists;//每页的显示的数据
public PageBean() {
super();
}
public int getCurrPage() {
return currPage;
}
public void setCurrPage(int currPage) {
this.currPage = currPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public List<T> getLists() {
return lists;
}
public void setLists(List<T> lists) {
this.lists = lists;
}
}
2、mapper层
<select id="selectInStockList" resultType="com.inchlifc.entity.InStock">
SELECT * FROM in_stock ORDER BY createTime desc
<if test="pageStart!=null and pageSize!=null">
limit #{pageStart},#{pageSize}
</if>
</select>
3、Dao层
/**
* 分页查询所有信息
*
* @param pageStart 页数
* @param pageSize 每页显示条数
* @return
*/
List<InStock> selectInStockList(@Param("pageStart") int pageStart, @Param("pageSize") int pageSize);
4、service层
/**
* 分页查询所有信息
*
* @return
*/
public PageBean<InStock> findAllInStock(int pageStart) {
PageBean<InStock> pageBean = new PageBean<InStock>();
pageBean.setCurrPage(pageStart);
pageBean.setPageSize(10);
//封装总记录数
int totalCount = inStockDao.inStockCountAll();
pageBean.setTotalCount(totalCount);
//封装总页数
double tc = totalCount;
Double num = Math.ceil(tc / 10);//向上取整
pageBean.setTotalPage(num.intValue());
//封装每页显示的数据
List<InStock> lists = inStockDao.selectInStockList((pageStart - 1) * 10, 10);
pageBean.setLists(lists);
return pageBean;
}
5、controller层
/**
* 分页查询所有信息
* @param currentPage 当前页数
* @param model model
* @param session 会话
* @return
*/
@RequestMapping(value = "/list", method = RequestMethod.GET)
@ResponseBody
private ModelAndView getInStockList(@RequestParam(value = "currentPage", defaultValue = "1", required = false) int currentPage, Model model, HttpSession session) {
ModelAndView mv = new ModelAndView();
//获取当前登录用户名
String currentUserName = (String) session.getAttribute("userName");
if (!currentUserName.equals("engine")) {
mv.setViewName("permission");
return mv;
}
mv.addObject("stockList", inStockService.findAllInStock(currentPage));
mv.setViewName("inStockList");
return mv;
}
6、显示页面
<ul style="font-size: large;text-align: center">
<span>第${requestScope.stockList.currPage }/ ${requestScope.stockList.totalPage}页</span>
<span>总记录数:${requestScope.stockList.totalCount } 每页显示:${requestScope.stockList.pageSize}</span>
<span>
<c:if test="${requestScope.stockList.currPage != 1}">
<a href="<%=basePath%>inStock/list?currentPage=1">[首页]</a>
<a href="<%=basePath%>inStock/list?currentPage=${requestScope.stockList.currPage-1}">[上一页]</a>
</c:if>
<c:if test="${requestScope.stockList.currPage != requestScope.stockList.totalPage}">
<a href="<%=basePath%>inStock/list?currentPage=${requestScope.stockList.currPage+1}">[下一页]</a>
<a href="<%=basePath%>inStock/list?currentPage=${requestScope.stockList.totalPage}">[尾页]</a>
</c:if>
</span>
</ul>
7、结果显示
上一篇: JDK 安装配置
下一篇: BigDecimal