概述
今天在写查询的时候突然遇到了一个问题,虽然这个问题以前也是遇到过但是解决方法忘记了,经过查询终于记起来解决方法,在此记录一下:
报错信息如下(简单复制了一点):
log4j:WARN No appenders could be found for logger (com.zit.epms.controller.CheckTemporaryTrafficPermitInfoController).
log4j:WARN Please initialize the log4j system properly.
java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.github.pagehelper.Page
at com.zit.epms.controller.CheckTemporaryTrafficPermitInfoController.temporaryTrafficPermitInfoCheckSearch(CheckTemporaryTrafficPermitInfoController.java:137)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
代码如下:
@RequestMapping("/temporaryTrafficPermitInfoCheckSearch")
public ModelAndView temporaryTrafficPermitInfoCheckSearch(HttpServletRequest request,Integer curr,Integer limit,
TemporaryTrafficPermitInfo temporaryTrafficPermitInfo) {
ModelAndView mView = new ModelAndView();
mView.setView(Jackson2Util.jsonView());
PageHelper.startPage(curr, limit);
long total = 0;
currentUser = (User) request.getSession().getAttribute("currentUser");
try {
if(null!=currentUser) {
BrigadeInfo brigadeInfo = brigadeInfoService.searchByBrigadeName(currentUser.getName());
if(StringUtil.isNotEmpty(String.valueOf(brigadeInfo.getId()))) {
List<EnranceInfo> enranceInfos = enranceInfoService.selectEntrance(brigadeInfo.getId());
List<String> sunList = new ArrayList<>();
enranceInfos.forEach((a) ->sunList.add(String.valueOf(a.getId())));
Object[] strArray = sunList.toArray();
Long [] idsResult = new Long[sunList.size()];
for (int i=0;i<strArray.length;i++) {
idsResult[i] = Long.valueOf((String) strArray[i]);
}
temporaryTrafficPermitInfo.setEntranceList(idsResult);
}
}
List<TemporaryTrafficPermitInfo> temporaryTrafficPermitInfos = temporaryTrafficPermitInfoService.selectTemporaryTrafficPermitInfo(temporaryTrafficPermitInfo);
for (TemporaryTrafficPermitInfo tmp : temporaryTrafficPermitInfos) {
if(new Date().getTime()>tmp.getYxjs().getTime()) {
tmp.setDqztString(DqztEnum.OUT_DATE.getValue());
}else {
tmp.setDqztString(DqztEnum.NOT_OUT_DATE.getValue());
}
}
Page<TemporaryTrafficPermitInfo> page = (Page<TemporaryTrafficPermitInfo>)temporaryTrafficPermitInfos;
total = page.getTotal();
mView.addObject("count", total);
mView.addObject("code", 0);
mView.addObject("data", temporaryTrafficPermitInfos);
} catch (Exception e) {
logger.error("通行证信息查询失败", e);
e.printStackTrace();
}
return mView;
}
原因是代码中的pageHelper只会对它下一条查询语句分页生效(看代码标红位置),所以在对这个查询分页的时候会不能分页。
解决方法是将 PageHelper.startPage(curr, limit);与查询的语句放在一起。这样pagehelper会对这个语句进行分页
如下:
PageHelper.startPage(curr, limit);
List<TemporaryTrafficPermitInfo> temporaryTrafficPermitInfos = temporaryTrafficPermitInfoService.selectTemporaryTrafficPermitInfo(temporaryTrafficPermitInfo);
for (TemporaryTrafficPermitInfo tmp : temporaryTrafficPermitInfos) {
if(new Date().getTime()>tmp.getYxjs().getTime()) {
tmp.setDqztString(DqztEnum.OUT_DATE.getValue());
}else {
tmp.setDqztString(DqztEnum.NOT_OUT_DATE.getValue());
}
}
Page<TemporaryTrafficPermitInfo> page = (Page<TemporaryTrafficPermitInfo>)temporaryTrafficPermitInfos;
total = page.getTotal();
最后
以上就是野性星月为你收集整理的pagehelper.Page分页查询报错的全部内容,希望文章能够帮你解决pagehelper.Page分页查询报错所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复