如何将可选参数传递给spring控制器和jpql查询

时间:2016-11-01 10:21:19

标签: java spring-mvc spring-data

这是我的存储库:

@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

1 个答案:

答案 0 :(得分:0)

关于JPQL问题

这里:

where lm.boolDepart=false OR lm.ficheDeMission.codeDeFicheDeMission=:CodeMission


如果您的:CodeMission参数值为not null,则您的过滤符会按预期运行,但如果值为null,则会过滤:codeMission==null条件并且不是你想要的。

要解决这两种情况(null :CodeMission param和not null :CodeMission param),请尝试:

where lm.boolDepart=false OR 
      (:CodeMission is null OR lm.ficheDeMission.codeDeFicheDeMission=:CodeMission)

具体地说:

如果100填充:CodeMission作为where lm.boolDepart=false OR (lm.ficheDeMission.codeDeFicheDeMission=:100) 的值,则会产生:

null

如果:CodeMission的值为where lm.boolDepart=false OR (null is null) ,则会产生以下结果:

codeMission

关于Rest配置问题

如果您使用Spring 4和Java 8,则可以@RequestMapping( value = "admin/dashboard/getIncidentDepart/{codeMission}", method = RequestMethod.GET ) public Reponse getIncidentDepart( HttpServletResponse response, @PathVariable Optional<Integer> codeMission ) { ... } 可选:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="192dp"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">

            <ImageView
                android:id="@+id/header"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/sport"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/anim_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/background"
        android:clipToPadding="false"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/background"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/title_lay"
                android:layout_width="match_parent"
                android:layout_height="72dp"
                android:background="@color/background"
                android:gravity="center_vertical"
                android:orientation="horizontal"
                android:paddingLeft="30dp"
                android:paddingRight="30dp">

                <FrameLayout
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:layout_marginRight="16dp">

                    <ImageView
                        android:id="@+id/iv_icon"
                        android:layout_width="40dp"
                        android:layout_height="40dp" />

                </FrameLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="center_vertical"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/club_title"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center_vertical"
                        android:textSize="16sp"
                        android:textColor="@color/second_text" />

                    <TextView
                        android:id="@+id/club_description"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center_vertical"
                        android:textColor="@color/text_color" />
                </LinearLayout>


            </LinearLayout>

            <LinearLayout
                android:id="@+id/subscription_lay"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_below="@id/title_lay"
                android:layout_marginBottom="8dp"
                android:background="@drawable/backgroundtoolbar"
                android:clickable="true"
                android:gravity="center_vertical"
                android:orientation="horizontal">

                <ImageView
                    android:layout_width="24dp"
                    android:layout_height="24dp" />

                <TextView
                    android:id="@+id/subscription_txt"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/str_subscribe_on"
                    android:textSize="16sp" />

            </LinearLayout>

            <android.support.v7.widget.CardView
                android:id="@+id/address_card_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:background="@color/second_background"
                android:layout_below="@id/subscription_lay"
                app:cardCornerRadius="4dp"
                >

                <RelativeLayout
                    android:id="@+id/address_lay"
                    android:layout_width="match_parent"
                    android:layout_height="48dp"
                    android:background="@color/second_background"
                    android:gravity="center_vertical"
                    android:paddingLeft="16dp"
                    android:paddingRight="16dp">

                    <TextView
                        android:id="@+id/club_address"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text=""
                        android:textColor="@color/second_text" />

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:src="@drawable/ic_maps_place" />
                </RelativeLayout>

            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                android:id="@+id/schedule_card_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:background="@color/second_background"
                android:layout_below="@id/address_card_view"
                app:cardCornerRadius="4dp"
                >

                <RelativeLayout
                    android:id="@+id/schedule_lay"
                    android:layout_width="match_parent"
                    android:layout_height="64dp"
                    android:background="@color/second_background"
                    android:gravity="center_vertical"
                    android:paddingLeft="16dp"
                    android:paddingRight="16dp">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:gravity="center_vertical"
                        android:orientation="vertical">

                        <TextView
                            android:id="@+id/club_schedule"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/schedule_title"
                            android:gravity="center_vertical"
                            android:textColor="@color/text_color" />

                        <TextView
                            android:id="@+id/schedule_value"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text=""
                            android:gravity="center_vertical"
                            android:textColor="@color/second_text" />
                    </LinearLayout>

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:layout_centerVertical="true"
                        android:src="@drawable/ic_notification_event_note" />
                </RelativeLayout>

            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                android:id="@+id/contact_card_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:background="@color/second_background"
                android:layout_below="@id/schedule_card_view"
                app:cardCornerRadius="4dp"
                >

                <RelativeLayout
                    android:id="@+id/contact_lay"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/second_background"
                    android:gravity="center_vertical"
                    android:paddingLeft="16dp"
                    android:paddingRight="16dp">

                    <RelativeLayout
                        android:id="@+id/phone_lay"
                        android:layout_width="match_parent"
                        android:gravity="center_vertical"
                        android:layout_height="64dp">

                        <LinearLayout

                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:gravity="center_vertical"
                            android:orientation="vertical">

                            <TextView
                                android:id="@+id/phone_title"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text=""
                                android:gravity="center_vertical"
                                android:textColor="@color/text_color" />

                            <TextView
                                android:id="@+id/phone_value"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text=""
                                android:gravity="center_vertical"
                                android:textColor="@color/second_text" />
                        </LinearLayout>

                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentRight="true"
                            android:layout_centerVertical="true"
                            android:src="@drawable/ic_maps_local_phone" />
                    </RelativeLayout>

                    <RelativeLayout
                        android:id="@+id/web_lay"
                        android:layout_width="match_parent"
                        android:layout_height="64dp"
                        android:gravity="center_vertical"
                        android:layout_below="@id/phone_lay">

                        <LinearLayout

                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:gravity="center_vertical"
                            android:orientation="vertical">

                            <TextView
                                android:id="@+id/web_title"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:gravity="center_vertical"
                                android:text=""
                                android:textColor="@color/text_color" />

                            <TextView
                                android:id="@+id/web_value"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:gravity="center_vertical"
                                android:text=""
                                android:textColor="@color/second_text" />
                        </LinearLayout>

                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentRight="true"
                            android:layout_centerVertical="true"
                            android:src="@drawable/ic_action_language" />
                    </RelativeLayout>

                    <RelativeLayout
                        android:id="@+id/mail_lay"
                        android:layout_width="match_parent"
                        android:layout_height="64dp"
                        android:gravity="center_vertical"
                        android:background="@color/second_background"
                        android:layout_below="@id/web_lay">

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:gravity="center_vertical"
                            android:orientation="vertical">

                            <TextView
                                android:id="@+id/mail_title"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text=""
                                android:gravity="center_vertical"
                                android:textColor="@color/text_color" />

                            <TextView
                                android:id="@+id/mail_value"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="12-3123-123-34"
                                android:gravity="center_vertical"
                                android:textColor="@color/second_text" />
                        </LinearLayout>

                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentRight="true"
                            android:layout_centerVertical="true"
                            android:src="@drawable/ic_content_send" />
                    </RelativeLayout>

                    <View
                        android:id="@+id/devider"
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:layout_below="@id/mail_lay"
                        android:paddingLeft="-30dp"
                        android:paddingRight="-30dp"
                        android:background="@color/second_background" />

                    <RelativeLayout
                        android:id="@+id/other_lay"
                        android:layout_width="match_parent"
                        android:layout_height="48dp"
                        android:gravity="center_vertical"
                        android:layout_below="@id/devider">

                        <TextView
                            android:id="@+id/club_contacts"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/detail"
                            android:layout_centerVertical="true"
                            android:textColor="@color/text_color" />

                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentRight="true"
                            android:layout_centerVertical="true"
                            android:src="@drawable/ic_hardware_keyboard_arrow_right" />
                    </RelativeLayout>

                </RelativeLayout>

            </android.support.v7.widget.CardView>
        </RelativeLayout>

    </android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>

否则,使用两个不同的路径创建两个方法。