您的位置:首页 > 其它

使用mybatis遇到的关于条件查询">"以及if test传参的使用问题

2015-07-21 22:33 597 查看
第一个:传参后判断的问题

Map map = new HashMap();
map.put("str","1,2");
//将map当做参数传进去
public list dotest(Map map);


//后台的xml中执行的sql
<select  result="map"  resultType="map">
select * from t_table
where 1=1
<!--这种写法是没有问题的,不等于可以写-->
<if test="str!=null and str!='' and str !='1,2'">
name = #{str}
</if>
<!--如果这样写就会报错 str不能让它与字符串比较-->
<if test="str!=null and str =='1,2'">
name = #{str}
</if>
</select>


第二个:sql中不能直接使用 “<” 或者 “>”

还是用上面代码说明下

<select  result="map"  resultType="map">
select * from t_table
where 1=1
<!--"<="一起写是可以的-->
<if test="str!=null" >
id <= '3'
</if>
<!--如果这样写就会报错,">"这个符号会被默认成结束字符而不是大于号-->
<if test="str==null">
id > '5'
</if>
<!--因此你可以改成这样,使用xml过滤成普通文本-->
<if test="str==null">
<![data[ id > '5']]>
</if>
</select>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  参数 mybatis