动态SQL

前言

        为了简化编码,避免拼装SQL,MyBatis提供了对SQL语句的组装能力。提高了开发效率,降低了代码的维护难度,体现了MyBatis的灵活性、高度可配置性、可维护性。接下来我们将进一步探讨动态SQL。

1、动态SQL包含的元素

        通过如下表格展示:

        6082dc0a8659bc95d9abd0869ff2c7a5736.jpg

2、各个元素详细介绍

        2.1 if元素

            if常用的判断语句,程序猿都和它很熟悉。在这里用法类似,它有一个test属性联合使用。当test属性值为true时执行否则不执行

            实例:

<select id="findRolsesById" parameterType="String" resultMap="roleResultMap">
    select role_no,role_name,note from t_role where 1=1
    <if test="roleId !=null and roleId != ''"
       and role_id = id
    </if>
</select>

            存在问题,为什么会出现1=1这样莫名其妙的代码?这个问题降在2.3节进行介绍

        2.2 choose、when、otherwise元素

           程序猿不仅了解if语句,对switch...case...default语句也是相当的熟悉,choose、when、otherwise元素的作用和switch语句相同。

            实例:

<select id="findActiveBlogLike"
     resultType="Blog">
  SELECT * FROM BLOG WHERE state = ‘ACTIVE’
  <choose>
    <when test="title != null">
      AND title like #{title}
    </when>
    <when test="author != null and author.name != null">
      AND author_name like #{author.name}
    </when>
    <otherwise>
      AND featured = 1
    </otherwise>
  </choose>
</select>

        2.3 trim、where、set元素

            在2.1节我们提出了一个问题,为什么要加”1=1”这个条件,如果不加当条件不满足的时候where后直接跟and,这样就导致语法错误。现在我们可以通过where元素解决,MyBatis用where会对and和or进行合适的处理。

            实例:

<select id="findUserById" resultType="user">
           select * from user 
           <where>
               <if test="id != null">
                   id=#{id}
               </if>
               and deleteFlag=0;
           </where>
 </select>

            应用trim可以自定义去掉SQL语法,如下实例:

<select ... >
    select  ...
    <trim prefix="where" prefixOverrides="and">
    ...
    </trim>
</select>

        prefix:指定关键字。

        prefixOverrides:指定需要去掉的那种字符串(前缀)。

        在update语句中set语法也有相同的用法

        实例:

        不使用set的更新语句:

<update id="updateUser" parameterType="com.jin.learn.User">
           update user set 
           <if test="name != null">
               name = #{name},
           </if> 
           <if test="password != null">
               password = #{password},
           </if> 
           <if test="age != null">
               age = #{age}
           </if> 
           <where>
               <if test="id != null">
                   id = #{id}
               </if>
               and deleteFlag = 0;
           </where>
</update>

        使用set的更新语句

<update id="updateUser" parameterType="com.jin.learn.User">
           update user
        <set>
          <if test="name != null">name = #{name},</if> 
             <if test="password != null">password = #{password},</if> 
             <if test="age != null">age = #{age},</if> 
        </set>
           <where>
               <if test="id != null">
                   id = #{id}
               </if>
               and deleteFlag = 0;
           </where>
</update>

         应用trim的更新语句

<trim prefix="SET" suffixOverrides=",">
  ...
</trim>

        这里使用suffixOverrides(后缀)指定要去掉的字符。

        2.4 foreach元素

            和Java中的foreach一样是一个表示循环语句的元素。可以用它来遍历集合。

            实例:

<select id="selectPostIn" resultType="domain.blog.Post">
  SELECT *
  FROM POST P
  WHERE ID in
  <foreach item="item" index="index" collection="list"
      open="(" separator="," close=")">
        #{item}
  </foreach>
</select>

            属性介绍:

                a、collection,配置传递进来的参数名称,可以是一个数组、List、Set等

                b、item,表示集合中每一个元素进行迭代时的别名;

                c、index,指 定一个名字,用于表示在迭代过程中,每次迭代到的位置;

                d、open,表示该语句以什么开始,

                e、separator,表示在每次进行迭代之间以什么符号作为分隔符;

                f、close,表示以什么结束。

        注: 你可以传递一个 List 实例或者数组作为参数对象传给MyBatis。当你这么做的时候,MyBatis会自动将它包装在一个Map中,用名称在作为键。List实例将会以“list” 作为键,而数组实例将会以“array”作为键。

        2.5 bind元素

        作用:通过OGNL表达式自定义一个上下文变量,方便我们使用。

        实例

<select id="selectBlogsLike" resultType="Blog">
  <bind name="pattern" value="'%' + _parameter.getTitle() + '%'" />
  SELECT * FROM BLOG
  WHERE title LIKE #{pattern}
</select>

        pattern为传进来的参数。

总结

        到此我们了解了动态SQL,方便我们灵活构建映射器,结合前几篇文章,MyBatis基础的知识我们已经介绍完毕。想要真正学懂一个框架,我们还必须深入了解它的实现原理。这是一个不断学习的过程。

转载于:https://my.oschina.net/u/3942106/blog/1937446

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值