昨天运行程序,程序报了mybatis参数格式化异常:NumberFormatException: For input string:"xx",错误日志如下:
错误日记:
```[16/08/1602:15:40:040CST]http-nio-8080-exec-4ERRORfilter.ExceptionFilter: [DUBBO]Gotuncheckedandundeclaredexceptionwhichcalledby127.0.0.1.service:cn.test.web.service.ShareService,method:getShareChannelStInfoList,exception:org.mybatis.spring.MyBatisSystemException:nestedexceptionisorg.apache.ibatis.exceptions.PersistenceException:
###Errorqueryingdatabase.Cause:java.lang.NumberFormatException:Forinputstring:"2016-08-11"
###Cause:java.lang.NumberFormatException:Forinputstring:"2016-08-11",dubboversion:2.8.4,currenthost:127.0.0.1
org.mybatis.spring.MyBatisSystemException:nestedexceptionisorg.apache.ibatis.exceptions.PersistenceException:
###Errorqueryingdatabase.Cause:java.lang.NumberFormatException:Forinputstring:"2016-08-11"
###Cause:java.lang.NumberFormatException:Forinputstring:"2016-08-11"
atorg.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:76)
atorg.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:399)
atcom.sun.proxy.$Proxy32.selectList(UnknownSource)
atorg.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:205)
atorg.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:122)
atorg.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:64)
atorg.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:53)
atcom.sun.proxy.$Proxy57.getShareChannelStInfo(UnknownSource)
atcn.test.web.repository.ShareStInfoRepo.getShareChannelList(ShareStInfoRepo.java:37)
atcn.test.web.impl.ShareServiceImpl.getShareChannelStInfoList(ShareServiceImpl.java:72)
atcom.alibaba.dubbo.common.bytecode.Wrapper19.invokeMethod(Wrapper19.java)
atcom.alibaba.dubbo.rpc.proxy.javassist.JavassistProxyFactory$1.doInvoke(JavassistProxyFactory.java:46)
atcom.alibaba.dubbo.rpc.proxy.AbstractProxyInvoker.invoke(AbstractProxyInvoker.java:72)
atcom.alibaba.dubbo.rpc.protocol.InvokerWrapper.invoke(InvokerWrapper.java:53)
atcom.alibaba.dubbo.rpc.filter.ExceptionFilter.invoke(ExceptionFilter.java:64)
atcom.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)
atcom.alibaba.dubbo.monitor.support.MonitorFilter.invoke(MonitorFilter.java:75)
atcom.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)
atcom.alibaba.dubbo.rpc.filter.TimeoutFilter.invoke(TimeoutFilter.java:42)
atcom.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)
atcom.alibaba.dubbo.rpc.protocol.dubbo.filter.TraceFilter.invoke(TraceFilter.java:78)
atcom.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)
atcom.alibaba.dubbo.rpc.filter.ContextFilter.invoke(ContextFilter.java:70)
atcom.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)
atcom.alibaba.dubbo.rpc.filter.GenericFilter.invoke(GenericFilter.java:132)
atcom.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)
atcom.alibaba.dubbo.rpc.filter.ClassLoaderFilter.invoke(ClassLoaderFilter.java:38)
atcom.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)
atcom.alibaba.dubbo.rpc.filter.EchoFilter.invoke(EchoFilter.java:38)
atcom.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)
atcom.alibaba.dubbo.rpc.proxy.InvokerInvocationHandler.invoke(InvokerInvocationHandler.java:52)
atcom.alibaba.dubbo.common.bytecode.proxy9.getShareChannelStInfoList(proxy9.java)
atsun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)
atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
atjava.lang.reflect.Method.invoke(Method.java:498)
atorg.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:137)
atorg.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:288)
atorg.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:242)
atorg.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:229)
atorg.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:356)
atorg.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:179)
atorg.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:220)
atorg.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
atorg.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)
atjavax.servlet.http.HttpServlet.service(HttpServlet.java:729)
atcom.alibaba.dubbo.rpc.protocol.rest.DubboHttpServer$RestHandler.handle(DubboHttpServer.java:86)
atcom.alibaba.dubbo.remoting.http.servlet.DispatcherServlet.service(DispatcherServlet.java:64)
atjavax.servlet.http.HttpServlet.service(HttpServlet.java:729)
atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
atorg.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
atcom.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123)
atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
atorg.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
atorg.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
atorg.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
atorg.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
atorg.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
atorg.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
atorg.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
atorg.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
atorg.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099)
atorg.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
atorg.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
atorg.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
atjava.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
atjava.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
atorg.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
atjava.lang.Thread.run(Thread.java:745)
```
错误代码
这段代码,就是mybatis的动态查询,就是组装sql语句。但是,这里必须注意一个问题,那就是departmentName 和 ordertime,这两个变量是EarlyOrder中的私有变量,是String类型的。 错误的引起,是 departmentName != 0 引起的,原因给的是字符串,却拿一个数字比较,mybatis会做一些转化,结果在数字格式化时发生异常。
<selectid="selectEarlyOrder"resultType="com.example.demo.model.EarlyOrder">
selectusername,ordertime,overworktime,status,departmentname
fromtabordermeal
<where>
1=1
<iftest="departmentName != 0 and departmentName != '' and departmentName !=null and departmentName != 'null'">
anddepartmentName=#{departmentName}
</if>
<iftest="ordertime != '' and ordertime !=null and ordertime != 'null'">
andordertimeLIKE'${ordertime}%'
</if>
</where>
limit#{pageindex},#{pagesize}
</select>
解决错误
加单引号是无法解决的,比如:departmentName != '0',此时,mybatis认为拿一个字符串(String)和一个字符(char)比较,也会报错。 因此,有几种方法可以解决此问题:
**转成字符串:'0'.toString()**
<selectid="selectEarlyOrder"resultType="com.example.demo.model.EarlyOrder">
selectusername,ordertime,overworktime,status,departmentname
fromtabordermeal
<where>
1=1
<iftest="departmentName != '0'.toString() and departmentName != '' and departmentName !=null and departmentName != 'null'">
anddepartmentName=#{departmentName}
</if>
<iftest="ordertime != '' and ordertime !=null and ordertime != 'null'">
andordertimeLIKE'${ordertime}%'
</if>
</where>
limit#{pageindex},#{pagesize}
</select>
加转义实体字符串引号"
<iftest=\"departmentName != null and departmentName != "0"\">
外部单双引号交换:
<iftest='startd != null and startd != \"0\"'>
注意:在xml配置中,使用第三种方式,不需要添加反斜杠("\"),<if test='startd != null and startd != "0"'>