Mybatis中 <where> </where> 标签

本文深入探讨MyBatis框架中动态SQL的使用技巧,特别是&lt;where&gt;标签的智能处理方式。当where子句后的筛选条件为空时,&lt;where&gt;标签会自动移除整个where语句,避免了不必要的and关键字,确保生成的SQL语句正确无误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

mybatis的动态sql非常强大。

其中  <where> </where> 标签 在不满足 where子句后面的筛选条件时,会去掉 where 子句。

例: 

<select id="findList" resultMap="userMap" parameterType="UserDto">
       select
          *
        FROM
          users
    <where>
         <if test ="status != null">
          status = #{status}
        </if>
        <if test ="minPrice != null">
            and now_price &gt;= #{minPrice}
        </if>
        <if test="maxPrice != null">
          and now_price &lt;= #{maxPrice}
        </if>
        <if test="minTime != null">
          and create_time &lt;= #{minTime}
        </if>
        <if test="maxTime != null">
            and create_time &gt;= #{maxTime}
        </if>
        <if test="itemName != null and itemName != ''">
            AND item_name LIKE concat('%',#{itemName},'%')
        </if>
    </where>
</select>

以上例子中,如果where 子句中的筛选条件为空的话,将会变成下面的sql

select * from users;    就会查询所有数据。

### 3.1 `<where>` 标签的基本作用 在 MyBatis 中,`<where>` 标签用于动态生成 SQL 查询语句中的 `WHERE` 条件部分。其主要作用是自动添加 `WHERE` 关键字,并且会智能地去除条件中第一个子句前多余的 `AND` 或 `OR`,从而避免语法错误。 例如: ```xml <select id="getUserByNameAndSex" parameterType="map" resultType="user"> select * from user <where> and uname like '%${username}%' and sex=#{usex} </where> </select> ``` 即使第一个条件前有 `and`,MyBatis 也会自动将其去除,最终生成的 SQL 为: ```sql SELECT * FROM user WHERE uname LIKE '%xxx%' AND sex = ? ``` 该机制简化了动态查询条件的拼接逻辑[^1]。 --- ### 3.2 `<where>` 标签与 `<if>` 标签配合使用 为了实现更灵活的条件查询,通常将 `<where>` 标签与 `<if>` 标签结合使用。每个 `<if>` 标签代表一个可选的查询条件,只有当对应参数满足测试表达式时才会被包含在最终的 SQL 语句中。 示例代码如下: ```xml <select id="findSearch" resultType="com.shiro.shirodemo.entity.Outbound"> SELECT outbound.outboundID as outBoundId, outbound.equipmentID as equipmentId, outbound.encodingID as encodingId, outbound.classes as classes, outbound.foursID as foursId, outbound.outname as outName, outbound.outstate as outState, outbound.outtime as outTime, outbound.printtime as printTime, outbound.printstate as printState FROM outbound, `order` <where> <if test="outState != null">and outstate=#{outState}</if> <if test="printState != null"> and printstate=#{printState}</if> <if test="classes != null"> and classes=#{classes}</if> <if test="foursId != null"> and foursID=#{foursId}</if> </where> ORDER BY order_foursshop.ordertime DESC </select> ``` 在运行时,根据传入的参数值是否为空,MyBatis 会动态拼接对应的条件,并通过 `<where>` 标签确保 SQL 语句结构正确[^2]。 --- ### 3.3 `<where>` 标签与 `<include>` 标签联合使用 有时需要复用一些固定的查询条件片段,此时可以借助 `<sql>` 和 `<include>` 标签定义和引用公共的 SQL 片段,并与 `<where>` 标签一起使用以保持语法一致性。 例如: ```xml <sql id="del">stu.del=false</sql> <select id="getStudentByCondition" resultType="student"> select * from student <where> <include refid="del"></include> <if test="stu.name != null and stu.name != ''"> and stu.name like '%${stu.name}%' </if> </where> </select> ``` 这样可以在多个查询中重复使用 `stu.del=false` 的条件,并确保整个 SQL 语句的完整性与正确性[^3]。 --- ###
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值