添加bean.PageInfo
package com.zhongruan.bean;
import java.util.List;
public class PageInfo {
private List<T> list;
private int size;
private int totalPage;
private int totalCount;
private int currentPage;
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
@Override
public String toString() {
return "PageInfo{" +
"list=" + list +
", size=" + size +
", totalPage=" + totalPage +
", totalCount=" + totalCount +
", currentPage=" + currentPage +
'}';
}
}
更新findAll方法
service.UserService
PageInfo<User> findAll(int currentPage,String username);
// List findAll();
service.impl.UserServiceImpl
@Override
public PageInfo<User> findAll(int currentPage, String username) {
PageInfo<User> pageInfo = new PageInfo<>();
pageInfo.setSize(5);
//tc为查询数据的总行数
int tc = userDao.getTotalCount(username);
pageInfo.setTotalCount(tc);
//tp为总页数
int tp = (int)Math.ceil(tc/5.0);
pageInfo.setTotalPage(tp);
if(currentPage<1){
pageInfo.setTotalCount(1);
}else if(currentPage>tp){
pageInfo.setCurrentPage(tp);
}else {
pageInfo.setCurrentPage(currentPage);
}
//每一页开始搜索的位置
int start = (pageInfo.getCurrentPage()-1)*5;
List<User> userList = userDao.findAll(start,username);
pageInfo.setList(userList);
return pageInfo;
}
// @Override
// public List findAll() {
// return userDao.findAll();
// }
dao.UserDao
List<User> findAll(@Param("start") int start, @Param("username") String username);
// List findAll();
controller.UserController
// @RequestMapping("/findAll.do")
// public ModelAndView findAll(){
// List users = userService.findAll();
// ModelAndView mv = new ModelAndView();
// mv.addObject(“users”,users);
// mv.setViewName(“user-list”);
// return mv;
// }
@RequestMapping("/findAll.do")
public ModelAndView findAll(@RequestParam(defaultValue = "1")int currentPage, String username,
@RequestParam(defaultValue = "0")int type, HttpSession session){
if(type==1){
session.setAttribute("searchName",username);
}else{
username=(String) session.getAttribute("searchName");
}
PageInfo<User> pageInfo = userService.findAll(currentPage,username);
ModelAndView mv = new ModelAndView();
mv.addObject("pageInfo",pageInfo);
mv.setViewName("user-list");
return mv;
}
UserMapper.xml
<!--<select id="findAll" resultType="user">-->
<!--select * from tb_user-->
<!--</select>-->
<select id="findAll" resultType="user">
select * from tb_user
<if test="username!=null and username!=''">
where username like concat ("%",#{username},"%")
</if>
limit #{start},5
</select>
更新user-list.jsp
<div class="box-tools pull-right">
<ul class="pagination">
<li><a href="/user/findAll.do" aria-lable="Previous">首页</a></li>
<li><a href="/user/findAll.do?currentPage=${pageInfo.currentPage-1}">上一页</a></li>
<c:forEach begin="1" end="${pageInfo.totalPage}" var="pageNum">
<li><a href="/user/findAll.do?currentPage=${pageNum}">${pageNum}</a></li>
</c:forEach>
<li><a href="/user/findAll.do?currentPage=${pageInfo.currentPage+1}">下一页</a></li>
<li><a href="/user/findAll.do?currentPage=${pageInfo.totalPage}" aria-lable="Previous">尾页</a></li>
</ul>
</div>