使用SSM框架编写项目时,有时从数据库中获取的数据太过庞大,在网页中显示会不太美观,为了避免这种情况,同时可以有效的管理数据,采用数据分页的方式。
有什么地方讲的不对,希望诸位大佬指正!
1.首先看一下效果
2.要完成分页功能,要对应四个功能:首页、上一页、下一页、尾页
要实现这样的功能,要在实体类中定义这样几个变量:
private int pageSize = 10;//每页多少数据
private int pageCount; //页总数
private int total; //数据总数
private int nowPage=1; //当前页码
private int offset; //每页第一条数据下标
我这里的实体类命名为:PageService.java(可能不准确,大家理解就好)
建立对应的GetSet方法,需要注意,我们需要注意setTotal和setNowPage这两个方法,其他的默认生成即可。
public void setTotal(int total) { //设置数据总数
pageCount = total/pageSize; //页总数=数据总数/每页容可纳的数据数(最后取整)
if(total%pageSize!=0){ //如果取余不为0,说明有额外的数据,则页数需加一
pageCount++;
}
this.total = total; //赋值给数据总数
}
public void setNowPage(int nowPage) { //设置当前页码
if(nowPage<=1){ //确保页码数不小于1
nowPage = 1;
}
offset = (nowPage-1)*pageSize;//每页第一个数据的下标算法,第一页第一个数下标为0,第二页第一个数据下标为10……以此类推
this.nowPage = nowPage;//复制给当前页码
}
3.随后就需要在xxxMapper.xml里写相应的sql语句
<select id="selectAllByPage" parameterType="com.hospital.pojo.PageService" resultType="com.hospital.pojo.Patient">
select pid,name,age
from patient
ORDER BY pid ASC
LIMIT #{offset},#{pageSize}
</select>
parameterType中写上面定义的还有分页相关变量的实体类
resultType中写sql查询出的数据对应的实体类变量
ORDER BY pid ASC 根据pid进行升序排列
LIMIT #{offset},#{pageSize} 表示从每一页第一个数据下标开始限制10个数据展示,这就是完成了一个页面展示pageSize(我这里定义的十个)个数据
4.在对应的xxxMapper.java里写
List<XXX> selectAllByPage(PageService pageService);
注意其中参数的值,XXX是指你数据库表对应的实体类
5.在XxxService接口中写
List<XXX> selectAllByPage(PageService pageService);
与上面xxxMapper.java中一致,XXX是指你数据库表对应的实体类
6.在xxxServiceImpl.java的实现类中写
@Service("xxxService") //在最外层
public class xxxServiceImpl implements xxxService{
@Autowired
private xxxMapper xxxMapper;
@Override
public List<Patient> selectAllByPage(PageService pageService) {
return patientMapper.selectAllByPage(pageService);
}
7.在Controller类中写对应的实现功能
@Controller
public class xxxController {
@Autowired
private XxxService xxxService;
@RequestMapping("search")
public String search(Integer nowpage,Model model){
PageService page = new PageService();//创建管理分页的对象
if(nowpage==null){//确保当前页码不为空
nowpage=1;
}
page.setNowPage(nowpage);
page.setTotal(xxxService.getPatientCount(page));//获取数据总数,这个对应的sql我放下面
model.addAttribute("total",page.getTotal());
model.addAttribute("pageCount",page.getPageCount());
model.addAttribute("nowPage",page.getNowPage());
model.addAttribute("pageSize",page.getPageSize());
model.addAttribute("offset",page.getOffset());
List<XXX> list = xxxService.selectAllByPage(page);
model.addAttribute("list",list);
model.addAttribute("errorCount",xxxService.getPatientErrorCount(page));//获取失效的数据总数,这个对应的sql我放下面,我这里只是认定名字为空的为失效数据
return "Test.jsp";
}
}
xxxMapper.xml里写有关sql语句,实现查询数据总数以及失效数据数
<select id="getPatientCount" parameterType="com.hospital.pojo.PageService" resultType="int">
select count(*) from patient where name!=""
</select>
<select id="getPatientErrorCount" parameterType="com.hospital.pojo.PageService" resultType="int">
select count(*) from patient where name is null
</select>
在对应的xxxMapper.java,xxxService.java,xxxServiceImpl.java里与之同步,和上面差不多,注意我这里参数代表的值。。
8.最后在jsp页面里写
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<form method="post" action="search.action">
<input type="submit" value="查询所有病人信息"/>
</form>
<hr>
<form>
<table>
<tr><td>编号</td><td>姓名</td><td>年龄</td></tr>
<c:forEach items="${list }" var="patient">
<tr>
<td><input type="text" value="${patient.pid }"/></td>
<td><input type="text" value="${patient.name }"/></td>
<td><input type="text" value="${patient.age }"/></td>
</tr>
</c:forEach>
</table>
<a href="searchPatient.action?nowpage=1">首页</a>
<a href="searchPatient.action?nowpage=${nowPage-1 }">上一页</a>
<c:if test="${nowPage<pageCount }">
<a href="searchPatient.action?nowpage=${nowPage+1 }">下一页</a>
</c:if>
<c:if test="${nowPage>=pageCount }">
<a href="searchPatient.action?nowpage=${pageCount }">下一页</a>
</c:if>
<a href="searchPatient.action?nowpage=${pageCount }">尾页</a>
应该分为多少页:${pageCount }<br>
当前页数:${nowPage }<br>
当前页数下标:${offset }<br>
每页多少数据:${pageSize }<br>
数据总数量:${total }<br>
无效数据数量:${errorCount }<br>
</form>
</body>
</html>
jsp页面中与分页无关的我都删除了,其他的应该可以看得懂。
最后再附上我的项目结构图:
希望可以帮助到大家!