简体   繁体   English

Spring Batch 如何编写 Map

[英]Spring Batch how to write a Map

i've already seen a lot of tutorials and spring material but i have found only examples that contains a simple object in the ItemWriter.我已经看过很多教程和弹簧材料,但我只找到了在 ItemWriter 中包含一个简单对象的示例。

For example...例如...

    @Bean
    JdbcBatchItemWriter<Student> writer = new JdbcBatchItemWriter<>();
    writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Student>());
    writer.setItemPreparedStatementSetter(new ItemPreparedStatementSetter<Student>(){
        @Override
        public void setValues(Student student, PreparedStatement ps) throws SQLException {
            ps.setLong(1,student.getStudentCode());
            ps.setObject(2,student.getStudentGrade());
        }});
    writer.setSql(QUERY_STUDENT_GRADE_INSERT);
    writer.setDataSource(dataSource);
    return writer;  

in my actual scenario i must write a Map that contains a key and a value like:在我的实际场景中,我必须编写一个包含键和值的 Map,例如:

Map<KeyObject,Long>

But i have no idea how to code something that be able to insert all the content that i've received in my Map.但我不知道如何编写能够插入我在地图中收到的所有内容的代码。

There's another question...还有一个问题...

When i process certain number of items i need to store this data in the database, but the previous content stay in my collection, that way... how can i clean the map content after write ?当我处理一定数量的项目时,我需要将此数据存储在数据库中,但以前的内容仍保留在我的集合中,这样……我如何在写入后清理地图内容?

The solution was replace my Map for a List解决方案是将我的地图替换为列表

I'm just using lambda to filter the objects in the list and writing them through the common itemWriter.我只是使用 lambda 过滤列表中的对象并通过公共 itemWriter 写入它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM