如何为具体查询设置fetchSize?

时间:2019-03-23 20:54:09

标签: java spring jdbc spring-jdbc jdbctemplate

我只想为一个查询设置fetchSize,但找不到适合的API。

我的代码如下:

 <?xml version="1.0" encoding="utf-8"?>
   <RelativeLayout
 style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" 
    android:layout_width="match_parent"
   android:theme="@style/no_title"
  android:layout_height="match_parent"
  >
  <RelativeLayout
    android:id="@+id/real"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/main"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:fitsSystemWindows="true"
        android:orientation="vertical"
        tools:ignore="MissingPrefix"
        >
        <android.support.design.widget.AppBarLayout

            android:layout_width="match_parent"
            android:layout_height="170dp"
            android:fitsSystemWindows="true">

            <ImageView
                android:id="@+id/appbar"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:scaleType="centerCrop"
                android:src="@drawable/person_ic" />

        </android.support.design.widget.AppBarLayout>
        <android.support.design.widget.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:src="@android:drawable/ic_menu_camera"
            app:fabSize="mini"
            app:layout_anchor="@id/appbar"
            app:layout_anchorGravity="bottom|right|end" />
    </android.support.design.widget.CoordinatorLayout>
       </RelativeLayout>
       <ScrollView
        android:id="@+id/scroll_info"
          android:layout_width="match_parent"
         android:layout_height="match_parent"
        android:fillViewport="true" >
        <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="20dp"
        android:orientation="vertical">
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <android.support.design.widget.TextInputLayout
                android:id="@+id/USerNameLayout"  
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="240dp"
                android:textColorHint="#cccccc">
                <EditText
                    android:id="@+id/NameStudentRe"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:backgroundTint="#ffff"
                    android:clickable="true"
                    android:cursorVisible="false"
                    android:drawableLeft="@drawable/ic_person_black_24dp"
                    android:focusable="false"
                    android:focusableInTouchMode="false"
                    android:hint="   Name"
                    android:inputType="textPersonName"
                    android:padding="3dp"
                    android:text=" Sukaina Ahmed Al-Mutawakel"
                    android:textColor="#cccccc"
                    android:textSize="20sp" />
            </android.support.design.widget.TextInputLayout>
        </RelativeLayout>
             </LinearLayout>
         </ScrollView>
</RelativeLayout>

如何将fetchSize传递给查询?

1 个答案:

答案 0 :(得分:1)

可以使用PreparedStatementSetterJavaDoc
完成此操作 给定查询

select * from mytable where user_id > ?

您可以使用

jdbcTemplate.query(query, 
    new PreparedStatementSetter() {
       @Override
       public void setValues(final PreparedStatement ps) throws SQLException {
           ps.setInt(1, userId);
           ps.setFetchSize(500);
       }
    }, 
    new RowCallbackHandler() {
       @Override
       public void processRow(final ResultSet rs) throws SQLException {
         ...
       }
    });  

或更紧凑的形式

jdbcTemplate.query(
      query, 
      ps -> { 
         ps.setInt(1, userId);
         ps.setFetchSize(500);
      },
      rs -> { ... }
);

请记住

  

实施负责设置任何必要的参数。
  带有占位符的SQL已经提供。


对于单个结果对象,请使用

public <T> T query(String sql,
                   PreparedStatementSetter pss,
                   ResultSetExtractor<T> rse) throws DataAccessException;

方法。例如

jdbcTemplate.query(
    query,
    new ArgumentPreparedStatementSetter(new Object[] {balanceId}),
    rs -> {
       final String field1 = rs.getString("field1");
       // Get other fields and construct the resulting object
       return new YourClass(field1, ...);
    }
);