1. 写一个储存页面数据的类PageBean,计算的值写好方便使用
package com.dmc.util;
import java.util.List;
public class PageBean<T> {
//当前页
private Integer localPage;
//总页数
private Integer totalPage;
//每页显示数量
private Integer pageSize = 5;
//总数据量
private Integer totalNum;
//首页
private Integer firstPage = 1;
//上一页
private Integer prePage;
//下一页
private Integer nextPage;
//尾页
private Integer lastPage;
//显示的数据
private List<T> list;
public PageBean() {
}
//只需要传入两个值就可以将其他几个值算出来,将这个算法写好方便使用
public PageBean(Integer localPage, Integer totalNum) {
this.localPage = localPage;
this.totalNum = totalNum;
//保证总页数为整数
this.totalPage = this.totalNum%this.pageSize==0?this.totalNum/this.pageSize:this.totalNum/this.pageSize+1;
this.lastPage = this.totalPage;
//上一页,为一表示第一页不再减一
this.prePage = this.localPage==1?1:this.localPage-1;
this.nextPage = this.localPage==this.totalPage?this.totalPage:this.localPage+1;
}
public Integer getLocalPage() {
return localPage;
}
public void setLocalPage(Integer localPage) {
this.localPage = localPage;
}
public Integer getTotalPage() {
return totalPage;
}
public void setTotalPage(Integer totalPage) {
this.totalPage = totalPage;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Integer getTotalNum() {
return totalNum;
}
public void setTotalNum(Integer totalNum) {
this.totalNum = totalNum;
}
public Integer getFirstPage() {
return firstPage;
}
public void setFirstPage(Integer firstPage) {
this.firstPage = firstPage;
}
public Integer getPrePage() {
return prePage;
}
public void setPrePage(Integer prePage) {
this.prePage = prePage;
}
public Integer getNextPage() {
return nextPage;
}
public void setNextPage(Integer nextPage) {
this.nextPage = nextPage;
}
public Integer getLastPage() {
return lastPage;
}
public void setLastPage(Integer lastPage) {
this.lastPage = lastPage;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
@Override
public String toString() {
return "PageBean [localPage=" + localPage + ", totalPage=" + totalPage + ", pageSize=" + pageSize
+ ", totalNum=" + totalNum + ", firstPage=" + firstPage + ", prePage=" + prePage + ", nextPage="
+ nextPage + ", lastPage=" + lastPage + ", list=" + list + "]";
}
}
2.jsp中的代码
忽略其中较长的请求和参数,每个href中数据都将传到后台进行查询
这里已经用到了上面的pageBean里面的所有字段
<nav class="navbar-right">
<ul class="pagination" id="paging">
<li>
<span>当前第${pageBean.localPage }页</span>
</li>
<li>
<a href="jobs/go?title=${condition.title }&positiontype=${condition.positiontype}">
<span aria-hidden="true">首页</span>
</a>
</li>
<li>
<a href="jobs/go?localPage=${pageBean.prePage }&title=${condition.title }&positiontype=${condition.positiontype}" aria-label="上一页"><!-- system/jobs/previousPage -->
<span aria-hidden="true">上一页</span>
</a>
</li>
<li>
</li>
<li>
<a href="jobs/go?localPage=${pageBean.nextPage }&title=${condition.title }&positiontype=${condition.positiontype}" aria-label="下一页"><!-- system/jobs/nextPage -->
<span aria-hidden="true">下一页</span>
</a>
</li>
<li>
<a href="jobs/go?localPage=${pageBean.lastPage }&title=${condition.title }&positiontype=${condition.positiontype}" aria-label="尾页">
<span aria-hidden="true">尾页</span>
</a>
</li>
<li>
<span>总页数:共${pageBean.totalPage }页</span>
<span>总数据:共${pageBean.totalNum }条</span>
</li>
</ul>
</nav>
3.Controller代码:
@Autowired
private IJobsService service;
@RequestMapping("/go")
public String show(Integer localPage , JobsCondition condition , Model mo){
PageBean<Jobs> pageBean = service.page(localPage,condition);
mo.addAttribute("pageBean",pageBean);
mo.addAttribute("condition",condition);
return "join_us_info";
}
4.业务层Service代码:
@Override
@Override
public PageBean<Jobs> page(Integer localPage) {
Integer totalNum = dao.findTotalNum();
if(localPage == null){
localPage = 1;
}
PageBean<Jobs> pageBean = new PageBean<>(localPage,totalNum);
List<Jobs> l = dao.findList((pageBean.getLocalPage()-1)*pageBean.getPageSize(),pageBean.getPageSize());
pageBean.setList(l);
return pageBean;
}
5.Dao代码:
@Override
public Integer findTotalNum(String sql2) {
String sql = "select count(id) from jobs where isenabled=1" + sql2;
return temple.queryForObject(sql, Integer.class);
}
@Override
public List<Jobs> findList(int i, Integer pageSize, String sql2) {
String sql = "select * from view_jobs_citys where isenabled=1 "+ sql2 +" limit ? , ? ";
return temple.query(sql, new BeanPropertyRowMapper<Jobs>(Jobs.class) , i , pageSize);
}
6.前端页面-数据展示:
<!--职位列表-->
<div class="container job-table">
<table class="table table-hover">
<tr>
<th class="hidden-sm">编号</th>
<th>工作职位</th>
<th>地点</th>
<th>人数</th>
<th>薪资待遇</th>
<th>是否启用</th>
<th>发布时间</th>
<th>操作</th>
</tr>
<tbody id="tbody">
<c:forEach items="${pageBean.list }" var="job">
<tr>
<th>#${job.id }</th>
<th>${job.title }</th>
<th>${job.address }</th>
<th>${job.jobnum }</th>
<th>${job.treatment }</th>
<th>
<c:if test="${!job.isenabled}" var="s">
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
</c:if>
<c:if test="${!s }">
<span class="glyphicon glyphicon-ok" aria-hidden="true"></span>
</c:if>
</th>
<th>${job.inputdate }</th>
<th>
<a href="jobs/addJsp" class="btn-default tableA"><span class="glyphicon glyphicon-plus" aria-hidden="true">添加</span></a>
<a href="jobs/update?id=${job.id}" class="btn-default tableA"><span class="glyphicon glyphicon-pencil" aria-hidden="true">修改</span></a>
<a href="jobs/del?id=${job.id}" class="btn-default tableA"><span class="glyphicon glyphicon-trash" aria-hidden="true">删除</span></a>
</th>
</tr>
</c:forEach>
</tbody>
</table>