`
中华好儿孙
  • 浏览: 66609 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

mybatis里if标签判断字符串相等不相等的问题

阅读更多
<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方法的调用
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics