导包
链接:https://pan.baidu.com/s/1uLVyJrOKcHRFGNaPEBZ6KA
提取码:rw5f
这条博客只是给自己做个笔记,由于里面的方法提取的不够统一,不建议参照。。。。
servlet代码
UserService userService = new UserService();
public String selectStaff(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Map<String, String[]> map = request.getParameterMap();
//这个就是把前端表单发过来的数据一一放进page里面,相当于多个request.getParameter
Page page = CommonUtils.toBean(map, Page.class);
List<Staff> list = userService.selectStaff(page);
//设置数据总条数
page.setTotal(userService.count());
request.setAttribute("page", page);
request.setAttribute("userList",list);
return "f:user.jsp";
}
service代码
UserDao userDao = new UserDao();
public List<Staff> selectStaffByD(String depName){
List<Staff> list = userDao.selectByD(depName);
return list;
}
public int count(){
return userDao.count();
}
dao代码
public List<Staff> selectStaff(Page page){
List<Staff> list = new ArrayList<>();
Connection con = DBUtil.getConnection();
PreparedStatement ptmt = null;
ResultSet res = null;
//主要要注意这里的limit,其他的乱七八糟的都要改
String sql = "SELECT * FROM crm_staff,crm_post,crm_department "
+ "WHERE crm_staff.postID=crm_post.id "
+ "AND crm_post.depID=crm_department.id order by crm_staff.id "
+ "limit ?,?";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
ptmt = con.prepareStatement(sql);
ptmt.setInt(1, page.getStart());
ptmt.setInt(2, page.getCount());
res = ptmt.executeQuery();
while(res.next()){
Staff staff = new Staff();
staff.setStaffName(res.getString("staffName"));
staff.setGender(res.getString("gender"));
Date d = res.getDate("onDutyDate");
staff.setOnDutyDate(simpleDateFormat.format(d.getTime()));
staff.setDepName(res.getString("depName"));
staff.setPostName(res.getString("postName"));
staff.setId(res.getInt("id"));
list.add(staff);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
public int count(){
Connection con = DBUtil.getConnection();
PreparedStatement ptmt = null;
ResultSet res = null;
int count = 0;
String sql = "SELECT count(*) FROM crm_staff,crm_post,crm_department "
+ "WHERE crm_staff.postID=crm_post.id "
+ "AND crm_post.depID=crm_department.id";
try {
ptmt = con.prepareStatement(sql);
res = ptmt.executeQuery();
if(res.next()){
count = res.getInt(1);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return count;
}
前端代码,只要在jstl循环后面加上这个jsp片段就可以实现分页效果,记得要把bootstrap导进去
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<script>
$(function(){
$("ul.pagination li.disabled a").click(function(){
return false;
});
});
</script>
<nav>
<ul class="pagination">
<li <c:if test="${!page.hasPreviouse}">class="disabled"</c:if>>
<a href="?method=selectStaff&start=0${page.param}" aria-label="Previous" >
<span aria-hidden="true">«</span>
</a>
</li>
<li <c:if test="${!page.hasPreviouse}">class="disabled"</c:if>>
<a href="?method=selectStaff&start=${page.start-page.count}${page.param}" aria-label="Previous" >
<span aria-hidden="true">‹</span>
</a>
</li>
<c:forEach begin="0" end="${page.totalPage-1}" varStatus="status">
<c:if test="${status.count*page.count-page.start<=20 && status.count*page.count-page.start>=-10}">
<li <c:if test="${status.index*page.count==page.start}">class="disabled"</c:if>>
<a
href="?method=selectStaff&start=${status.index*page.count}${page.param}"
<c:if test="${status.index*page.count==page.start}">class="current"</c:if>
>${status.count}</a>
</li>
</c:if>
</c:forEach>
<li <c:if test="${!page.hasNext}">class="disabled"</c:if>>
<a href="?method=selectStaff&start=${page.start+page.count}${page.param}" aria-label="Next">
<span aria-hidden="true">›</span>
</a>
</li>
<li <c:if test="${!page.hasNext}">class="disabled"</c:if>>
<a href="?method=selectStaff&start=${page.last}${page.param}" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</ul>
</nav>
page类
public class Page {
private int start; //开始页数
private int count; //每页显示个数
private int total; //总个数
private String param; //参数
private static final int defaultCount = 5; //默认每页显示5条
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public Page (){
count = defaultCount;
}
public Page(int start, int count) {
super();
this.start = start;
this.count = count;
}
public boolean isHasPreviouse(){
if(start==0)
return false;
return true;
}
public boolean isHasNext(){
if(start==getLast())
return false;
return true;
}
public int getTotalPage(){
int totalPage;
// 假设总数是50,是能够被5整除的,那么就有10页
if (0 == total % count)
totalPage = total /count;
// 假设总数是51,不能够被5整除的,那么就有11页
else
totalPage = total / count + 1;
if(0==totalPage)
totalPage = 1;
return totalPage;
}
public int getLast(){
int last;
// 假设总数是50,是能够被5整除的,那么最后一页的开始就是45
if (0 == total % count)
last = total - count;
// 假设总数是51,不能够被5整除的,那么最后一页的开始就是50
else
last = total - total % count;
last = last<0?0:last;
return last;
}
@Override
public String toString() {
return "Page [start=" + start + ", count=" + count + ", total=" + total + ", getStart()=" + getStart()
+ ", getCount()=" + getCount() + ", isHasPreviouse()=" + isHasPreviouse() + ", isHasNext()="
+ isHasNext() + ", getTotalPage()=" + getTotalPage() + ", getLast()=" + getLast() + "]";
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public String getParam() {
return param;
}
public void setParam(String param) {
this.param = param;
}
}
总结:前端直接把代码加进去就行,servlet里面需要一直更新page的值,dao需要一个select里面有limit的方法和一个count(*)的方法,这样就可以实现分页了