【Mybatis】动态SQL之where标签和if标签

1、使用 where 标签

在 mybatis 的 xml 文件编写 SQL 语句已经是开发中最常使用的功能。但是通常我们看到的 SQL 都是下面这样:

这是正确的写法:

<select id="selectTableNameByEntity" parameterType="TableName" resultType="TableName">
    select *
    from tableName t
    <where>
        <if test="id != null and id != ''">
            t.id = #{id}
        </if>
        <if test="userName != null and userName != ''">
            and t.userName = #{userName}
        </if>
        <if test="password != null and password != ''">
            and t.password = #{password}
        </if>
    </where>
</select>

可以发现,and 关键字总是放在 if 标签内部的前面,那么是否可以放在后面呢?例如:

<where>
    <if test="id != null and id != ''">
        t.id = #{id} and
    </if>
    <if test="userName != null and userName != ''">
        t.user_name = #{userName} and
    </if>
    <if test="password != null and password != ''">
        t.password = #{password}
    </if>
</where>

答案是不可以!

原因:用 <where> 和 <if> 进行组合时,对 <if> 条件进行判断,一旦条件不成立时,<where> 标签会把对应的 and 关键字去掉(还有 or 关键字),因此就不会对整个 sql 语句产生影响。

但需要注意的是,and 关键字要放在每个 <if> 语句中的库表字段赋值的前面。因为,一旦判断不成功,<where> 只会把库表字段前面的 and 关键字去掉(或者 or 关键字)

如果 password 为空,此时得到的 sql 语句如下所示:

select * from tableName where t.user_name = 'Jungle' and t.user_name = 'admin' and

可以看到,sql 语句后面多了一个 and,这明显不符合 sql 的语法,执行会报错!

2、不使用 where 标签


那是否可以不使用 <where> 标签呢?答案是可以的。例如:

<select id="selectTableNameByEntity" parameterType="TableName" resultType="TableName">
    select *
    from tableName t
    where
        1 = 1
        <if test="id != null and id != ''">
            and t.id = #{id}
        </if>
        <if test="userName != null and userName != ''">
            and t.userName = #{userName}
        </if>
        <if test="password != null and password != ''">
            and t.password = #{password}
        </if>
    where
</select>

假设代码正常执行,字段都不为 null,那么可以得到 sql

select * from tableName where 1=1 t.user_name = 'Jungle' and t.user_name = 'admin' and password='123456'

注意,不适用 <where> 标签,1=1 是必须的,否则 sql 执行会出错:

select * from tableName where and t.user_name = 'Jungle' and t.user_name = 'admin' and password='123456'

where 后面紧跟一个 and,这是不符合 mysql 语法的。

或者你还可以将 1=1 置于代码后面,这样 and 需要放在赋值字段后面,例如:

<select id="selectTableNameByEntity" parameterType="TableName" resultType="TableName">
    select *
    from tableName t
    where
        <if test="id != null and id != ''">
            t.id = #{id} and
        </if>
        <if test="userName != null and userName != ''">
            t.userName = #{userName} and
        </if>
        <if test="password != null and password != ''">
            t.password = #{password} and
        </if>
        1 = 1
    where
</select>

执行得到 sql 如下:

select * from tableName where t.user_name = 'Jungle' and t.user_name = 'admin' and password='123456' and 1=1

但总的来说,在程序中出现 1=1 这样冗余的代码是不够优雅的,应该尽量使用 mybatis 提供的标签来编写代码。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: MyBatis是一个流行的ORM框架,通过Mapper文件将Java对象映射到SQL语句。MyBatis的Mapper文件是一个XML文件,它包含了可以执行的SQL语句。动态SQLMyBatis的一个重要的功能,可以根据不同情况,生成不同的SQL语句,从而实现更加灵活的查询。 if标签MyBatis动态SQL的一种标签,它可以根据条件生成动态的SQL语句。if标签的语法如下: ``` <if test="condition1"> SQL语句 </if> ``` 其中,test属性表示需要判断的条件,可以是一个字符串表达式或者OGNL表达式。SQL语句则是当满足条件时需要执行的SQL语句。如果条件不满足,则不会执行这段SQL语句。 if标签可以包含多个嵌套的if标签,以实现更加复杂的条件判断。同时,还可以使用choose、when、otherwise标签结合if标签,实现更加灵活的条件判断。例如: ``` <select id="queryUsers" resultType="User"> SELECT * FROM users <where> <if test="username != null and username != ''"> AND username LIKE '%${username}%' </if> <if test="gender != null and gender != ''"> AND gender = #{gender} </if> <choose> <when test="orderBy == 'name'"> ORDER BY username </when> <when test="orderBy == 'age'"> ORDER BY age </when> <otherwise> ORDER BY id </otherwise> </choose> </where> </select> ``` 以上例子是一个查询用户的SQL语句,根据不同情况生成不同的SQL语句。其中,if标签根据传入的参数判断是否需要加入username、gender的查询条件,choose标签根据orderBy的值,生成不同的排序条件。 if标签MyBatis动态SQL的重要功能之一,可以根据条件生成不同的SQL语句,从而实现更加灵活的查询。熟练掌握if标签的使用,可以使MyBatis开发更加高效和灵活。 ### 回答2: Mybatis是一款流行的Java ORM框架,它提供了许多方便的功能,其中之一就是动态SQL。当我们需要根据不同的条件拼接SQL语句时,就可以使用动态SQL来实现。具体来说,if标签是其中一种实现方式,下面将详解其用法。 if标签可以用于where、set、foreach等标签中,用于判断当前条件是否成立,如果成立就拼接相应的SQL语句,否则不进行任何操作。其基本语法如下: <if test="condition"> SQL语句 </if> 其中,test属性用于指定判断条件,可以是简单的表达式或者是复杂的逻辑语句。下面是一些常用的判断条件: 1. 判断参数是否为空 <if test="param != null"> SQL语句 </if> 2. 判断字符串是否为空 <if test="str != ''"> SQL语句 </if> 3. 判断是否相等 <if test="param == 'value'"> SQL语句 </if> 4. 判断是否大于 <if test="param > 10"> SQL语句 </if> 5. 判断是否包含 <if test="str.indexOf('value') != -1"> SQL语句 </if> 除了以上几种基本用法,if标签还可以嵌套使用,用于实现更复杂的判断逻辑。例如: <if test="param1 != null"> SQL语句 <if test="param2 != null"> SQL语句 </if> </if> 上述代码示例中,如果param1不为空,就会拼接第一个SQL语句;如果param2也不为空,则会在第一个SQL语句之后再拼接第二个SQL语句。 总的来说,if标签Mybatis动态SQL的基础,能够帮助我们实现更加灵活的SQL拼接,提高代码的可读性和可维护性。学习和掌握其使用方法,对于开发高效、高质量的程序是非常有帮助的。 ### 回答3: MyBatis是一款非常优秀的Java持久层框架,提供了丰富的SQL查询方式。在进行SQL查询的时候,经常需要根据具体的条件组合而成不同的SQL语句。这个时候可以使用MyBatis动态SQL特性。在动态SQL中,if标签是常用的一种方式。下面就来详解一下if标签的用法。 if标签的作用:在SQL语句中根据判断条件动态生成SQL语句。相当于Java中的if语句。 if标签的用法:在Mapper.xml文件中,使用if标签包裹需要动态生成的SQL。if标签的属性为test,表示判断条件。属性值可以是任何符合OGNL规则的表达式,常见的有以下几种方式: 1. 如果条件不为空,则生成相应的SQL语句:<![CDATA[需要动态生成的SQL语句]]>。 例如: <select id="findUser" parameterType="User" resultMap="UserMap"> SELECT * FROM user <where> <if test="name != null and name != ''"> AND name = #{name} </if> </where> </select> 当调用findUser方法时,如果传入的name不为空,那么就会生成如下的SQL语句: SELECT * FROM user WHERE name = ? 2. 如果条件为true,则生成相应的SQL语句: 例如: <select id="findUsers" resultMap="UserMap"> SELECT * FROM user <where> <if test="name != null and name != ''"> AND name = #{name} </if> <if test="age != null"> AND age = #{age} </if> </where> </select> 当调用findUsers方法时,如果传入的name和age分别为"Tom"和20,那么就会生成如下的SQL语句: SELECT * FROM user WHERE name = 'Tom' AND age = 20 3. 如果条件为false,则不在生成相应的SQL语句: 例如: <select id="findUsers" resultMap="UserMap"> SELECT * FROM user <where> <if test="name != null and name != ''"> AND name = #{name} </if> <if test="age != null"> AND age = #{age} </if> </where> </select> 当调用findUsers方法时,如果传入的name为"Tom",age为空,那么就会生成如下的SQL语句: SELECT * FROM user WHERE name = 'Tom' if标签的嵌套: if标签可以进行嵌套,从而实现更加灵活的SQL生成方式。 例如: <select id="findUsers" resultMap="UserMap"> SELECT * FROM user <where> <if test="name != null and name != ''"> AND name = #{name} </if> <if test="age != null"> <if test="age < 18"> AND age < 18 </if> <if test="age >= 18 and age < 30"> AND age >= 18 AND age < 30 </if> <if test="age >= 30"> AND age >= 30 </if> </if> </where> </select> 当调用findUsers方法时,如果传入的name为空,age为25,那么就会生成如下的SQL语句: SELECT * FROM user WHERE age >= 18 AND age < 30 以上就是if标签的用法详解。使用if标签可以让查询条件更加灵活、动态,深受开发者们的喜爱。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值