Springboot ResultSetExtractor vs RowCallbackHandler 跳过第一行

问题描述

我在我的 ProductDaoImpl 类中编写了以下方法。我的 springboot 应用程序使用 postgres 作为数据库来存储产品记录,其中包含 id、productname、price、数量等字段(它是简单的基本/演示 springboot,带有控制器和 dao 层,具有一些附加功能,但从控制器到 dao 层仅包含以下方法.) .我已经为 jar 添加了 pom.xml。

在某些论坛中,我了解到 RowCallbackHandler 适用于大型数据集,因此我将 RowCallbackHandler 与 ResultSetExtractor 的用法进行了比较。下面的“getProducts”方法同时使用了 RowCallbackHandler & ResultSetExtractorapproach 在同一个方法中实现。两者都有打印行语句来在迭代结果集时打印记录。我看到 ResultSetExtractor 正在打印所有记录,包括一个记录,而 RowCallbackHandler 跳过第一个记录。

我也只尝试了两条记录 & 我看到 RowCallbackHandler 每次都会跳过第一条记录,即使在干净构建和重新启动之后我也没有看到 ResultSetExtractor 有任何问题,有人可以帮助我理解问题或需要注意什么.

ProductDaoImpl

  @Override
  public void getProducts() {
    
    
    List list = new ArrayList();
    String sql = "select * from product";
    
    //ResultSetExtractor Implementation
       jdbcTemplate.query(sql,new ResultSetExtractor<List>(){
        
        public List extractData(
           ResultSet rs) throws sqlException,DataAccessException {
           
           while(rs.next()){  
             System.out.println("ResultSetExtractor ::" + rs.getString("productname"));
           }  
           return list;  
        }        
     });
    
    
    //RowCallbackHandler Implementation
       
    jdbcTemplate.query(sql,new RowCallbackHandler() {
      public void processRow(ResultSet rs) throws sqlException {
          while (rs.next()) {
            System.out.println("RowCallbackHandler ::" + rs.getString("productname"));
              
          }
      }
    });
 
  }

POM.XML

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.11.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>demo</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>demo</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>nz.net.ultraq.thymeleaf</groupId>
                    <artifactId>thymeleaf-layout-dialect</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- JPA-->
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        
        <!-- Security -->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!-- JWT -->
        
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.9.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


        <!-- Mail -->
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

        <!-- Devtools hotdeploy-->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        
        <!-- Freemarker -->
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

解决方法

RowCallbackHandler 被 Spring 重复调用,每行调用一次。

您不得在 RowCallbackHandler 中编写自己的行处理循环。在 Spring 的每次调用中,您只处理 rs 中的当前行。

jdbcTemplate.query(SQL,new RowCallbackHandler() {
  public void processRow(ResultSet rs) throws SQLException {
    System.out.println("RowCallbackHandler ::" + rs.getString("productname"));
  }