PageRequest pageable = PageRequest.of(page, limit,new Sort(Direction.DESC,"XXX").and(new Sort(Direction.DESC,"XXX")));
1. 動態Where條件查詢
注意如果有動態where,而且沒有一項確定的值,需要加where 1=1
如果不爲空查詢,爲空不查詢
@Query(value = "select * from bug_data where if(?3 !='',bug_level=?3 ,1=1 )
and if(?2 !='',bug_type=?2 ,1=1 ) and if(?1 !='',LOCATE(?1, bug_name)>0,1=1 ) ",
nativeQuery = true)
public Page<BugData> dongtai3(@Param("bugName") String bugName,
@Param("bugType") String bugType,
@Param("bugLevel") String bugLevel, Pageable pageable);
@Query(value = "select * from xxxx where if(?3 !='',a=?3 ,1=1 )
and if(?2 !='',b=?2 ,1=1 ) and if(?1 !='',LOCATE(?1, c)>0,1=1 ) ",
nativeQuery = true)
public Page<BugData> dongtai3(@Param("a") String a,
@Param("b") String b,
@Param("c") String c, Pageable pageable);
但是不能進行一個多個字段查詢,比如 我要查詢 字段從c ='1'或者c='2'
的,或者c =1 or c=2,因爲String類型的C不能傳過來兩個字段,所以這個方案被否決了
//要查詢的語句
select bug_name,bug_type,bug_level,project_id, COUNT(project_id) nun
from bug_data where bug_level =? and if(? !='',bug_type=? ,1=1 )
and if(? !='',LOCATE(?, bug_name)>0,1=1 )GROUP BY bug_name HAVING nun >? limit ?
//要查詢的語句的count
select count(bug_name) from bug_data where
bug_level =? and if(? !='',bug_type=? ,1=1 )
and if(? !='',LOCATE(?, bug_name)>0,1=1 )
GROUP BY bug_name HAVING COUNT(project_id) >?
但是要注意的是,如果你有4個參數,必須都要寫if不然會報錯
JPA Could not locate ordinal parameter [4], expecting one of [1, 2, 3]
@Query(value = "select bug_name,bug_type,bug_level,project_id, COUNT(project_id) nun" +
" from bug_data where bug_level =?1 and if(?3 !='',bug_type=?3 ,1=1 ) " +
" and if(?2 !='',LOCATE(?2, bug_name)>0,1=1 )" +
"GROUP BY bug_name HAVING nun >?4",
nativeQuery = true,
countQuery = "select count(bug_name) from bug_data where bug_level =?1 and " +
" if(?3 !='',bug_type=?3 ,1=1 ) and if(?2 !='',LOCATE(?2, bug_name)>0,1=1 )" +
"GROUP BY bug_name HAVING COUNT(project_id) >?4")
public Page<Map<String,Object>> dongtai4(@Param("bugLevel") String bugLevel,
@Param("bugName") String bugName, @Param("bugType") String bugType,
@Param("eff") Integer eff,Pageable pageable);
IN操作爲什麼不行
@Query(value = "select bug_name,bug_type,bug_level,project_id, COUNT(project_id) nun" +
" from bug_data where 1=1 and if(?1 !='',bug_level in(?1) ,1=1 ) and if(?3 !='',bug_type=?3 ,1=1 ) " +
" and if(?2 !='',LOCATE(?2, bug_name)>0,1=1 )" +
"GROUP BY bug_name HAVING nun >?4",
nativeQuery = true,
countQuery = "select count(bug_name) from bug_data where where 1=1 and if(?1 !='',bug_level in(?1) ,1=1 ) and " +
" if(?3 !='',bug_type=?3 ,1=1 ) and if(?2 !='',LOCATE(?2, bug_name)>0,1=1 )" +
"GROUP BY bug_name HAVING COUNT(project_id) >?4")
public Page<Map<String,Object>> dongtaiIn(@Param("bugLevelList") List<String> bugLevelList,
@Param("bugName") String bugName, @Param("bugType") String bugType,
@Param("eff") Integer eff,Pageable pageable);
道理上是行得通的,但是,如果用in 就會變成
不滿足三元運算符了
if('CWE-189', 'CWE-119' !='',bug_type in('CWE-189', 'CWE-119') ,1=1'
怎麼解決IN操作呢,判斷非空用另外一個參數就好了
@Query(value = "select bug_name,bug_type,bug_level,project_id, " +
"COUNT(project_id) nun" +
" from bug_data where 1=1 " +
" and if(?4 >0 ,bug_level in (?3) ,1=1 )" +
" and if(?2 >0,bug_type in (?1) ,1=1 ) " +
" and if(?7 !='',LOCATE(?7, bug_name)>0,1=1 )" +
" and if(?8 !='' ,bug_isfavorites =?8 ,1=1 )" +
"GROUP BY bug_name " +
"HAVING nun between ?5 and ?6 ",
nativeQuery = true,
countQuery = "select count(bug_name) " +
" from bug_data where 1=1 " +
"and if(?4 >0,bug_level in (?3) ,1=1 )" +
" and if(?2 >0,bug_type in (?1) ,1=1 ) " +
" and if(?7 !='',LOCATE(?7, bug_name)>0,1=1 )" +
" and if(?8 !='' ,bug_isfavorites =?8 ,1=1 )" +
"GROUP BY bug_name " +
"HAVING count(project_id) between ?5 and ?6 ")
public Page<Map<String, Object>> ASSS(
@Param("bugTypeList") List<String> bugTypeList,
@Param("bugTypeListSize") Integer bugTypeListSize,
@Param("bugLevelList") List<String> bugLevelList,
@Param("bugLevelListSize") Integer bugLevelListSize,
@Param("effectStart") Integer effectStart,
@Param("effectEnd") Integer effectEnd,
@Param("bugName") String bugName, @Param("bugIsCollection") String bugIsCollection,
Pageable pageable);
最後的代碼
/**
* @param bugTypeList
* @param bugTypeListSize 用於判斷bugLevelList是否是null
* @param bugLevelList
* @param bugLevelListSize 用於判斷bugLevelList是否是null
* @param effectStart having的開始
* @param effectEnd having的結束
* @param bugName
* @param bugIsCollection
* @param pageable
* @return
*/
@Query(value = "select bug_id , bug_category , bug_releasedate,bug_type ,bug_level ,bug_isfavorites ,bug_fav_time ,big_score , bug_name, bug_type_url , " +
"COUNT(project_id) bug_effect_count" +
" from bug_data where 1=1 " +
" and if(?10 !='' ,user_id =?10 ,1=1 )" +
" and if(?9 !='' ,module_id =?9 ,1=1 )" +
" and if(?4 >0 ,bug_level in (?3) ,1=1 )" +
" and if(?2 >0,bug_type in (?1) ,1=1 ) " +
" and if(?7 !='',LOCATE(?7, bug_name)>0,1=1 )" +
" and if(?8 !='' ,bug_isfavorites =?8 ,1=1 )" +
"GROUP BY bug_name " +
"HAVING bug_effect_count between ?5 and ?6 ",
nativeQuery = true)
public Page<Map<String, Object>> selectAllBugDongTai(
@Param("bugTypeList") List<String> bugTypeList,
@Param("bugTypeListSize") Integer bugTypeListSize,
@Param("bugLevelList") List<String> bugLevelList,
@Param("bugLevelListSize") Integer bugLevelListSize,
@Param("effectStart") Integer effectStart,
@Param("effectEnd") Integer effectEnd,
@Param("bugName") String bugName,
@Param("bugIsCollection") String bugIsCollection,
@Param("moduleId") String moduleId,
@Param("userId") String userId,
Pageable pageable);