<foreach collection="accurateSearchMap" index="key" item="item"> <if test="key == 'gcc.sex' and item != '3' and item!='' and item!=null"> and ${key} = #{item} </if> <if test="key == 'gcc.classes' and item != '3' and item!='' and item!=null"> and ${key} = #{item} </if> <if test="key == 'gcc.add_target' and item != '2' and item!='' and item!=null"> and ${key} = #{item} </if> <if test="key == 'gcc.custype' and item != '3' and item!='' and item!=null"> and ${key} = #{item} </if> <if test="item!='' and item!=null and key != 'gcc.sex' and key != 'gcc.classes' and key != 'gcc.add_target' and key != 'gcc.custype'"> and ${key} = #{item} </if> </foreach>
因为accurateSearchMap是Map<String, Object>类型的,所以键对应的值是Object类型的,而对象间用==或!=比较的是地址引用,所以在值为Object类型的情况下以上代码是有问题的。
解决办法是:在值item后加上toString方法的调用,'1'后面也要加toString方法的调用