1. 程式人生 > >mybatis java.lang.NumberFormatException: For input string: "y"

mybatis java.lang.NumberFormatException: For input string: "y"

今天在做mybatis的查詢時,報了一個奇葩錯:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.lang.NumberFormatException: For input string: "y"
### Cause: java.lang.NumberFormatException: For input string: "y"
	at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:79)
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:447)
	at com.sun.proxy.$Proxy219.selectList(Unknown Source)
	at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:231)
	at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:128)

引起異常的程式碼如下:

<if test="like == 'y'">
    AND o.short_code LIKE CONCAT('%', #{shortCode}, '%')
</if>

經過網上查詢,原來’y’被認為是char型別,'yy’或者“y”沒有問題。

如何解決?

  1. 將程式碼改為 test=“like == ‘y’.toString()”
  2. 將程式碼改為 test=“like == “y””
  3. 將程式碼改為 test=‘like == “y”’