早上单元测试的时候,使用MyBatis的时候加了一段sql发现报错:
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.lang.IllegalArgumentException: invalid comparison: java.util.Arrays$ArrayList and java.lang.String
### Cause: java.lang.IllegalArgumentException: invalid comparison: java.util.Arrays$ArrayList and java.lang.String
新增加的sql代码如下:
<if test="progressTypeList != null and progressTypeList != ''">
AND
<foreach collection="progressTypeList" item="item" open="(" close=")" separator="OR">
<if test="item == 'prep'">
c.START_WORK_PRACTICAL_TIME IS NULL
</if>
<if test="item == 'ing'">
c.START_WORK_PRACTICAL_TIME IS NOT NULL
</if>
</foreach>
</if>
上网查了一下资料发现,原来progressTypeList这个变量是List但是只有一个值的时候,判断if标签哪里写了 progressType != ‘’ Mybatis认为progressTypeList 这个变量是字符串类型。 然后把这个判断 progressType != ‘’ 改成 progressTypeList.size > 0就可以了。
<if test="progressTypeList != null and progressTypeList.size > 0">
AND
<foreach collection="progressTypeList" item="item" open="(" close=")" separator="OR">
<if test="item == 'prep'">
c.START_WORK_PRACTICAL_TIME IS NULL
</if>
<if test="item == 'ing'">
c.START_WORK_PRACTICAL_TIME IS NOT NULL
</if>
</foreach>
</if>