java - mybatis参数格式化异常:NumberFormatException: For input string:"xx"
ringa_lee
ringa_lee 2017-04-18 09:48:57
0
2
1239

使用mybatis注解开发的时候,总会遇到很多问题,但是mybatis的注解并不是那么的美好,一些问题或者错误根本不能给出准确的定位,有时候,甚至会给出Unknow Source的错误,获取就是一个关闭标签没有关上。此类问题,在xml配置中,mybatis做的比较好,所以,官方也不是很放心其注解,建议使用xml。

1. 错误日志

[16/08/16 02:15:40:040 CST] http-nio-8080-exec-4 ERROR filter.ExceptionFilter:  [DUBBO] Got unchecked and undeclared exception which called by 127.0.0.1. service: cn.test.web.service.ShareService, method: getShareChannelStInfoList, exception: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.lang.NumberFormatException: For input string: "2016-08-11"
### Cause: java.lang.NumberFormatException: For input string: "2016-08-11", dubbo version: 2.8.4, current host: 127.0.0.1
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.lang.NumberFormatException: For input string: "2016-08-11"
### Cause: java.lang.NumberFormatException: For input string: "2016-08-11"
    at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:76)
    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:399)
    at com.sun.proxy.$Proxy32.selectList(Unknown Source)
    at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:205)
    at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:122)
    at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:64)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:53)
    at com.sun.proxy.$Proxy57.getShareChannelStInfo(Unknown Source)
    at cn.test.web.repository.ShareStInfoRepo.getShareChannelList(ShareStInfoRepo.java:37)
    at cn.test.web.impl.ShareServiceImpl.getShareChannelStInfoList(ShareServiceImpl.java:72)
    at com.alibaba.dubbo.common.bytecode.Wrapper19.invokeMethod(Wrapper19.java)
    at com.alibaba.dubbo.rpc.proxy.javassist.JavassistProxyFactory$1.doInvoke(JavassistProxyFactory.java:46)
    at com.alibaba.dubbo.rpc.proxy.AbstractProxyInvoker.invoke(AbstractProxyInvoker.java:72)
    at com.alibaba.dubbo.rpc.protocol.InvokerWrapper.invoke(InvokerWrapper.java:53)
    at com.alibaba.dubbo.rpc.filter.ExceptionFilter.invoke(ExceptionFilter.java:64)
    at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)
    at com.alibaba.dubbo.monitor.support.MonitorFilter.invoke(MonitorFilter.java:75)
    at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)
    at com.alibaba.dubbo.rpc.filter.TimeoutFilter.invoke(TimeoutFilter.java:42)
    at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)
    at com.alibaba.dubbo.rpc.protocol.dubbo.filter.TraceFilter.invoke(TraceFilter.java:78)
    at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)
    at com.alibaba.dubbo.rpc.filter.ContextFilter.invoke(ContextFilter.java:70)
    at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)
    at com.alibaba.dubbo.rpc.filter.GenericFilter.invoke(GenericFilter.java:132)
    at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)
    at com.alibaba.dubbo.rpc.filter.ClassLoaderFilter.invoke(ClassLoaderFilter.java:38)
    at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)
    at com.alibaba.dubbo.rpc.filter.EchoFilter.invoke(EchoFilter.java:38)
    at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)
    at com.alibaba.dubbo.rpc.proxy.InvokerInvocationHandler.invoke(InvokerInvocationHandler.java:52)
    at com.alibaba.dubbo.common.bytecode.proxy9.getShareChannelStInfoList(proxy9.java)
    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.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:137)
    at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:288)
    at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:242)
    at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:229)
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:356)
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:179)
    at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:220)
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at com.alibaba.dubbo.rpc.protocol.rest.DubboHttpServer$RestHandler.handle(DubboHttpServer.java:86)
    at com.alibaba.dubbo.remoting.http.servlet.DispatcherServlet.service(DispatcherServlet.java:64)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

2.错误代码

这段代码,就是mybatis的动态查询,就是组装sql语句。但是,这里必须注意一个问题,那就是startd和endd,这两个变量是ShareChannelStInfo中的私有变量,是String类型的。
错误的引起,是startd != 0endd != 0引起的,原因给的是字符串,却拿一个数字比较,mybatis会做一些转化,结果在数字格式化时发生异常。

@Select("<script>" +
            " select * from `test`.`share_channel_st_info`" +
            " where 1 = 1" +
            " <choose>" +
            " <when test=\"channel != null\">" +
            " and channel=#{channel}" +
            " </when>" +
            " <otherwise>" +
            " and channel=0" +
            " </otherwise>" +
            " </choose>" +
            " <if test=\"startd != null and startd != 0\">" +
            " and time &gt;=#{startd}</if>" +
            " <if test=\"endd != null and endd != 0\">" +
            " and time &lt;=#{endd}</if>" +
            " <choose>" +
            " <when test=\"orderby != null and orderby != ''\">" +
            " order by ${orderby}" +
            " </when>" +
            " <otherwise>" +
            " order by time desc" +
            " </otherwise>" +
            " </choose>" +
            " limit #{offset}, #{limit}" +
            "</script>")
    List<ShareChannelStInfo> getShareChannelStInfo(ShareChannelStInfo info);

3. 解决错误

加单引号是无法解决的,比如:startd != '0',此时,mybatis认为拿一个字符串(String)和一个字符(char)比较,也会报错。
因此,有几种方法可以解决此问题:

一是:转成字符串:'0'.toString()

@Select("<script>" +
        " select * from `test `.`share_channel_st_info`" +
        " where 1 = 1" +
        " <choose>" +
        " <when test=\"channel != null\">" +
        " and channel=#{channel}" +
        " </when>" +
        " <otherwise>" +
        " and channel=0" +
        " </otherwise>" +
        " </choose>" +
        " <if test=\"startd != null and startd != '0'.toString()\">" +
        " and time &gt;=#{startd}</if>" +
        " <if test=\"endd != null and endd != '0'.toString()\">" +
        " and time &lt;=#{endd}</if>" +
        " <choose>" +
        " <when test=\"orderby != null and orderby != ''\">" +
        " order by ${orderby}" +
        " </when>" +
        " <otherwise>" +
        " order by time desc" +
        " </otherwise>" +
        " </choose>" +
        " limit #{offset}, #{limit}" +
        "</script>")
List<ShareChannelStInfo> getShareChannelStInfo(ShareChannelStInfo info);   

二是:加转义实体字符串引号&quot;

 <if test=\"startd != null and startd != &quot;0&quot;\">

三是:外部单双引号交换:

<if test='startd != null and startd != \"0\"'>

注意:在xml配置中,使用第三种方式,不需要添加反斜杠("\"),<if test='startd != null and startd != "0"'>

ringa_lee
ringa_lee

ringa_lee

reply all(2)
小葫芦

java.lang.NumberFormatException: For input string: "2016-08-11"

The poster has already made this problem very obvious.
ShareChannelStInfo Take a look at how many int attributes there are in this entity class. Then put SQL into the database and query it. ShareChannelStInfo 你去看看这个实体类里面有几个 int 属性。然后把 SQL 放到数据库里面查询一下。
某行某列有一个值是 2016-08-11 的。但是你的实体却声明是 intThere is a value in a certain row and column which is 2016-08-11. But your entity is declared to be of type int (the error message does not indicate a specific column, so it cannot be determined here).
This is either a data error or a problem with your data type declaration. Please read the error message carefully and troubleshoot the problem based on the information.

迷茫

Sorry, I originally wrote notes but chose the wrong question instead.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!