mybatis mapper.xml檔案中判斷屬性型別
阿新 • • 發佈:2022-05-12
參考文件: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>