Mybatis详解(三)-----动态SQL

本文参考https://www.cnblogs.com/ysocean/p/7289529.html#_label0

 动态的SQL

1、if语句

根据 name 和 age 来查询数据。如果name为空,那么将只根据age来查询;反之只根据name来查询

<select id="getSelectResult" parameterType="map" resultType="com.zhiyou100.zjc.bean.User">
         select * from users where
             <if test="name!=null and name!='' ">
                 name=#{name}
             </if>
             <if test="age>0">
                 and age=#{age}
             </if>
     </select>

2、if+where语句

 这个“where”标签会知道如果它包含的标签中有返回值的话,它就插入一个‘where’。此外,如果标签返回的内容是以AND 或OR 开头的,则它会剔除掉。

<select id="getSelectResult" parameterType="map" resultType="com.zhiyou100.zjc.bean.User">
         select * from users 
         <where>
             <if test="name!=null and name!='' ">
                 name=#{name}
             </if>
             <if test="age>0">
                 and age=#{age}
             </if>
         
         </where>
         
     </select>

 

 3、if+set语句 

  如果第一个条件name 为空,那么 sql 语句为:update users u set u.age=? where id=?

    如果第一个条件不为空,那么 sql 语句为:update users u set u.name = ? ,u.age = ? where id=?,使用set时    ,  要放在修改条件的后面

<!-- 根据 id 更新 user 表的数据 -->
<update id="updateUserById" parameterType="com.zhiyou100.zjc.bean.User">
    update users u
        <set>
            <if test="name != null and name != ''">
                u.name = #{name},
            </if>
            <if test="age>0">
                u.age = #{age}
            </if>
        </set>    
     where id=#{id}
</update>

 4、choose(when,otherwise) 语句

  有时候,我们不想用到所有的查询条件,只想选择其中的一个,查询条件有一个满足即可,使用 choose 标签可以解决此类问题,类似于 Java 的 switch 语句 

<select id="selectUserByChoose" resultType="com.ys.po.User" parameterType="com.ys.po.User">
      select * from user
      <where>
          <choose>
              <when test="id !='' and id != null">
                  id=#{id}
              </when>
              <when test="username !='' and username != null">
                  and username=#{username}
              </when>
              <otherwise>
                  and sex=#{sex}
              </otherwise>
          </choose>
      </where>
  </select>

 

 5、trim 语句

  trim标记是一个格式化的标记,可以完成set或者是where标记的功能

  ①、用 trim 改写上面第二点的 if+where 语句

     <select id="selectUserByUsernameAndSex" resultType="user" parameterType="com.ys.po.User">  
      select * from user
<!-- <where> <if test="username != null"> username=#{username} </if> <if test="username != null"> and sex=#{sex} </if> </where> -->
     <!--prefix:前缀
        prefixOverrides:去掉第一个and或者是or-->
<trim prefix="where" prefixOverrides="and | or"> <if test="username != null"> and username=#{username} </if> <if test="sex != null"> and sex=#{sex} </if> </trim>
</select>

②、用 trim 改写上面第三点的 if+set 语句

<!-- 根据 id 更新 user 表的数据 -->
    <update id="updateUserById" parameterType="com.ys.po.User">
        update user u
            <!-- <set>
                <if test="username != null and username != ''">
                    u.username = #{username},
                </if>
                <if test="sex != null and sex != ''">
                    u.sex = #{sex}
                </if>
            </set> -->
       <!--suffix:后缀
          suffixOverrides:去掉最后一个逗号(也可以是其他的标记,就像是上面前缀中的and一样)--->
       <trim prefix="set" suffixOverrides=","> <if test="username != null and username != ''"> u.username = #{username}, </if> <if test="sex != null and sex != ''"> u.sex = #{sex}, </if> </trim> where id=#{id} </update>

 

 6、SQL 片段

  有时候可能某个 sql 语句我们用的特别多,为了增加代码的重用性,简化代码,我们需要将这些代码抽取出来,然后使用时直接调用。

  比如:假如我们需要经常根据用户名和性别来进行联合查询,那么我们就把这个代码抽取出来,如下:

  提取出来的字段:

 <sql id="usercolums">
         id,name,age,sex
     </sql>

    引用sql片段

 1 <select id="getSelectResult" parameterType="map" resultType="com.zhiyou100.zjc.bean.User">
 2          select <include refid="usercolums"/> from users 
 3          <where>
 4              <if test="name!=null and name!='' ">
 5                  name=#{name}
 6              </if>
 7              <if test="age>0">
 8                  and age=#{age}
 9              </if>
10          
11          </where>
12          
13      </select>

 7、foreach 语句

需求:我们需要查询 user 表中 id 分别为1,2,3的用户

   sql语句: select * from user where id in (1,2,3)

使用用 foreach 来操作:

<select id="selectUserByListId" parameterType="com.ys.vo.UserVo" resultType="com.ys.po.User">
        select * from user
        <where>
            <!--
                collection:指定输入对象中的集合属性
                item:每次遍历生成的对象
                open:开始遍历时的拼接字符串
                close:结束时拼接的字符串
                separator:遍历对象之间需要拼接的字符串
                select * from user where 1=1 and id in (1,2,3)
              -->
            <foreach collection="ids" item="id" open="and id in (" close=") " separator=",">
                #{id}
            </foreach>
        </where>
    </select>

 

posted @ 2019-08-30 21:35  小成~  阅读(149)  评论(0编辑  收藏  举报