[英]MyBatis NumberFormatException while executing query
Using MyBatis, I have a SQL that receives as parameter the String "fr.id_lotacao in (3007, 3008, 3009, 3010)"
使用MyBatis,我有一个SQL,它接收字符串"fr.id_lotacao in (3007, 3008, 3009, 3010)"
作为参数。
SQL: SQL:
...
<if test="idLotacao != -1">
and ${idLotacao}
</if>
...
I call from Java this way: 我以这种方式从Java调用:
getDB1SqlSessionTemplate().selectList("sql", MyBatisUtil.createMap("idLotacao", getIdsLotacao(lotacao)));
Where getIdsLotacao(lotacao)
returns the String passed as parameter. 其中getIdsLotacao(lotacao)
返回作为参数传递的字符串。
But, when executed, MyBatis throws the error: 但是,执行时,MyBatis会引发错误:
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.lang.NumberFormatException: For input string: "fr.id_lotacao in (3007, 3008, 3009, 3010)"
When receiving the parameter with $, isn't MyBatis supposed to replace ${idLotacao}
with the String "fr.id_lotacao in (3007, 3008, 3009, 3010)"
? 用$接收参数时,MyBatis是否不应该用字符串"fr.id_lotacao in (3007, 3008, 3009, 3010)"
替换${idLotacao}
?
What am I doing wrong? 我究竟做错了什么? What's causing this error? 是什么导致此错误?
Sometimes, even if you're comparing strings, and your parameter is also a String, Mybaits return a NFE. 有时,即使您正在比较字符串,并且您的参数也是字符串,Mybaits也会返回NFE。
To solve this, you have to replace the following expression: 为了解决这个问题,您必须替换以下表达式:
<if test="parameter =='A'">
AND YOURCOLUMN IS NOT NULL
</if>
for this: 为了这:
<if test="parameter eq 'A'.toString()">
AND YOURCOLUMN IS NOT NULL
</if>
Actually you are comparing a string with a number ie -1,which is causing number format exception. 实际上,您正在将字符串与数字(即-1)进行比较,这会导致数字格式异常。 Put -1 in quotes like '-1' to make it work 将-1加上“ -1”之类的引号使其生效
I had the same problem described by Aitor and resolved it a different way. 我遇到了Aitor描述的相同问题,并以不同的方式解决了它。 I think it's trying to do a numeric comparison because it's parsing 'A' as a char. 我认为它正在尝试进行数值比较,因为它会将“ A”解析为一个字符。
So I swapped the single quotes and double quotes to ensure it is being parsed as a String. 因此,我交换了单引号和双引号以确保将其解析为字符串。
<if test='parameter =="A"'>
AND YOURCOLUMN IS NOT NULL
</if>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.