springbatch自学之路-11(初识ItemReader)

目录

1.启动类

2.Job配置类


1.启动类

package com.springbatch._09item_reader;

import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * SpringBatchDemo项目启动类
 *
 * @Package: PACKAGE_NAME
 * @ClassName: com.springbatch._01helloworld.SpringBatchConfig
 * @author: zq
 * @since: 2020/5/22 20:31
 * @version: 1.0
 * @Copyright: 2020 zq. All rights reserved.
 */
@SpringBootApplication
@EnableBatchProcessing
public class SpringBatchConfig {

    public static void main(String[] args) {

        SpringApplication.run(SpringBatchConfig.class, args);

    }

}

2.Job配置类

package com.springbatch._09item_reader;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameter;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Configuration
public class ItemReaderJobConfig {

    //创建 创建job的对象
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    //创建 创建step的对象
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    Map<String, JobParameter> parameterMap;

    @Bean
    public Job itemReaderJob() {

        return jobBuilderFactory.get("itemReaderJob-01")
                .start(itemReaderJobStep())
                .build();

    }

    @Bean
    public Step itemReaderJobStep() {
        return stepBuilderFactory.get("itemReaderJobStep-01")
                .<String, String>chunk(2)
                .reader(myReaderDemo())
                .writer(list -> {

                    System.out.println("write=================");
                    for (String item : list) {

                        System.out.println(item + "----");

                    }

                })
                .build();
    }

    @Bean
    public MyItemReader myReaderDemo() {

        List<String> list = Arrays.asList("1q", "az", "2w", "sx");
        return new MyItemReader(list);

    }

}

reader

package com.springbatch._09item_reader;

import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.NonTransientResourceException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;

import java.util.Iterator;
import java.util.List;

/**
 * @Package: com.springbatch._09item_reader
 * @ClassName: MyItemReader
 * @author: zq
 * @since: 2020/6/21 21:10
 * @version: 1.0
 * @Copyright: 2020 zq. All rights reserved.
 */
public class MyItemReader implements ItemReader<String> {

    private final Iterator<String> iterater;

    public MyItemReader(List<String> list) {

        this.iterater = list.iterator();

    }

    @Override
    public String read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {

        if (iterater.hasNext()) {

            return iterater.next();

        } else {
            return null;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值