Mybatis中where标签与if标签怎么结合使用

其他教程   发布日期:2023年08月13日   浏览次数:570

这篇文章主要介绍“Mybatis中where标签与if标签怎么结合使用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Mybatis中where标签与if标签怎么结合使用”文章能帮助大家解决问题。

使用<where>标签

select筛选出视图对象的参数,用于给前端返回页面参数使用。

  1. <sql id="selectFileVo">
  2. select file_id,
  3. uuid,
  4. file_name,
  5. file_url,
  6. status,
  7. create_time,
  8. update_time
  9. from file
  10. </sql>

以下代码格式是正确,我们先观察下

  1. and
或者
  1. or
的位置。
  1. <select id="selectFileList" parameterType="File" resultMap="FileResult">
  2. <include refid="selectFileVo"/>
  3. <where>
  4. <if test="fileName != null and fileName != ''">
  5. and file_name like concat('%', #{fileName}, '%')
  6. </if>
  7. <if test="status != null and status != ''">
  8. and status = #{status}
  9. </if>
  10. <if test="params.beginCreateTime != null and params.beginCreateTime != '' and params.endCreateTime != null and params.endCreateTime != ''">
  11. and create_time between #{params.beginCreateTime} and #{params.endCreateTime}
  12. </if>
  13. </where>
  14. </select>

再看一下错误的写法;

  1. <select id="selectFileList" parameterType="File" resultMap="FileResult">
  2. <include refid="selectFileVo"/>
  3. <where>
  4. <if test="fileName != null and fileName != ''">
  5. file_name like concat('%', #{fileName}, '%') and
  6. </if>
  7. <if test="status != null and status != ''">
  8. status = #{status} and
  9. </if>
  10. <if test="params.beginCreateTime != null and params.beginCreateTime != '' and params.endCreateTime != null and params.endCreateTime != ''">
  11. create_time between #{params.beginCreateTime} and #{params.endCreateTime}
  12. </if>
  13. </where>
  14. </select>

这时候运行该代码,当

  1. beginCreateTime
  1. endCreateTime
为空时,我们会发现报错SQL执行异常,原因是where多了一个
  1. and

总结

  1. <if>
标签判断失败后,
  1. <where>
标签关键字可以自动去除掉库表字段赋值前面的
  1. and
,不会去掉语句后面的
  1. and
关键字,即
  1. <where>
标签只会去掉
  1. <if>
标签语句中的最开始的
  1. and
关键字。所以上面的写法(
  1. and
写在后面)是不符合mybatis规范的。

不使用<where>标签

当不使用

  1. <where>
标签时,正确的写法可以参考以下代码:
  1. <select id="selectFileList" parameterType="File" resultMap="FileResult">
  2. <include refid="selectFileVo"/>
  3. where 1=1
  4. <if test="fileName != null and fileName != ''">
  5. and file_name like concat('%', #{fileName}, '%')
  6. </if>
  7. <if test="status != null and status != ''">
  8. and status = #{status}
  9. </if>
  10. <if test="params.beginCreateTime != null and params.beginCreateTime != '' and params.endCreateTime != null and params.endCreateTime != ''">
  11. and create_time between #{params.beginCreateTime} and #{params.endCreateTime}
  12. </if>
  13. </select>

此时我们发现

  1. and
是写在前面的,同时增加了
  1. 1=1
条件。

如果我们去掉

  1. 1=1
条件,同时去掉第一个
  1. <if>
标签的
  1. and
  1. <select id="selectFileList" parameterType="File" resultMap="FileResult">
  2. <include refid="selectFileVo"/>
  3. where
  4. <if test="fileName != null and fileName != ''">
  5. file_name like concat('%', #{fileName}, '%')
  6. </if>
  7. <if test="status != null and status != ''">
  8. and status = #{status}
  9. </if>
  10. <if test="params.beginCreateTime != null and params.beginCreateTime != '' and params.endCreateTime != null and params.endCreateTime != ''">
  11. and create_time between #{params.beginCreateTime} and #{params.endCreateTime}
  12. </if>
  13. </select>

这种情况下,当

  1. fileName
为空时,sql语句中会出现
  1. where and
这种错误的语法,最终导致sql执行异常。所以正确的代码中,使用
  1. 1=1
条件,当
  1. fileName
为空时,sql语句就会变成
  1. where 1=1
,后面接不接
  1. and
都能正确执行。

在不使用

  1. <where>
标签的情况下,
  1. and
写在后面,在
  1. where
条件最后增加
  1. 1=1
判断,原理和上面一样,这里就不再赘述了。

以上就是Mybatis中where标签与if标签怎么结合使用的详细内容,更多关于Mybatis中where标签与if标签怎么结合使用的资料请关注九品源码其它相关文章!