JPA 條件語句


JPA if && in語句示例

示例:

//按照參數順序填入sql
    @Query(value = "select count(1) from exam where class=?1 AND if(COALESCE(?2,NULL) IS NOT NULL, status in (?2), 1=1) AND score=?3", nativeQuery = true)
    int countByParam(int className, List<Integer> statusList, int score);

    //按照參數順序填入sql
    @Query(value = "select count(1) from exam where class=:class AND if(COALESCE(:statusList,NULL) IS NOT NULL, status in (:statusList), 1=1) AND score=:score", nativeQuery = true)
    int countByParam(@Param("class") int className, @Param("statusList")List<Integer> statusList, @Param("score")int score);
  • 需要注意的是,list參數statusList不可以為empty list。 如果statusList.size() == 0,則設置statusList=null。

  • 語句說明:if(exp1, exp2, exp3) 表示 return exp1 ? exp2 : exp3。exp3一般是 1=1,一個空操作占位,使得sql語句合法。

  • ?1 和 :class 是兩種帶入參數的方法,不可在同一個sql語句中出現,否則報錯:Mixed parameter strategies - use just one of named, positional or JPA-ordinal strategy

JPA sql語句調試

application.properties 中添加配置

spring.jpa.show-sql=true # 顯示sql語句
spring.jpa.properties.hibernate.format_sql=true #sql語句格式規范

關於上述if & in 語句的調試,看下check list empty的幾種寫法和問題

COALESCE

@Query(value = "select count(1) from exam where if(COALESCE(:statusList,NULL) IS NOT NULL, status in (:statusList), 1=1) AND score=:score", nativeQuery = true)
int countByParam(@Param("statusList")List<Integer> statusList, @Param("score")int score);
  • statusList = [1]
    select
        count(1) 
    from
        exam 
    where
        if(COALESCE(?,NULL) IS NOT NULL, status in (?), 1=1) 
        and score=? 
  • statusList = [1,2]
    select
        count(1)  
    from
        exam 
    where
        if(COALESCE(?, ?,NULL) IS NOT NULL, status in (?, ?), 1=1) 
        and score=? 
  • statusList=[]
    select
        count(1)  
    from
        exam  
    where
        if(COALESCE(,NULL) IS NOT NULL, status in (), 1=1) 
        and score=? 

sql語句中有空白,會報錯 You have an error in your SQL syntax,...

  • statusList = null
    select
        count(1)  
    from
        exam  
    where
        if(COALESCE(?,NULL) IS NOT NULL, status in (?), 1=1) 
        and score=? 

IS NOT NULL

@Query(value = "select count(1) from exam where if(:statusList IS NOT NULL, status in (:statusList), 1=1) AND score=:score", nativeQuery = true)
int countByParam(@Param("statusList")List<Integer> statusList, @Param("score")int score);
  • statusList = [1]
    select
        count(1)  
    from
        exam  
    where
        if(? IS NOT NULL, status in (?), 1=1) 
        and score=? 
  • statusList = [1,2]
    select
        count(1)  
    from
        exam  
    where
        if(?, ? IS NOT NULL, status in (?, ?), 1=1)  
        and score=? 

sql語句有誤,報錯:You have an error in your SQL syntax

  • statusList = []
    select
        count(1)  
    from
        exam  
    where
        if( IS NOT NULL, status in (), 1=1)   
        and score=? 

sql語句有誤,報錯:You have an error in your SQL syntax

  • statusList = null
    select
        count(1)  
    from
        exam  
    where
        if(? IS NOT NULL, status in (?), 1=1)   
        and score=? 

where 子句實際為 where 1=1 and score=?

參考:
官方文檔


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM