亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 FlatFileItemReader 讀取元素列表

使用 FlatFileItemReader 讀取元素列表

慕桂英546537 2023-06-21 16:08:25
比方說,我有一個對象Engine,唯一的屬性是brandName,然后我有一個 CSV 文件如下:car_brand; bike_brand; airplane_brand; boat_brandbrand1;    brand2;     brand3;         brand4brand5;    brand6;     brand7;         brand8brand9;    brand10;    brand11;        brand12我想要做的是讀取 CSV 文件并為每一行創建一個列表。由于我的項目是Spring批處理項目,我想使用Reader,但是怎么實現呢?我嘗試這樣做:@Beanpublic FlatFileItemReader<Engine> reader() {    FlatFileItemReader<Engine> reader = new FlatFileItemReader<Project>();    reader.setResource(new ClassPathResource("file.csv"));    reader.setLinesToSkip(1);    reader.setLineMapper(new DefaultLineMapper<Project>() {        {            setLineTokenizer(new DelimitedLineTokenizer() {                {                    setNames(***);                }            });            setFieldSetMapper(new BeanWrapperFieldSetMapper<Project>() {                {                    setTargetType(Engine.class);                }            });        }    });    return reader;}通常你只用閱讀器創建一個對象,我如何用閱讀器創建列表?我應該將方法類型更改為<List<Engine>>嗎?編輯: 我的問題不是如何制作列表的 Reader,而是如何制作列表的 FlatFileItemReader,重復的問題不是我需要的答案。
查看完整描述

1 回答

?
DIEA

TA貢獻1820條經驗 獲得超2個贊

您可以嘗試以下操作:


@Bean

LineMapper<List<Engine>> lineMapper() {

    return new LineMapper<List<Engine>>() {

        @Override

        public <List<Engine>> mapLine(String line, int lineNum) throws Exception {

            String[] tokens = line.split(";");

            if (tokens.length < 1) {

                throw new DataIntegrityViolationException("Expecting at least one token in input line: " + line);

            }

            List<Engine> data = new ArrayList<Engine>;

            for (String token : tokens) {

                data.add(Engine.of(token));

            }

            return data;

        }

    };

}

....


FlatFileItemReader<List<Engine>> itemReader = new FlatFileItemReader<>();

        itemReader.setLineMapper(lineMapper);


查看完整回答
反對 回復 2023-06-21
  • 1 回答
  • 0 關注
  • 202 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號