mybatis使用PageInterceptor插件进行查询分页方法:
一. 加载包
1.1 Spring中如下加载
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.3.3</version> </dependency> <dependency> <groupId>com.github.jsqlparser</groupId> <artifactId>jsqlparser</artifactId> <version>4.6</version> </dependency>
1.2 SpringBoot中如下加载
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>2.1.0</version> </dependency>
二. 配置
2.1 spring中在mybatis-config.xml文件中配置
<!-- 配置分页插件 --> <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库--> <property name="helperDialect" value="mysql"/> </plugin> </plugins>
2.2 springboot在application.yml中配置
pagehelper: helper-dialect: mysql
三. 调用
@RequestMapping(value = "/studentList",method = RequestMethod.GET)
public String studentList(Student student, Model model,
@RequestParam(defaultValue = "1") Integer page,
HttpServletRequest request){
PageHelper.startPage(page,2);//开始分页
List<Student> list = studentService.studentAll(student);
PageInfo<Student> pageInfo = new PageInfo<Student>(list);//封装分页数据
model.addAttribute("studentList",list);
//model.addAttribute("pageInfo",pageInfo);
int total = (int) pageInfo.getTotal(); //页码总数
int size = 2;
int step = 9;
String pages = Pager.getPages(total, size, step, page, request, "page");
model.addAttribute("pages",pages);
return "admin/student/index";
}四. 视图页面
<table class="listTable">
<tr>
<td>
<form action="?">
<input type="text" name="names">
<select name="sex">
<option value="男">男</option>
<option value="女">女</option>
</select>
<input type="submit" value="查询">
</form>
</td>
</tr>
</table>
<table class="listTable">
<tr>
<td>选择</td>
<td>ID</td>
<td>姓名</td>
<td>性别</td>
<td>爱好</td>
<td>时间</td>
<td>图片</td>
<td>操作</td>
</tr>
<c:forEach items="${studentList }" var="list">
<tr>
<td>
<input type="checkbox" name="ids" value="${list.id}">
</td>
<td>${list.id}</td>
<td>
${list.names}
</td>
<td>
${list.sex}
</td>
<td>
${list.hobby}
</td>
<td>
<jsp:useBean id="dateValue" class="java.util.Date"/>
<jsp:setProperty name="dateValue" property="time" value="${list.time}"/>
<fmt:formatDate value="${dateValue}" pattern="yyyy-MM-dd HH:mm"/>
</td>
<td>
<c:if test="${list.pic != ''}">
<img src="/up/${list.pic}" style="max-width: 120px;">
</c:if>
</td>
<td>
<a href="">删除</a>
<a href="">修改</a>
</td>
</tr>
</c:forEach>
<tr>
<td colspan="8">
${pages}
</td>
</tr>
</table>效果如下:

Paper.getPasge见http://www.ncyteng.com/news/show/745.html
