分页问题
来源:3-8 _广告模块开发_分页标签封装
田心枫
2017-06-01
HTTP Status 500 - Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: type Exception report message Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: description The server encountered an internal error that prevented it from fulfilling this request. exception org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: java.lang.NullPointerException ### The error may exist in file [/usr/local/Cellar/tomcat/8.5.4/libexec/webapps/ROOT/WEB-INF/classes/mapper/AdDao.xml] ### The error may involve defaultParameterMap ### The error occurred while setting parameters ### Cause: java.lang.NullPointerException org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) javax.servlet.http.HttpServlet.service(HttpServlet.java:622) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121) org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) root cause org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: java.lang.NullPointerException ### The error may exist in file [/usr/local/Cellar/tomcat/8.5.4/libexec/webapps/ROOT/WEB-INF/classes/mapper/AdDao.xml] ### The error may involve defaultParameterMap ### The error occurred while setting parameters ### Cause: java.lang.NullPointerException org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:77) org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446) com.sun.proxy.$Proxy17.selectList(Unknown Source) org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:230) org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:122) org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:64) org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:53) com.sun.proxy.$Proxy18.selectByPage(Unknown Source) org.imooc.service.impl.AdServiceImpl.searchByPage(AdServiceImpl.java:63) org.imooc.controller.content.AdController.initList(AdController.java:27) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:498) org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:222) org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137) org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:814) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:737) org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) javax.servlet.http.HttpServlet.service(HttpServlet.java:622) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121) org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) root cause org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: java.lang.NullPointerException ### The error may exist in file [/usr/local/Cellar/tomcat/8.5.4/libexec/webapps/ROOT/WEB-INF/classes/mapper/AdDao.xml] ### The error may involve defaultParameterMap ### The error occurred while setting parameters ### Cause: java.lang.NullPointerException org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30) org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:122) org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:113) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:498) org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433) com.sun.proxy.$Proxy17.selectList(Unknown Source) org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:230) org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:122) org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:64) org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:53) com.sun.proxy.$Proxy18.selectByPage(Unknown Source) org.imooc.service.impl.AdServiceImpl.searchByPage(AdServiceImpl.java:63) org.imooc.controller.content.AdController.initList(AdController.java:27) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:498) org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:222) org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137) org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:814) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:737) org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) javax.servlet.http.HttpServlet.service(HttpServlet.java:622) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121) org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) root cause java.lang.NullPointerException org.imooc.dao.interceptor.PageInterceptor.intercept(PageInterceptor.java:39) org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:61) com.sun.proxy.$Proxy29.prepare(Unknown Source) org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:76) org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:61) org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:303) org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:154) org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:102) org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:82) org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:120) org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:113) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:498) org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433) com.sun.proxy.$Proxy17.selectList(Unknown Source) org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:230) org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:122) org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:64) org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:53) com.sun.proxy.$Proxy18.selectByPage(Unknown Source) org.imooc.service.impl.AdServiceImpl.searchByPage(AdServiceImpl.java:63) org.imooc.controller.content.AdController.initList(AdController.java:27) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:498) org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:222) org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137) org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:814) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:737) org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) javax.servlet.http.HttpServlet.service(HttpServlet.java:622) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121) org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) note The full stack trace of the root cause is available in the Apache Tomcat/8.5.4 logs. Apache Tomcat/8.5.4
做分页的时候遇到这个报错,研究两三天了,解决不了。老师给看看怎么回事
AdDao.xml这样写的:
<select id="selectByPage" resultType="Ad"> select id,title,img_file_name,link,weight from ad <where> <if test="title != null and title != ''"> title like '%' #{title} '%' </if> </where> order by weight desc,id </select>
11回答
-
单纯的模仿会有问题的,要想一想,难道非要在
AdDto adDto = new AdDto();
的时候也同时new一个Page对象才可以吗?我的代码没报错,而你的报错,就是因为模仿时,会有疏漏,要有自己的想法才行,不过你已经有收获了,因为你现在这种写法是个办法,不一定非要和我的代码一模一样,但要知道【为什么】,这个非常非常重要!想想,【为什么】
022017-11-06 -
源生活
2017-06-01
嘿嘿,你的没错,我的也没错!
想想,如何让我的代码不报错?
022017-06-01 -
田心枫
提问者
2017-06-01
老师
这个地方初始化广告列表页面的时候AdDto adDto = new AdDto()后,要new 一个Page吧?然后adDto.setPage(page);不然直接用adDto里面的Page对象是null吧。
00 -
田心枫
提问者
2017-06-01
@RequestMapping public String init(Model model) { AdDto adDto = new AdDto(); Page page = new Page(); adDto.setPage(page); model.addAttribute("list", adService.searchByPage(adDto)); model.addAttribute("searchParam", adDto); return "/content/adList"; }
@RequestMapping public String init(Model model) { AdDto adDto = new AdDto(); model.addAttribute("list", adService.searchByPage(adDto)); model.addAttribute("searchParam", adDto); return "/content/adList"; }
下面这种方法不对 照着老师视频上的错了
00 -
新手上路wn
2017-06-01
<select id="selectByPage" resultType="Ad">
select id,title,img_file_name,link,weight from ad
<where>
<if test="title != null and title != ''">
title like '%' #{title} '%'
</if>
</where>
order by weight desc,id
</select>
like '%'#{title}'%' 不能这么写
1、可以把%在传入的时候添加到title字段
2、也可以使用CONCAT('%,', #{id}, ',%'),这个函数是mysql的,其他sql有其他写法022017-06-01 -
源生活
2017-06-01
分页拦截器只是共通,它并不知道怎么个分法:【每页显示多少条】、【当前需要的是第几页】,这些都要有人告诉它。
谁告诉它的?以【ByPage】结尾的Dao层方法,Dao层方法里参数又是谁给的?Service层,Service层的参数又是谁给的?
Controller,视频里,到底最终是谁决定了:【每页显示多少条】、【当前需要的是第几页】?
直接告诉你,没有任何意义。我相信这样说你再跟着视频看,如果看懂了,一定能解决问题,祝你成功!
022017-06-01 -
源生活
2017-06-01
问题不在分页拦截器本身了,分页拦截器最多加个判断可以防止【空指针异常】的发生,但结果肯定是错的,仔细看视频,想想,是哪出问题了。
012017-06-01 -
田心枫
提问者
2017-06-01
是的.
032017-06-01 -
田心枫
提问者
2017-06-01
package org.imooc.dao.interceptor; import org.apache.ibatis.executor.parameter.ParameterHandler; import org.apache.ibatis.executor.statement.StatementHandler; import org.apache.ibatis.mapping.BoundSql; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.plugin.Intercepts; import org.apache.ibatis.plugin.Invocation; import org.apache.ibatis.plugin.Plugin; import org.apache.ibatis.plugin.Signature; import org.apache.ibatis.reflection.DefaultReflectorFactory; import org.apache.ibatis.reflection.MetaObject; import org.apache.ibatis.reflection.SystemMetaObject; import org.imooc.bean.BaseBean; import org.imooc.bean.Page; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.Properties; @Intercepts({@Signature(type=StatementHandler.class,method="prepare",args={Connection.class})}) public class PageInterceptor implements Interceptor{ @Override public Object intercept(Invocation arg0) throws Throwable { StatementHandler statementHandler = (StatementHandler)arg0.getTarget(); MetaObject metaObject = MetaObject.forObject(statementHandler, SystemMetaObject.DEFAULT_OBJECT_FACTORY, SystemMetaObject.DEFAULT_OBJECT_WRAPPER_FACTORY,new DefaultReflectorFactory()); MappedStatement mappedStatement = (MappedStatement)metaObject.getValue("delegate.mappedStatement"); String id = mappedStatement.getId(); if(id.endsWith("ByPage")) { BoundSql boundSql = statementHandler.getBoundSql(); String sql = boundSql.getSql(); String countSql = "select count(*) from(" + sql + ")t"; Connection conn = (Connection)arg0.getArgs()[0]; PreparedStatement statement = conn.prepareStatement(countSql); ParameterHandler parameterHandler = (ParameterHandler)metaObject.getValue("delegate.parameterHandler"); parameterHandler.setParameters(statement); ResultSet rs = statement.executeQuery(); BaseBean bean = (BaseBean)boundSql.getParameterObject(); Page page = bean.getPage(); if(rs.next()) { page.setTotalNumber(rs.getInt(1)); } String pageSql = sql + " limit " + (page.getCurrentPage() - 1) * page.getPageNumber() + "," + page.getPageNumber(); metaObject.setValue("delegate.boundSql.sql", pageSql); } return arg0.proceed(); } @Override public Object plugin(Object arg0) { return Plugin.wrap(arg0, this); } @Override public void setProperties(Properties arg0) { } }
那个我已经看过了。PageInterceptor是这样的。
00 -
源生活
2017-06-01
### The error may involve defaultParameterMap
### The error occurred while setting parameters根据这两行提示,非常有可能是分页拦截器里:
PreparedStatement statement = conn.prepareStatement(countSql);
ParameterHandler parameterHandler = (ParameterHandler)metaObject.getValue("delegate.parameterHandler");
parameterHandler.setParameters(statement);这几行代码,有写错的,导致PreparedStatement在执行SQL时,参数为空。
建议:
把《通过自动回复机器人学Mybatis---加强版》中分页拦截器的讲解认真看完再接着看这边的。
本门课提到的基础课程都是有原因,如果不熟悉是必须要看懂的,不是可看可不看的。
00 -
源生活
2017-06-01
org.imooc.dao.interceptor.PageInterceptor.intercept(PageInterceptor.java:39)
分页拦截器PageInterceptor第39行空指针,你先自己判断一下,这一行是什么导致的空指针,然后不行把分页拦截器代码贴出来看下。
052017-06-01
IT段子手详解MyBatis遇到Spring 秒学Java SSM开发大众点评
1001 学习 · 485 问题
相似问题