mybatis mapper.xml文件中判断属性类型
参考文档:apache ognl 参考文档
假设map.id有两种类型,String或Array(别抬杠,我就想这么处理)
之前的写法:
<where>
<if test="map.id !=null and map.id != ''">
AND asset.id in
<foreach collection="map.id" item="item" open="(" close=")" separator=",">#{item}</foreach>
</if>
</where>
这样如果传进来的id是String型,将会报错:
Error evaluating expression ‘xxx’. Return value (xxx) was not iterable.
那么如何在mapper.xml里面判断两种属性呢
<where>
<choose>
<when test="map.id instanceof String">
AND asset.id = #{map.id}
</when>
<otherwise>
<if test="map.id !=null and map.id != ''">
AND asset.id in
<foreach collection="map.id" item="item" open="(" close=")" separator=",">#{item}</foreach>
</if>
</otherwise>
</choose>
</where>