版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a913858/article/details/82761098
测试别人的报表,前台报错提示查询失败。马上看后台报下面这个错误:
### Cause: java.lang.NumberFormatException: For input string: "y"
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:75)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:371)
at com.sun.proxy.$Proxy22.selectList(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:205)
at com.bonatone.eweb.service.impl.DefaultService.selectList(DefaultService.java:166)
... 79 common frames omitted
数字转换异常的错误,然后看sql没发现有转换的错误啊,就看到一处判断有个y。
<if test="flag == 'n' ">
AND h.START_TIME is null
</if>
<if test="flag == 'y'">
AND h.START_TIME is not null
</if>
心想肯定这块的问题,我就先把这块注释掉。程序再次查询没有问题了。定位到这里也就是判断出的问题,可是为啥会报这个错误呢。原来是 Mybatis会将 “y” 解析为字符( 后台估计会变成uncode码吧),而非字符串,不能做到判断的效果。
那么问题来了怎么才能判断呢?
<if test="flag == 'n'.toString()">
AND h.START_TIME is null
</if>
<if test="flag == 'y'.toString()">
AND h.START_TIME is not null
</if>
或者
<if test='flag == "n" '>
AND h.START_TIME is null
</if>
<if test='flag == "y" '>
AND h.START_TIME is not null
</if>
这两种方式给大家参考。