方便你们解决错误,我把解决方法放在前面
首先你要检查你的PageHelper是什么版本的,如果是5.xx那么你的配置文件如下
<!-- PageHelper配置-->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<props>
<!-- 配置页码修正 -->
<prop key="reasonable">true</prop>
</props>
</property>
</bean>
</array>
</property>
如果是较低一点的版本,那么你需要将上述com.github.pagehelper.PageInterceptor
更换为com.github.pagehelper.PageHelper
。
无论你选择在mybatis-config.xml文件中配配置还是在spring的sqlSessionFactoryBean中去配置,这不影响你的正常使用。但是如果两个都配置的话就可能会由冲突!
最后如果还是报错500,那么很好。这说明你的pageHelper和jsqlparser不配套,我昨天就是这样,然后一直以为是编码的问题,最后来回梳理很多遍确定没问题。就一直更换版本,最终得出下面这俩,有需要的可以放心使用
<!-- 分页插件PageHelper -->
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.jsqlparser/jsqlparser -->
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>2.0</version>
</dependency>
昨天使用SSM结合PageHelper做分页查询,需要导入两个jar包。本人用maven进行资源注入。第一次用pageHelper这个分页插件,觉得挺简单的。tomcat跑起来之后完犊子了。首先你的tomCat运行日志会报错,大概率是空指针异常。
紧接着去翻看异常信息,发现之前在spring容器中注册好的bean也出了问题???我真是一阵一阵无语,怎么代码放时间长了还能坏,,然后就开始各种整顿。
总而言之言而总之,很多时候程序报出的错误可能并不是你编码有问题,但是如果你对于自己所使用的技术栈或者框架不够熟悉的话那么一切错误你都会觉得毫无规律可循。
PageHelper用法
导入上述资源后,在spring容器中将PageHelper配置在SqlSessionFactoryBean中。
测试controller
public String getBookList(@RequestParam(value = "start",defaultValue = "1") int start,Model model){
System.out.println("接收到start===="+start);
// 调用业务层查询方法
// start当前页码
PageHelper.startPage(start,3);
List<Book> list = bookService.getBookList();
// 将数据丢给插件
PageInfo<Book> info = new PageInfo<Book>(list);
/*
public PageInfo(List<T> list) {
this(list, 8);}
*/
// 添加数据到视图
model.addAttribute("alist",info);
return "allBook";
}
jsp界面获取:
<tr>
<a href="${pageContext.request.contextPath}/book/allBook1">首页</a>
<a href="${pageContext.request.contextPath}/book/allBook1?start=${alist.prePage}">上一页</a>
<c:forEach items="${alist.navigatepageNums}" var="num">
<c:if test="${num == alist.pageNum}">
[${alist.pageNum}]
</c:if>
<c:if test="${num != alist.pageNum}">
<a href="${pageContext.request.contextPath}/book/allBook1?start=${num}"> ${num} </a>
</c:if>
</c:forEach>
<a href="${pageContext.request.contextPath}/book/allBook1?start=${alist.nextPage}">下一页</a>
<a href="${pageContext.request.contextPath}/book/allBook1?start=${alist.pages}">尾页</a>
</tr>