亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

mybatis的mapper特殊字符轉移及動態(tài)SQL條件查詢小結

 更新時間:2021年09月24日 16:09:39   作者:kenx  
mybatis mapper文件中條件查詢符,如>=,<,之類是不能直接寫的會報錯的需要轉移一下,本文給大家介紹了常見的條件查詢操作,對mybatis的mapper特殊字符及動態(tài)SQL條件查詢相關知識感興趣的朋友一起看看吧

前言

我們知道在項目開發(fā)中之前使用數(shù)據(jù)庫查詢,都是基于jdbc,進行連接查詢,然后是高級一點jdbcTemplate進行查詢,但是我們發(fā)現(xiàn)還是不是很方便,有大量重復sql語句,與代碼偶合,效率低下,于是就衍生出來ORM框架,如Mybatis,Hibernate,還有SpringBoot的,Spring Data JPA

條件查詢

我們知道在mybatis mapper文件中條件查詢符,如>=,<,之類是不能直接寫的會報錯的需要轉移一下 如下圖表

詳細內容參考

常見的條件查詢操作有

我們通過mybatis 提供的特有標簽進行條件判斷,達到動態(tài)拼接sql語句

if標簽 where標簽 choose when otherwise標簽 foreach標簽

快速入門

if標簽

語法:

<if test="xxx != null and xxx != ''">

test中寫判斷條件 參數(shù)直接paramN或者別名 多個條件使用and或者or連接

只要條件成立就拼接在Sql語句中,都成立就全部都拼接

注意where子句中加上1=1來規(guī)避and的風險

如下例子:

<select id="selg" resultType="log">
    select * from log where 1=1
    <if test="param1!=null and param1!=''">
    and outno=#{param1}
    </if>
    <if test="param2!=null and param2!=''">
    and inno=#{param2}
    </if>
</select>

where標簽

對上面if標簽條件判斷where連接做了處理會自動的給Sql語句添加where關鍵字,并將第一個and去除

上面sql可以改造成如下:

<select id="selg" resultType="log">
      select * from log 
  <where>
       <if test="param1!=null and param1!=''">
      and outno=#{param1}
      </if>
      <if test="param2!=null and param2!=''">
      and inno=#{param2}
      </if>
  </where>
     
</select>

choose when otherwise標簽

類似于Java語法中的,case,switch語句判斷

條件只要有一個成立,其他的就不會再判斷了。如果沒有成立的條件則默認執(zhí)行otherwise中的內容

上面sql可以改造成如下:

<select id="selg" resultType="log">
      select * from log 
  <where>
    <choose>
       <when test="param1!=null and param1!=''">
      and outno=#{param1}
      </when>
      <when test="param2!=null and param2!=''">
      and inno=#{param2}
      </when>
      <otherwise>
        and 1=1
      </otherwise>
    </choose>
  </where>
     
</select>

foreach標簽

語法:

 <foreach collection="idList" item="id" open="(" separator="," close=")">
</foreach>

  • collection:要遍歷的集合對象
  • item:記錄每次遍歷的結果
  • open:在結果的左邊添加內容
  • separator:結果和結果之間的內容
  • close:在最后添加的內容

常用于in查詢,和批量插入操作 如下案例:

<select id="selF" parameterType="list" resultType="account">
    select * from account where ano in
    <foreach collection="list" item="item" open="(" separator="," close=")">
    #{item}
    </foreach>
   </select>


<insert id="insertBatch">
        INSERT INTO t_user
        (id, name, password)
        VALUES
        <foreach collection ="userList" item="user" separator =",">
            (#{user.id}, #{user.name}, #{user.password})
        </foreach >
    </insert>

其他標簽使用參考點擊進入·

場景案例

1.當我們需要對多張表的關聯(lián)數(shù)據(jù)進行復雜動態(tài)條件查詢的時候,就需要用到 if標簽進行判斷 如下

根據(jù)用戶手機號姓名年齡性別,等進行動態(tài)條件檢索,這個時候我們需要動態(tài)通過調節(jié)去拼接sql 當條件滿足sql語句加上對應條件差許

<select id="findUsersByUser" resultType="cn.soboys.kmall.sys.entity.User">
        select tu.USER_ID,tu.USERNAME,tu.SSEX,td.DEPT_NAME,tu.MOBILE,tu.EMAIL,tu.STATUS,tu.CREATE_TIME,
        td.DEPT_ID
        from t_user tu left join t_dept td on tu.DEPT_ID = td.DEPT_ID
        where tu.ADMIN_TYPE_ID  &gt;= 0 AND tu.ADMIN_TYPE_ID  &lt;= #{userParams.adminType}
        <if test="userParams.roleId != null and userParams.roleId != ''">
           and (select group_concat(ur.ROLE_ID)
            from t_user u
            right join t_user_role ur on ur.USER_ID = u.USER_ID,
            t_role r
            where r.ROLE_ID = ur.ROLE_ID
            and u.USER_ID = tu.USER_ID and r.ROLE_ID=#{userParams.roleId})
        </if>


        <if test="userParams.mobile != null and userParams.mobile != ''">
            AND tu.MOBILE =#{userParams.mobile}
        </if>
        <if test="userParams.username != null and userParams.username != ''">
            AND tu.USERNAME   like CONCAT('%',#{userParams.username},'%')
        </if>
        <if test="userParams.ssex != null and userParams.ssex != ''">
            AND tu.SSEX  =#{userParams.ssex}
        </if>
        <if test="userParams.status != null and userParams.status != ''">
            AND tu.STATUS =#{userParams.status}
        </if>
        <if test="userParams.deptId != null and userParams.deptId != ''">
            AND td.DEPT_ID =#{userParams.deptId}
        </if>
        <if test="userParams.createTime != null and userParams.createTime != ''">
            AND DATE_FORMAT(tu.CREATE_TIME,'%Y%m%d') BETWEEN substring_index(#{userParams.createTime},'#',1) and substring_index(#{userParams.createTime},'#',-1)
        </if>
    </select>

對應mapper對應的方法

<T> IPage<User> findUsersByUser(Page<T> page, @Param("userParams") SearchUserParams userParams);

對應參數(shù)實體對象

@Data
public class SearchUserParams {
    private String username;
    private String mobile;
    private String status;
    private String ssex;
    private Long deptId;
    private String createTime;
    private long adminType;
    private String roleId;
}

通過if標簽去判斷條件是否滿足,滿足就拼接對應sql

注意在上面我們提到的條件拼接第一個是where連接,而不是and應規(guī)避and風險保證sql語法正確 如下

<select id="findSearchCouponsPage" parameterType="cn.soboys.kmall.bean.web.params.SearchCouponParams" resultType="coupon">
        select *
        from coupon c
        left join user_coupon uc on c.coupon_id = uc.coupon_id
        WHERE 1 = 1
        <if test="couponParams.userId != null and couponParams.userId != ''">
           and uc.user_id =#{couponParams.userId}
        </if>
        <if test="couponParams.status != null and couponParams.status != ''">
            and c.status =#{couponParams.status}
        </if>
        <if test="couponParams.couponId != null and couponParams.couponId != ''">
            and c.coupon_id =#{couponParams.couponId}
        </if>
        <if test="couponParams.couponType != null and couponParams.couponType != ''">
            and c.type =#{couponParams.couponType}
        </if>
    </select>

我們可以通過假定給他一個默認條件 WHERE 1 = 1來解決,也可以通過嵌套where標簽來解決

到此這篇關于mybatis的mapper特殊字符轉移及動態(tài)SQL條件查詢的文章就介紹到這了,更多相關mybatis的mapper特殊字符轉移內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 雙重檢查鎖定模式Java中的陷阱案例

    雙重檢查鎖定模式Java中的陷阱案例

    這篇文章主要介紹了雙重檢查鎖定模式Java中的陷阱,雙重檢查鎖定(也叫做雙重檢查鎖定優(yōu)化)是一種軟件設計模式,它的作用是減少延遲初始化在多線程環(huán)境下獲取鎖的次數(shù),尤其是單例模式下比較突出,想具體了解的小伙伴可以參考下面文章內容,附呦詳細的舉例說明
    2021-10-10
  • SpringBoot中@ControllerAdvice注解的使用方法

    SpringBoot中@ControllerAdvice注解的使用方法

    這篇文章主要介紹了SpringBoot中@ControllerAdvice注解的使用方法,這是一個增強的?Controller,對controller層做異常處理、數(shù)據(jù)預處理、全局數(shù)據(jù)綁定,?springboot?會自動掃描到,不需要調用,這個注解是spring?MVC提供的,在springboot中也可以使用,需要的朋友可以參考下
    2024-01-01
  • JAVA函數(shù)的定義、使用方法實例分析

    JAVA函數(shù)的定義、使用方法實例分析

    這篇文章主要介紹了JAVA函數(shù)的定義、使用方法,結合實例形式分析了JAVA函數(shù)的基本概念、功能、原理、定義、使用方法與操作注意事項,需要的朋友可以參考下
    2020-04-04
  • Java在并發(fā)環(huán)境中SimpleDateFormat多種解決方案

    Java在并發(fā)環(huán)境中SimpleDateFormat多種解決方案

    這篇文章主要介紹了Java在并發(fā)環(huán)境中SimpleDateFormat多種解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-07-07
  • Java讓泛型實例化的方法

    Java讓泛型實例化的方法

    這篇文章主要介紹了Java讓泛型實例化的方法,文中示例代碼非常詳細,幫助大家更好的理解和學習,感興趣的朋友可以了解下
    2020-07-07
  • intellij idea的快速配置使用詳細教程

    intellij idea的快速配置使用詳細教程

    這篇文章主要介紹了intellij idea的快速配置使用詳細教程,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-08-08
  • 基于Java實現(xiàn)一個高效可伸縮的計算結果緩存

    基于Java實現(xiàn)一個高效可伸縮的計算結果緩存

    這篇文章將通過對一個計算結果緩存的設計迭代介紹,分析每個版本的并發(fā)缺陷,并分析如何修復這些缺陷,最終完成一個高效可伸縮的計算結果緩存,感興趣的小伙伴可以了解一下
    2023-06-06
  • Java實現(xiàn)的簡單音樂播放器功能示例

    Java實現(xiàn)的簡單音樂播放器功能示例

    這篇文章主要介紹了Java實現(xiàn)的簡單音樂播放器功能,涉及java針對多媒體文件相關載入、播放相關操作技巧,需要的朋友可以參考下
    2019-02-02
  • Java8使用LocalDate計算日期實例代碼解析

    Java8使用LocalDate計算日期實例代碼解析

    這篇文章主要介紹了Java8使用LocalDate計算實例代碼解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-04-04
  • Java8進行多個字段分組統(tǒng)計的實例代碼

    Java8進行多個字段分組統(tǒng)計的實例代碼

    在本篇文章里小編給大家分享的是關于Java8進行多個字段分組統(tǒng)計的實例代碼,需要的朋友們可以學習下。
    2020-05-05

最新評論