2016-08-19 65 views
0
@Repository 
public interface WANRepository extends JpaRepository<WANConfiguration,Long>{ 
    @Query("select ads from WANConfiguration as ads where ?1 BETWEEN ads.minAge AND ads.maxAge AND ads.expiryTime >?2 and ads.region=?3 and ads.gender=?4") 
    List<WANConfiguration> findByAllAdsForUser(Integer userAge,Date currentTime,String region, String gender); 
} 

這裏是調用代碼春天JPA庫:如何發送默認值參數,如果標準不匹配

List<WANConfiguration> list = fanRepository.findByAllAdsForUser(userAgeInYear, new Date(),userLocation.getRegionName(), user.getGender().name()); 

現在還有就是我想通過默認區域的情況下,即,用戶位置.getRegionName(),那麼我該如何做以及在數據庫和存儲庫或查詢中需要做哪些更改?

+0

你能清楚你問的是什麼?你想在'userLocation.getRegionName()'爲空/空等時傳遞默認區域? – ByeBye

+0

是的確實...我想通過一個默認區域,當它爲空或空 –

回答

0

您可以創建上述方法層:

List<WANConfiguration> findByAllAdsForUser(Integer userAge,Date currentTime,String region, String gender) { 
    if(region == null || region.isEmpty()) { 
     String defaultRegion = "what you want"; 
     return findByAllAdsForUser(userAge, currentTime, defaultRegion, gender); 
    } 
    return findByAllAdsForUser(userAge, currentTime, region, gender); 
} 
+0

做了相同的結果。其實我是JPA和Spring的新手,我想到了一些不同的東西,這就是爲什麼我有點困惑... Thanx ... @ByeBye –

+0

這是很好的方法來做到這一點。如果我幫忙請接受我的回答。 – ByeBye