ecimal格式的PassThroughFieldExtract

ecimal格式的PassThroughFieldExtract

本文介绍了Spring Batch:具有BigDecimal格式的PassThroughFieldExtractor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring Batch从具有混合列类型的数据库表中提取CSV文件.样本表SQL模式是

I'm using Spring Batch to extract a CSV file from a DB table which has a mix of column types. The sample table SQL schema is

[product] [varchar](16) NOT NULL,
[version] [varchar](16) NOT NULL,
[life_1_dob] [date] NOT NULL,
[first_itm_ratio] [decimal](9,6) NOT NULL,

"first_itm_ration"字段的示例数据库列值是

the sample Database column value for the 'first_itm_ration' field are

first_itm_ratio
1.050750
0.920000

但是我希望CSV删除值中的尾随零.

but I would like my CSV to drop the trailing zero's from values.

first_itm_ratio
1.05075
0.92

我不想不必为表中的每个特定字段定义格式,而是为该数据类型的所有列都具有全局对象特定的格式.

I'd prefer not to have to define the formatting for each specific field in the table, but rather have a global object specific formatting for all columns of that data type.

我的csvFileWriter bean

My csvFileWriter bean

<bean id="csvFileWriter"      class="org.springframework.batch.item.file.FlatFileItemWriter" scope="step">
    <property name="resource" ref="fileResource"/>
    <property name="lineAggregator">
        <bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
            <property name="delimiter">
                <util:constant static-field="org.springframework.batch.item.file.transform.DelimitedLineTokenizer.DELIMITER_COMMA"/>
            </property>
            <property name="fieldExtractor">
                <bean class="org.springframework.batch.item.file.transform.PassThroughFieldExtractor" />
            </property>
        </bean>
    </property>
</bean>

推荐答案

您可以

  1. 编写您自己的BigDecimalToStringConverter implements Converter<BigDecimal, String>以设置大十进制格式而不会尾随0'
  2. 创建一个新的ConversionService(MyConversionService)并注册到自定义转换器中
  3. 扩展DelimitedLineAggregator,注入MyConversionService,覆盖doAggregate()以使用注入的转换服务格式化字段
  1. Write your own BigDecimalToStringConverter implements Converter<BigDecimal, String> to format big decimal without trailing 0's
  2. Create a new ConversionService (MyConversionService) and register into the custom converter
  3. Extends DelimitedLineAggregator, inject MyConversionService, override doAggregate() to format fields using injected conversion service


public class MyConversionService extends DefaultConversionService {
  public MyConversionService() {
    super();
    addConverter(new BigDecimalToStringConverter());
  }
}


public class MyFieldLineAggregator<T> extends DelimitedLineAggregator<T> {
  private ConversionService cs = new MyConversionService();

  public String doAggregate(Object[] fields) {
    for(int i = 0;i < fields.length;i++) {
      final Object o = fields[i];
      if(cs.canConvert(o.getClass(), String.class)) {
        fields[i] = cs.convert(o, String.class);
      }
    }
    return super.doAggregate(fields);
  }
}

这篇关于Spring Batch:具有BigDecimal格式的PassThroughFieldExtractor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 02:54