您好,登录后才能下订单哦!
这篇文章主要介绍“Mybatis中where标签与if标签怎么结合使用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Mybatis中where标签与if标签怎么结合使用”文章能帮助大家解决问题。
select筛选出视图对象的参数,用于给前端返回页面参数使用。
<sql id="selectFileVo"> select file_id, uuid, file_name, file_url, status, create_time, update_time from file </sql>
以下代码格式是正确,我们先观察下and或者or的位置。
<select id="selectFileList" parameterType="File" resultMap="FileResult">
<include refid="selectFileVo"/>
<where>
<if test="fileName != null and fileName != ''">
and file_name like concat('%', #{fileName}, '%')
</if>
<if test="status != null and status != ''">
and status = #{status}
</if>
<if test="params.beginCreateTime != null and params.beginCreateTime != '' and params.endCreateTime != null and params.endCreateTime != ''">
and create_time between #{params.beginCreateTime} and #{params.endCreateTime}
</if>
</where>
</select>再看一下错误的写法;
<select id="selectFileList" parameterType="File" resultMap="FileResult">
<include refid="selectFileVo"/>
<where>
<if test="fileName != null and fileName != ''">
file_name like concat('%', #{fileName}, '%') and
</if>
<if test="status != null and status != ''">
status = #{status} and
</if>
<if test="params.beginCreateTime != null and params.beginCreateTime != '' and params.endCreateTime != null and params.endCreateTime != ''">
create_time between #{params.beginCreateTime} and #{params.endCreateTime}
</if>
</where>
</select>这时候运行该代码,当beginCreateTime或endCreateTime为空时,我们会发现报错SQL执行异常,原因是where多了一个and。
当<if>标签判断失败后, <where> 标签关键字可以自动去除掉库表字段赋值前面的and,不会去掉语句后面的and关键字,即<where> 标签只会去掉<if> 标签语句中的最开始的and关键字。所以上面的写法(and写在后面)是不符合mybatis规范的。
当不使用<where>标签时,正确的写法可以参考以下代码:
<select id="selectFileList" parameterType="File" resultMap="FileResult">
<include refid="selectFileVo"/>
where 1=1
<if test="fileName != null and fileName != ''">
and file_name like concat('%', #{fileName}, '%')
</if>
<if test="status != null and status != ''">
and status = #{status}
</if>
<if test="params.beginCreateTime != null and params.beginCreateTime != '' and params.endCreateTime != null and params.endCreateTime != ''">
and create_time between #{params.beginCreateTime} and #{params.endCreateTime}
</if>
</select>此时我们发现and是写在前面的,同时增加了1=1条件。
如果我们去掉1=1条件,同时去掉第一个<if>标签的and。
<select id="selectFileList" parameterType="File" resultMap="FileResult">
<include refid="selectFileVo"/>
where
<if test="fileName != null and fileName != ''">
file_name like concat('%', #{fileName}, '%')
</if>
<if test="status != null and status != ''">
and status = #{status}
</if>
<if test="params.beginCreateTime != null and params.beginCreateTime != '' and params.endCreateTime != null and params.endCreateTime != ''">
and create_time between #{params.beginCreateTime} and #{params.endCreateTime}
</if>
</select>这种情况下,当fileName为空时,sql语句中会出现where and这种错误的语法,最终导致sql执行异常。所以正确的代码中,使用1=1条件,当fileName为空时,sql语句就会变成where 1=1 ,后面接不接and都能正确执行。
在不使用<where>标签的情况下,and写在后面,在where条件最后增加1=1判断,原理和上面一样,这里就不再赘述了。
关于“Mybatis中where标签与if标签怎么结合使用”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。