JPA多属性排序以及JPAwhere多条件动态查询

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

你可能感兴趣的:(JPA,Hibernate)