【问题标题】:read database in spring batch based on reading List<Hashmap<String,String>>基于读取 List<Hashmap<String,String>> 在 Spring Batch 中读取数据库
【发布时间】:2018-02-04 01:35:52
【问题描述】:

我有一个场景,我需要从包含 hashmap 的列表中读取数据。通过各种论坛阅读后,我能够理解甚至使用 jdbccursor 项目阅读器通过标准查询来读取数据库。但不确定如何从上一步中检索此 hashmap 列表以动态创建查询

下面是我当前运行硬编码查询的代码,而不是我想接收哈希图列表并创建选择查询然后运行它

    @StepScope
    private JdbcCursorItemReader<List<HashMap<String, String>>> checkDatExist(@Value("#{jobParameters['tableName']}") String tableName) {


    JdbcCursorItemReader<List<HashMap<String, String>>> getExcelData = new JdbcCursorItemReader<List<HashMap<String, String>>>();
    getExcelData.setDataSource(dataSource);
    getExcelData.setSql("SELECT id from "+tableName);
    getExcelData.setRowMapper(new dbMapper());
    return getExcelData;

}

大家有什么意见

【问题讨论】:

    标签: spring spring-batch


    【解决方案1】:

    你可以把这个map放到上一步的ExecutionContext中,在你的Reader中读取。

    @Component
    @JobScope
    class PreviousStep {
    
       @Value("#{jobExecution.executionContext}")
       private ExecutionContext executionContext;
    
       public void someFunction() {
         this.executionContext.put("someVariable", mapYouWantToStore);
       }
    }
    

    你必须在 You Reader 课上做同样的事情。只需按照上面的 Autowire ExecutionContext 进行操作,然后执行“someVariable”。

    this.executionContext.get("someVariable");
    

    这将为您提供您在上一步中存储的地图。

    【讨论】:

    • 谢谢,我尝试过类似的东西,但仍然缺少一些东西。在我以前的作家中,我写过类似
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-15
    • 2021-07-17
    • 1970-01-01
    • 2018-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多