[英]how to pass a optional param into spring controller and jpql query
this is my repository: 这是我的存储库:
@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 );
and heres my controller : 这是我的控制器:
@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 ) );
}
My problems are: this first problem i want to pass the codeMission as choosen variable, i dont how i can put that but i will explain, if i passe the codeMission variable in the controller the request should work, and if i dont passe the codeMission variable in the controlle the request also should work, how i can do that please, and also i'm not sure if the condition in the request works level is fine: 我的问题是:这第一个问题我想将codeMission传递为选择的变量,我不知道该如何放置,但是我会解释一下,如果我在控制器中传递了codeMission变量,请求应该可以正常工作,如果我不传递codeMission控件中的变量请求也应该起作用,请问我该怎么做,而且我不确定请求中的条件是否正常:
where lm.boolDepart=false OR lm.ficheDeMission.codeDeFicheDeMission=:CodeMission
其中lm.boolDepart = false或lm.ficheDeMission.codeDeFicheDeMission =:CodeMission
About JPQL question 关于JPQL问题
Here : 这里 :
where lm.boolDepart=false OR lm.ficheDeMission.codeDeFicheDeMission=:CodeMission
If your :CodeMission
param is not null
valued, your filter works as expected but if it has null
value, it filters on :codeMission==null
condition and it is not what you wish. 如果您的
:CodeMission
参数not null
值,则您的过滤器会按预期工作,但如果具有null
值,则它会根据:codeMission==null
条件进行过滤,这不是您想要的。
To address both cases ( null :CodeMission
param and not null :CodeMission
param) , try that : 要解决这两种情况(
null :CodeMission
参数,而not null :CodeMission
参数),请尝试:
where lm.boolDepart=false OR
(:CodeMission is null OR lm.ficheDeMission.codeDeFicheDeMission=:CodeMission)
Concretely : 具体来说:
If it is filled with for example 100
as value for :CodeMission
, it results in : 如果用
:CodeMission
100
填充:CodeMission
值,则结果为:
where lm.boolDepart=false OR
(lm.ficheDeMission.codeDeFicheDeMission=:100)
If it is filled with null
as value for :CodeMission
, it results in : 如果将
:CodeMission
值填充为null
,则结果为:
where lm.boolDepart=false OR
(null is null)
About Rest configuration question 关于Rest配置问题
If you use Spring 4 and Java 8, you can make codeMission
optional : 如果您使用Spring 4和Java 8,则可以使
codeMission
可选:
@RequestMapping( value = "admin/dashboard/getIncidentDepart/{codeMission}", method = RequestMethod.GET )
public Reponse getIncidentDepart( HttpServletResponse response, @PathVariable Optional<Integer> codeMission ) {
...
}
Otherwise, create two methods with two distinct paths. 否则,创建具有两个不同路径的两个方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.