![](/img/trans.png)
[英]How to make a Jpql query work in Spring Jpa Repository with Optional return?
[英]how to pass a optional param into spring controller and jpql query
這是我的存儲庫:
@Query( "SELECT new com.app.gsc.entities.ListeBI(lm.listeDeCriteres.designation,count(*) as totalNumber,lm.ficheDeMission.dateEtHeurDeDepart) from ListeDeControleDetailsMission lm where lm.boolDepart=false OR lm.ficheDeMission.codeDeFicheDeMission=:CodeMission GROUP BY lm.listeDeCriteres.designation,DATE(lm.ficheDeMission.dateEtHeurDeDepart)" )
List<ListeBI> getIncidentDepart( @Param( "CodeMission" ) Integer missId );
這是我的控制器:
@RequestMapping( value = "admin/dashboard/getIncidentDepart/{codeMission}", method = RequestMethod.GET )
public Reponse getIncidentDepart( HttpServletResponse response, @PathVariable Integer codeMission ) { // entêtes
CorsController.getIncidentDepart( response, codeMission );
if ( messages != null ) {
return new Reponse( -1, messages );
}
List<ListeBI> listeBIs = null;
try {
listeBIs = application.getIncidentDepart( codeMission );
} catch ( Exception e ) {
return new Reponse( 5, Static.getErreursForException( e ) );
}
return new Reponse( 0, Static.getMapForMissionIncidents( listeBIs ) );
}
我的問題是:這第一個問題我想將codeMission傳遞為選擇的變量,我不知道該如何放置,但是我會解釋一下,如果我在控制器中傳遞了codeMission變量,請求應該可以正常工作,如果我不傳遞codeMission控件中的變量請求也應該起作用,請問我該怎么做,而且我不確定請求中的條件是否正常:
其中lm.boolDepart = false或lm.ficheDeMission.codeDeFicheDeMission =:CodeMission
關於JPQL問題
這里 :
where lm.boolDepart=false OR lm.ficheDeMission.codeDeFicheDeMission=:CodeMission
如果您的:CodeMission
參數not null
值,則您的過濾器會按預期工作,但如果具有null
值,則它會根據:codeMission==null
條件進行過濾,這不是您想要的。
要解決這兩種情況( null :CodeMission
參數,而not null :CodeMission
參數),請嘗試:
where lm.boolDepart=false OR
(:CodeMission is null OR lm.ficheDeMission.codeDeFicheDeMission=:CodeMission)
具體來說:
如果用:CodeMission
100
填充:CodeMission
值,則結果為:
where lm.boolDepart=false OR
(lm.ficheDeMission.codeDeFicheDeMission=:100)
如果將:CodeMission
值填充為null
,則結果為:
where lm.boolDepart=false OR
(null is null)
關於Rest配置問題
如果您使用Spring 4和Java 8,則可以使codeMission
可選:
@RequestMapping( value = "admin/dashboard/getIncidentDepart/{codeMission}", method = RequestMethod.GET )
public Reponse getIncidentDepart( HttpServletResponse response, @PathVariable Optional<Integer> codeMission ) {
...
}
否則,創建具有兩個不同路徑的兩個方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.