MyBatis中的常见的标签(resultMap)sql where if标签 set标签 trim标签 foreach标签

MyBatis动态SQL详解
本文深入解析MyBatis中的动态SQL标签,包括resultMap、sql、where、set、trim和foreach,阐述如何通过这些标签实现高效的数据操作,如实体类与数据表映射、SQL片段复用、条件查询、批量操作等。

MyBatis中的标签

1.resultMap

完成实体类和数据表的映射
<resultMap id="唯一表示" type="声明映射的对象">
<id property="对象中的主键属性" cloum="表中的主键">
<result property="对象中的主键属性" cloum="表中的对应字段">
</resultMap>

2.sql标签

能够提取sql语句中的公共部分使用 <include refid="sql标签的id"/>

<!--抽取公共的sql片段-->
    <sql id="baseUser">
        id,
        name,
        password,
        role_name
    </sql>
<select id="getUserList" resultMap="userResultMap">
    select
     <include refid="baseUser"/>
     from t_user;
</select>

3.where标签

能够给sql语句自动添加一个where,而且能够去掉sql中多余的and

if标签

判断传入的参数值是否为空或者为空字符串,达到根据不同的条件查询不同数据的目的

<select id="findUserList" resultMap="userResultMap">
        select
        <include refid="baseUser"/>
        from t_user
        <where>
            <if test="name != null and name != ''">
                and name like concat("%",#{name},"%")
            </if>
            <if test="password != null and password !=''">
                and password=#{password}
            </if>
        </where>

    </select>

4.set标签

为sql语句添加一个set,并且能够去掉末尾多余的,

 <update id="updateUser">
        update t_user
        <set>
            <if test="name != null and name != ''">
                name = #{name},
            </if>
            <if test="password != null and password != ''">
                password = #{password},
            </if>
        </set>
        where id=#{id}
    </update>

5.trim标签

可以代替where标签和set标签

  <update id="updateUser1">
        update t_user
        <!--
       prefix:前缀在语句之前添加内容
       suffix:后缀在语句之后添加的内容
       suffixOverrides:后缀覆盖,去除后面多余的,
       prefixOverrides:前缀覆盖,去除前面多余的and
        -->
      <trim prefix="set" suffix="" prefixOverrides="" suffixOverrides=",">
          <if test="name != null and name != ''">
              name = #{name},
          </if>
          <if test="password != null and password != ''">
              password = #{password},
          </if>
      </trim>
           where id=#{id}
    </update>

6.foreach标签

用户批量删除,循环遍历list

    <delete id="batchDel">
        DELETE from t_user where id  in
        <!--
        collection:遍历的集合
        open:开始遍历是调用
        close:结束时调用
        item:当前遍历的对象
        separator:每次遍历都执行
        index:当前遍历的索引值
        -->
        <foreach collection="list" open="(" close=")" item="id" separator="," index="index">
        #{id}
        </foreach>
    </delete>
### MyBatis 动态 SQL 常用标签及其作用与使用方法 #### 1. `<if>` 标签 `<if>` 标签用于实现条件判断,当满足指定条件时,才会将对应的 SQL 片段包含在最终生成的 SQL 语句中[^2]。 ```xml <select id="findUser" resultType="User"> SELECT * FROM user <where> <if test="name != null">AND name = #{name}</if> <if test="age != null">AND age = #{age}</if> </where> </select> ``` 上述代码表示只有当 `name` 或 `age` 参数不为空时,才会将其作为查询条件。 #### 2. `<where>` 标签 `<where>` 标签自动处理 SQL 中的 `WHERE` 关键字,并且会智能地去掉多余的 `AND` 或 `OR`[^1]。 ```xml <select id="findUser" resultType="User"> SELECT * FROM user <where> <if test="name != null">name = #{name}</if> <if test="age != null">AND age = #{age}</if> </where> </select> ``` 如果所有条件都不满足,则 `<where>` 标签不会生成任何内容。 #### 3. `<trim>` 标签 `<trim>` 标签可以更灵活地控制 SQL 的前缀、后缀以及去除多余的关键字[^1]。 ```xml <select id="findUser" resultType="User"> SELECT * FROM user <trim prefix="WHERE" prefixOverrides="AND |OR"> <if test="name != null">AND name = #{name}</if> <if test="age != null">AND age = #{age}</if> </trim> </select> ``` 此示例中,`prefixOverrides` 属性会移除生成 SQL 中多余的 `AND` 或 `OR`。 #### 4. `<set>` 标签 `<set>` 标签主要用于更新操作,自动处理 `SET` 关键字并移除多余的逗号。 ```xml <update id="updateUser"> UPDATE user <set> <if test="name != null">name = #{name},</if> <if test="age != null">age = #{age},</if> </set> WHERE id = #{id} </update> ``` #### 5. `<foreach>` 标签 `<foreach>` 标签用于遍历集合,常用于批量插入或批量更新场景[^1]。 ```xml <insert id="batchInsert"> INSERT INTO user (name, age) VALUES <foreach collection="list" item="user" separator=","> (#{user.name}, #{user.age}) </foreach> </insert> ``` 上述代码实现了对用户列表的批量插入。 #### 6. `<choose>`、`<when>` 和 `<otherwise>` 标签 这些标签类似于 Java 中的 `switch` 语句,用于多条件分支判断[^2]。 ```xml <select id="findUser" resultType="User"> SELECT * FROM user <where> <choose> <when test="name != null">name = #{name}</when> <when test="age != null">age = #{age}</when> <otherwise>del_flag = 0</otherwise> </choose> </where> </select> ``` #### 7. `<bind>` 标签 `<bind>` 标签用于创建上下文变量,特别适用于模糊查询场景[^3]。 ```xml <select id="getStudent" resultMap="student"> <bind name="pattern" value="'%'+parameter+'%'"/> SELECT * FROM student WHERE name LIKE #{pattern} </select> ``` #### 8. `<sql>` 和 `<include>` 标签 `<sql>` 标签用于定义可复用的 SQL 片段,而 `<include>` 标签则用于引用这些片段[^4]。 ```xml <sql id="Base_Column_List"> id, name, age, hobby, del_flag, create_time, update_time </sql> <select id="listAnimals" resultType="com.zhang.entity.Animal"> SELECT <include refid="Base_Column_List"/> FROM animal </select> ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值