Spring Batch、JpaRepository、RepositoryItemReader - 如何限制阅读器中的整个结果集?

问题描述 投票:0回答:1

我是 springBatch 的新手,我正在研究如何进行 SQL 查询限制。 在读取过程中,我从数据库条目中获取我的结果集。我尝试使用 setMaxItemCount 来限制行数,但没有成功。它仍在读取所有数据库条目。

public class PeopleReader extends RepositoryItemReader<Product>  {

        public People read () throws Exception {
            DataSourceContextHolder.set("people");
            return this.doRead();
        }

    public PeopleReader(final PeopleRepository peopleRepository) {
        DataSourceContextHolder.set("people");
        this.setRepository(peopleRepository);
        this.setMaxItemCount(100);
        this.setSort(new HashMap<>());
        this.setPageSize(100);
    } 
}
@Repository
public interface PeopleRepository extends JpaRepository<People, String> {
}

如何将数据源的整个结果集限制为 100(例如 SQL 查询

Limit 100
)?

spring spring-batch limit jparepository
1个回答
0
投票

最后,为了限制阅读器中的整个结果集,我添加了一个计数器

public class PeopleReader extends RepositoryItemPeople<People>  {

    private int counter = 0;

    public People read () throws Exception {
        DataSourceContextHolder.set("people");
        if(counter<100)
        {   counter++;
            return this.doRead();
        }
        else
           return null;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.