在xml中根据条件拼接sql,
<if test="lrfmc != null and lrfmc != ' '"> and lr.mc like concat('%',#{lrfmc},'%') </if>
传入的lrfmc是字符串类型,比如,传入灵洞乡,会提示如下错误:
Error querying database. Cause: java.lang.NumberFormatException: For input string: \\\"灵洞乡\\\"\\r\\n### Cause: java.lang.NumberFormatException: For input string: \\\"灵洞乡\\\"\",\"msg\":\"nested exception is org.apache.ibatis.exceptions.PersistenceException: \\r\\n### Error querying database. }
错误原因:
对于单引号的内容,如<if test=" sfjbnt != 'A' " >,系统会试图把'A'转成数字
解决方式:将if进行改写:
<if test='lrfmc != null and lrfmc != " " '> and lr.mc like concat('%',#{lrfmc},'%') </if>
另外发现当前段传递的lrfmc为" "时,仍然进入了条件内部,最好使用 lrfmc.trim() !=""