欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

一篇文章帶你了解mybatis的動態(tài)SQL

 更新時間:2022年01月23日 11:23:15   作者:YSOcean  
這篇文章主要為大家介紹了mybatis的動態(tài)SQL?,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助

1、動態(tài)SQL:if 語句

根據(jù) username 和 sex 來查詢數(shù)據(jù)。如果username為空,那么將只根據(jù)sex來查詢;反之只根據(jù)username來查詢

首先不使用 動態(tài)SQL 來書寫

<select id="selectUserByUsernameAndSex"
        resultType="user" parameterType="com.ys.po.User">
    <!-- 這里和普通的sql 查詢語句差不多,對于只有一個參數(shù),后面的 #{id}表示占位符,里面不一定要寫id,
            寫啥都可以,但是不要空著,如果有多個參數(shù)則必須寫pojo類里面的屬性 -->
    select * from user where username=#{username} and sex=#{sex}
</select>

上面的查詢語句,我們可以發(fā)現(xiàn),如果 #{username} 為空,那么查詢結果也是空,如何解決這個問題呢?使用 if 來判斷

<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>
</select>

這樣寫我們可以看到,如果 sex 等于 null,那么查詢語句為 select * from user where username=#{username},但是如果usename 為空呢?那么查詢語句為 select * from user where and sex=#{sex},這是錯誤的 SQL 語句,如何解決呢?請看下面的 where 語句

2、動態(tài)SQL: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>
</select>

這個“where”標簽會知道如果它包含的標簽中有返回值的話,它就插入一個‘where’。此外,如果標簽返回的內容是以AND 或OR 開頭的,則它會剔除掉。

3、動態(tài)SQL:if+set 語句

同理,上面的對于查詢 SQL 語句包含 where 關鍵字,如果在進行更新操作的時候,含有 set 關鍵詞,我們怎么處理呢?

<!-- 根據(jù) id 更新 user 表的數(shù)據(jù) -->
<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>
     where id=#{id}
</update>

這樣寫,如果第一個條件 username 為空,那么 sql 語句為:update user u set u.sex=? where id=?

如果第一個條件不為空,那么 sql 語句為:update user u set u.username = ? ,u.sex = ? where id=?

4、動態(tài)SQL: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>

也就是說,這里我們有三個條件,id,username,sex,只能選擇一個作為查詢條件

如果 id 不為空,那么查詢語句為:select * from user where id=?

如果 id 為空,那么看username 是否為空,如果不為空,那么語句為 select * from user whereusername=?;

如果 username 為空,那么查詢語句為 select * from user where sex=?

5、動態(tài)SQL: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>  -->
        <trim prefix="where" prefixOverrides="and | or">
            <if test="username != null">
               and username=#{username}
            </if>
            <if test="sex != null">
               and sex=#{sex}
            </if>
        </trim>
    </select>

prefix:前綴      

prefixoverride:去掉第一個and或者是or

②、用 trim 改寫上面第三點的 if+set語句

<!-- 根據(jù) id 更新 user 表的數(shù)據(jù) -->
    <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> -->
            <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>

suffix:后綴  

suffixoverride:去掉最后一個逗號(也可以是其他的標記,就像是上面前綴中的and一樣)

6、動態(tài)SQL: SQL 片段

有時候可能某個 sql 語句我們用的特別多,為了增加代碼的重用性,簡化代碼,我們需要將這些代碼抽取出來,然后使用時直接調用。

比如:假如我們需要經常根據(jù)用戶名和性別來進行聯(lián)合查詢,那么我們就把這個代碼抽取出來,如下:

<!-- 定義 sql 片段 -->
<sql id="selectUserByUserNameAndSexSQL">
    <if test="username != null and username != ''">
        AND username = #{username}
    </if>
    <if test="sex != null and sex != ''">
        AND sex = #{sex}
    </if>
</sql>

引用 sql 片段

注意:

①、最好基于 單表來定義 sql 片段,提高片段的可重用性

②、在 sql 片段中最好不要包括 where

7、動態(tài)SQL: foreach 語句

需求:我們需要查詢 user 表中 id 分別為1,2,3的用戶

sql語句:

select * from user where id=1 or id=2 or id=3

select * from user where id in (1,2,3)

①、建立一個 UserVo 類,里面封裝一個 List<Integer> ids 的屬性

package com.ys.vo;
import java.util.List;
public class UserVo {
    //封裝多個用戶的id
    private List<Integer> ids;
    public List<Integer> getIds() {
        return ids;
    }
    public void setIds(List<Integer> ids) {
        this.ids = ids;
    }
}  

②、我們用 foreach 來改寫select * from user where id=1 or id=2 or id=3

<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=1 or id=2 or id=3)
          -->
        <foreach collection="ids" item="id" open="and (" close=")" separator="or">
            id=#{id}
        </foreach>
    </where>
</select>

測試:

//根據(jù)id集合查詢user表數(shù)據(jù)
@Test
public void testSelectUserByListId(){
    String statement = "com.ys.po.userMapper.selectUserByListId";
    UserVo uv = new UserVo();
    List<Integer> ids = new ArrayList<>();
    ids.add(1);
    ids.add(2);
    ids.add(3);
    uv.setIds(ids);
    List<User> listUser = session.selectList(statement, uv);
    for(User u : listUser){
        System.out.println(u);
    }
    session.close();
}

③、我們用 foreach 來改寫select * from user where id in (1,2,3)

<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>

8、總結

其實動態(tài) sql 語句的編寫往往就是一個拼接的問題,為了保證拼接準確,我們最好首先要寫原生的 sql 語句出來,然后在通過 mybatis 動態(tài)sql 對照著改,防止出錯。

本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關注腳本之家的更多內容!

相關文章

  • JPA設置默認字段及其長度詳解

    JPA設置默認字段及其長度詳解

    JPA是Java Persistence API的簡稱,中文名Java持久層API,是JDK 5.0注解或XML描述對象-關系表的映射關系,并將運行期的實體對象持久化到數(shù)據(jù)庫中。本文主要介紹了JPA如何設置默認字段及其長度,感興趣的同學可以了解一下
    2021-12-12
  • IntelliJ IDEA使用tomcat和jetty配置詳解

    IntelliJ IDEA使用tomcat和jetty配置詳解

    這篇文章主要介紹了IntelliJ IDEA使用tomcat和jetty配置詳解,以便進一步地開發(fā)和調試,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-10-10
  • Java生成二維碼的實例代碼

    Java生成二維碼的實例代碼

    這篇文章主要介紹了Java生成二維碼的實例代碼,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2020-09-09
  • SSM框架前后端信息交互實現(xiàn)流程詳解

    SSM框架前后端信息交互實現(xiàn)流程詳解

    這篇文章主要介紹了SSM框架前后端信息交互實現(xiàn)流程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-07-07
  • Java結合Vue項目打包并進行服務器部署

    Java結合Vue項目打包并進行服務器部署

    本文主要介紹了Java結合Vue項目打包并進行服務器部署,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-07-07
  • 詳解Spring Cloud Zuul重試機制探秘

    詳解Spring Cloud Zuul重試機制探秘

    本篇文章主要介紹了詳解Spring Cloud Zuul重試機制探秘,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • Java并發(fā)編程之Exchanger方法詳解

    Java并發(fā)編程之Exchanger方法詳解

    這篇文章主要介紹了Java并發(fā)編程之Exchanger方法詳解,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-04-04
  • struts+spring+hibernate三個框架的整合

    struts+spring+hibernate三個框架的整合

    這篇文章主要介紹了struts+spring+hibernate三個框架的整合,需要的朋友可以參考下
    2017-09-09
  • JAVA JNI函數(shù)的注冊過程詳細介紹

    JAVA JNI函數(shù)的注冊過程詳細介紹

    這篇文章主要介紹了JAVA JNI函數(shù)的注冊過程詳細介紹的相關資料,需要的朋友可以參考下
    2016-11-11
  • 二叉搜索樹實例練習

    二叉搜索樹實例練習

    一棵二叉查找樹是按二叉樹結構來組織的。這樣的樹可以用鏈表結構表示,其中每一個結點都是一個對象
    2012-11-11

最新評論