执行查询时 MyBatis NumberFormatException

Mar*_*aka 2 java mybatis

使用 MyBatis,我有一个 SQL 作为参数接收字符串 "fr.id_lotacao in (3007, 3008, 3009, 3010)"

查询语句:

...
<if test="idLotacao != -1">
    and ${idLotacao}
</if>
...
Run Code Online (Sandbox Code Playgroud)

我以这种方式从 Java 调用:

getDB1SqlSessionTemplate().selectList("sql", MyBatisUtil.createMap("idLotacao", getIdsLotacao(lotacao)));
Run Code Online (Sandbox Code Playgroud)

其中getIdsLotacao(lotacao)返回作为参数传递的字符串。

但是,当执行时,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)"
Run Code Online (Sandbox Code Playgroud)

当用 $ 接收参数时,MyBatis 不是应该${idLotacao}用 String替换"fr.id_lotacao in (3007, 3008, 3009, 3010)"吗?

我究竟做错了什么?是什么导致了这个错误?

Ait*_*tor 5

有时,即使您正在比较字符串,并且您的参数也是一个字符串,Mybaits 也会返回一个 NFE。

要解决这个问题,您必须替换以下表达式:

<if test="parameter =='A'"> 
AND YOURCOLUMN IS NOT NULL 
</if>
Run Code Online (Sandbox Code Playgroud)

为了这:

<if test="parameter eq 'A'.toString()"> 
AND YOURCOLUMN IS NOT NULL 
</if>
Run Code Online (Sandbox Code Playgroud)