2021年12月2日 星期四

模糊查詢遇到null情況的小坑


String medicalId = pbSearch.getMedicalId()==""?null:pbSearch.getMedicalId(); 		
String identityId = pbSearch.getIdentityId()==""?null:pbSearch.getIdentityId();
String fullname = pbSearch.getFullname()==""?"":pbSearch.getFullname();
String mode = pbSearch.getMode();
PageRequest pageRequest =  size == 0 ? null:  PageRequest.of(--page, size, Sort.by("sysTime").descending());	
PagepatientBasicList = patientBasicRepo.findVoByDomainAndMedicalIdOrIdentityIdOrFullnameOrMode(domain
				,medicalId, identityId, fullname,mode,pageRequest);
                
@Query("select new com.ICU.ICUmodule.vo.caseManage.PatientBasicVo(pb.id as id, pb.medicalId as medicalId, pb.identityId as identityId, pb.fullname as fullname, "
+ "pb.telephone as telephone, pb.localDate as localDate, pb.treatment as treatment, pr.mode as mode) "
+ "from patient_basic pb left join patient_returned pr on pb.id=pr.patientBasic.id "
+ "where pb.domain=:doamin and (:medicalId is null or pb.medicalId =:medicalId) and (:identityId is null or pb.identityId =:identityId) and (:fullname is '' or pb.fullname LIKE '%' || :fullname||'%') and (:mode is null or pr.mode =:mode)")
Page findVoByDomainAndMedicalIdOrIdentityIdOrFullnameOrMode(String doamin, String medicalId,
			String identityId, String fullname, String mode, Pageable pageRequest);

在一般情況下, 若是要判斷是否為 null 再拉進JPQL裡面做is null or object.xxx = :xxx

然而在模糊查詢的時候, 這裡若為 null 帶入到JPQL LIKE語法, 會產生判斷JPQL語法錯誤問題

因此在這裡LIKE語法是不能帶null, 而要給""字串來做判斷, 這樣JPQL就不會run出問題, 也可以進行模糊查詢及一般查詢

沒有留言:

張貼留言

題目: 瞬間有100-200萬筆資料量進來, 如何最快安全的接收?

如果想到快, 可能直覺就是Redis, 但是... 問:瞬間有100-200萬筆資料量進來, 如果存到redis, 這樣操作有沒有可能壓垮redis? 答: 在瞬間大量資料(如100-200萬筆)快速進入Redis,確實有可能導致Redis的性能瓶頸甚至崩潰,特別是在以下情況下:...