spring批处理flatfileitemwriter-在写入后在csv中显示指数值

f0ofjuux  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(360)

我有一个spring批处理应用程序,它从mysql数据库读取数据并将其写入csv文件。
问题:当我看到csv中的数据时,一些大的数字显示为指数值。例如,对于这个数字“4491611100277480”,它显示为“4.49161e+15”
下面是我正在使用的代码片段

// Job
@Bean
public Job databaseToCsvJob() {
    return jobBuilderFactory.get("databaseToCsvJob").incrementer(new RunIdIncrementer())
            // .listener(listener)
            .start(databaseToCsvJob_step()).build();
}

// Step
@Bean
public Step databaseToCsvJob_step() {
    return stepBuilderFactory.get("databaseToCsvJob_step").<Person, Person>chunk(1000).reader(personReader())
            .writer(personWriter()).build();
}

// Reader
@Bean
public JdbcCursorItemReader<Person> personReader() {
    JdbcCursorItemReader<Person> reader = new JdbcCursorItemReader<>();
    reader.setSql("select empCode , empName , profileName, ID from profile");
    reader.setDataSource(mysqlDataSource);
    reader.setRowMapper(personRowmapper());
    return reader;
}

// Writer
@Bean
public FlatFileItemWriter<Person> personWriter() {
    // Create writer instance
    FlatFileItemWriter<Person> writer = new FlatFileItemWriter<>();

    // Set output file location
    writer.setResource(new FileSystemResource("src/main/resources/people-data.csv"));

    // All job repetitions should "append" to same output file
    writer.setAppendAllowed(true);

    // Name field values sequence based on object properties
    writer.setLineAggregator(new DelimitedLineAggregator<Person>() {
        {
            setDelimiter(",");
            setFieldExtractor(new BeanWrapperFieldExtractor<Person>() {
                {
                    setNames(new String[] { "empCode", "empName", "profileName", "id" });
                }
            });
        }
    });
    return writer;
}
l2osamch

l2osamch1#

当使用double作为数据类型时会发生这种情况。把它改成long,它应该可以正常工作。
如果出于某种原因必须保持双精度,则可以在提取期间转换值:

new BeanWrapperFieldExtractor<Person>() {
  {
    setNames(new String[] { "empCode", "empName", "profileName", "id" });
  }
  public Object[] extract(Person person) {
    return new Object[] { person.getEmpCode(), person.getEmpName(), person.getProfileName(), ((long) person.getId()) };
  }
}

相关问题