我们要想实现分页,就必须得现有一个Page的工具类,用来存放我们的分页信息:
public class Page {
private int start = 0;
//count为11表明一页显示11条数据
private int count = 11;
private int last = 0;
public int getStart() {
return start;
}
//这么写确保了第一页的时候再点前一页不会错误,以及最后一页的时候再点后一页依然正常
public void setStart(int start) {
if (start >= 0 && start <= last) {
this.start = start;
} else if (start < 0){
this.start = 0;
} else {
this.start = last;
}
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getLast() {
return last;
}
public void setLast(int last) {
this.last = last;
}
public void calculate(int total) {
//假设总数是99,是能够被11整除的,最后一页的开始就是88
if (0 == total % count) {
last = total - count;
} else {
last = total - total % count;
}
}
}
在mapper下写:
public interface CategoryMapper {
List<Category> list(Page page);
int total();
}
在service中改一下接口和实现类:
public interface CategoryService {
int total();
List<Category> list(Page page);
}
@Service
public class CategoryServiceImpl implements CategoryService {
@Autowired
CategoryMapper categoryMapper;
@Override
public int total() {
return categoryMapper.total();
}
@Override
public List<Category> list(Page page) {
return categoryMapper.list(page);
}
}
对应的,我们查询语句也得改写一下,在我们的mybatis的xml中改:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hty.mapper.CategoryMapper">
<!--分页查询-->
<select id="list" resultType="Category">
select * from category
<if test="start != null and count != null">
limit #{start},#{count}
</if>
</select>
<!--获取总数-->
<select id="total" resultType="Integer">
select count(*) from category
</select>
</mapper>
然后就是写我们的controller了:
@Controller
public class CategoryController {
@Autowired
private CategoryService categoryService;
@RequestMapping(value = "/listCategory", method = RequestMethod.GET)
public ModelAndView listCategory(Page page) {
ModelAndView modelAndView = new ModelAndView();
List<Category> categories = categoryService.list(page);
int total = categoryService.total();
page.calculate(total);
//放入转发参数 相当于request.setAttribute("categories", categories);
modelAndView.addObject("categories", categories);
//放入jsp路径
modelAndView.setViewName("listCategory");
return modelAndView;
}
}
在我们的listCategory.jsp中写一个简单的表单来测试
ps:这里的删除跟编辑不用管,主要是看分页效果
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<table align="center" border="1" cellspacing="0">
<tr>
<td>id</td>
<td>name</td>
</tr>
<c:forEach items="${categories}" var="c" varStatus="st">
<tr>
<td>${c.id}</td>
<td>${c.name}</td>
<td><a href="${pageContext.request.contextPath}/delete/${c.id}">删除</a> </td>
<td><a href="${pageContext.request.contextPath}/editCategory?id=${c.id}">编辑</a> </td>
</tr>
</c:forEach>
</table>
<div style="text-align: center">
<a href="?start=0">首 页</a>
<a href="?start=${page.start - page.count}&last=${page.last}">上一页</a>
<a href="?start=${page.start + page.count}&last=${page.last}">下一页</a>
<a href="?start=${page.last}&last=${page.last}">末 页</a>
</div>
</body>
</html>
ok,现在运行看效果:
点击下一页,我们看地址栏的变化:
在首页点上一页:
在末页点下一页: