mybatis动态sql将字符串转换成数字类型报错

报错信息

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 
	### Error querying database.  Cause: java.lang.NumberFormatException: For input string: "xxx"
	### Cause: java.lang.NumberFormatException: For input string: "xxx"
	java.util.concurrent.CompletionException: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 

解决办法

  • 1、外面使用单引号,参数使用双引号
<if test='test == "xxx" '>

</if>

注意!!!:当有多个判断时,需要全部使用单引号,如:

<choose>
	<when test='status == "jj"'>
      and STATUS = 'jj'
	</when>
	<when test='status == "xx"'>
      and STATUS = 'xx'
	</when>
	<otherwise>
		and STATUS != 'jj' and STATUS != 'xx'
	</otherwise>
</choose>

  • 2、加toString()方法
<if test="test == 'xxx'.toString()">

</if>
posted @ 2023-12-08 15:47  fchhk  阅读(235)  评论(0编辑  收藏  举报