MyBatis 动态 SQL 常用标签用法合集,建议收藏!

1、动态SQL是什么:

        MyBatis是一个流行的Java持久化框架,它提供了动态SQL的功能,可以根据不同的条件拼接SQL语句。动态SQL是基于OGNL表达式的,通过使用一些特定的元素,如if、choose、trim、where、set和foreach,可以实现灵活的条件判断和SQL拼接。

2、标签的用法:

       1、 if 标签:if元素用于简单的条件判断,可以根据条件拼接SQL语句。例如,根据用户名进行模糊查询,如果用户名不为空,则拼接相应的条件,否则忽略该条件。

<select id="findUserInfoByUserNameWithIf" parameterType="UserInfo" resultType="userInfo">
  select * from user_info ui
  <if test="userName != null and userName != ''">
    where ui.userName like CONCAT('%', CONCAT(#{userName}, '%'))
  </if>
</select>

      2、 where 标签:where元素用于拼接SQL语句的where子句,只有在条件成立时才会加上where关键字。可以避免拼接多余的关键字,如where and或where or。

<select id="findUserInfoByUserNameAndStatus" parameterType="UserInfo" resultType="UserInfo">
  select * from user_info ui
  <where>
    <if test="userName != null and userName != ''">
      and ui.userName like CONCAT('%', CONCAT(#{userName}, '%'))
    </if>
    <if test="status > -1">
      and ui.status = #{status}
    </if>
  </where>
</select>

     3、 set 标签:set元素用于拼接update语句的set子句,只有在条件成立时才会加上set关键字。可以避免拼接多余的逗号。

<update id="updateUserInfo" parameterType="UserInfo">
  update user_info
  <set>
    <if test="userName != null and userName != ''">
      userName = #{userName},
    </if>
    <if test="password != null and password != ''">
      password = #{password}
    </if>
  </set>
  where id = #{id}
</update>

    4、 foreach 标签:用于循环遍历集合或数组,并将集合或数组中的元素拼接到SQL语句中。可以指定循环的索引、元素名称以及循环的起始和结束符号。

 <delete id="deleteLaneLedconfigByIds" parameterType="String">
        delete from t_lane_ledconfig where lane_id in 
        <foreach item="laneId" collection="array" open="(" separator="," close=")">
            #{laneId}
        </foreach>
    </delete>

    5、 sql 标签:用于定义可重用的SQL片段,类似于Java中的方法。可以在其他SQL语句中引用这些SQL片段。

<sql id="selectParkPayPpVo">
        select id, park_id, park_name, park_uuid, server_url, server_port, app_secret, status, admin_name, create_time,channel_type from t_park_pay_pp
    </sql>

    6、 include 标签:用于引用其他SQL语句中定义的SQL片段,类似于Java中的方法调用。可以将其他SQL片段插入到当前SQL语句中。

    7、 choose、when、otherwise 组合标签使用标签

       7.1. choose标签:choose标签类似于Java中的switch语句,用于选择满足条件的SQL语句执行。它包含多个when和一个otherwise子标签,按顺序判断when标签中的条件,如果有一个条件满足,则执行对应的SQL语句,否则执行otherwise中的SQL语句。

       7.2. when标签:when标签用于定义条件,当条件满足时,执行其中的SQL语句。

       7.3 otherwise标签:otherwise标签用于定义默认的SQL语句,当所有的when条件都不满足时,执行其中的SQL语句。

<select id="getUserList" parameterType="User" resultType="User">
  SELECT * FROM users
  <where>
    <choose>
      <when test="name != null">
        AND name = #{name}
      </when>
      <when test="age != null">
        AND age = #{age}
      </when>
      <otherwise>
        AND gender = #{gender}
      </otherwise>
    </choose>
  </where>
</select>

8、 trim 标签:有四种用法:

  • prefix:在trim标签内的SQL语句前添加指定内容。
  • suffix:在trim标签内的SQL语句末尾添加指定内容。
  • prefixOverrides:去除trim标签内的SQL语句的指定首部内容。
  • suffixOverrides:去除trim标签内的SQL语句的指定尾部内容。

         8.1. 去掉对于关键字、trim标签的prefix属性设置为"WHERE",prefixOverrides属性设置为"AND"。这样,当lastName或age为空时,就会去除多余的AND关键字

<select id="getUser" resultType="User">
  SELECT * FROM user_tab
  <trim prefix="WHERE" prefixOverrides="AND">
    <if test="lastName != null">
      AND last_name = #{lastName}
    </if>
    <if test="age != null">
      AND age = #{age}
    </if>
  </trim>
</select>

         8.2. 添加前缀和后缀、trim标签的prefix属性设置为"SET",suffixOverrides属性设置为","。这样,当lastName或age为空时,就会去除多余的逗号

<update id="updateUser" parameterType="User">
  UPDATE user_tab
  <trim prefix="SET" suffixOverrides=",">
    <if test="lastName != null">
      last_name = #{lastName},
    </if>
    <if test="age != null">
      age = #{age},
    </if>
  </trim>
  WHERE id = #{id}
</update>

3、MyBatis关联查询

       1、 MyBatis一对多关联查询:

<!--一对多-->
<resultMap id="myStudent1" type="student1">
    <id property="sid" column="sid"/>
    <result property="sname" column="sname"/>
    <result property="sex" column="sex"/>
    <result property="sage" column="sage"/>
    <collection property="list" ofType="teacher">
        <id property="tid" column="tid"/>
        <result property="tname" column="tname"/>
        <result property="tage" column="tage"/>
    </collection>
</resultMap>

<!--一对多-->
<select id="find1" resultMap="myStudent1">
    select  *  from  student1  s  left  join  teacher  t  on s.sid=t.sid
</select>

      2、MyBatis多对一关联查询

<!--多对一-->
<resultMap id="myTeacher" type="teacher">
    <id property="tid" column="tid"/>
    <result property="tname" column="tname"/>
    <result property="tage" column="tage"/>
    <association property="student1" javaType="Student1">
        <id property="sid" column="sid"/>
        <result property="sname" column="sname"/>
        <result property="sex" column="sex"/>
        <result property="sage" column="sage"/>
    </association>
</resultMap>


<!--多对一-->
<select id="find2" resultMap="myTeacher">
select  *  from  teacher  t right join student1 s on  t.sid=s.sid
</select>

     3、MyBatis多对多关联查询

<!--多对多 以谁为主表查询的时候,主表约等于1的一方,另一方相当于多的一方-->
<select id="find3" resultMap="myStudent1">
    select  *  from  student1 s  left join relevance r on  s.sid=r.sid  left join teacher t on  r.tid=t.tid
</select>

4、MyBatis中 <= 号报错解决方式:<![CDATA[<=]]>编写即可

        

  • 22
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiaolong_gogo

你的认可是我最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值