0
  1. 我必须从数据库表中读取数据并将其转换为 XML 文件。

  2. 此外,需要转换为 XML 的字段(表的列)应该是配置驱动的(属性文件或配置表)。

我已经使用 Spring Batach(Spring Boot App) 完成了第一部分。请提供指导以完成第二部分。

<code>
    @Bean("dbToXmlJob")
    public Job dbToXmlJob(@Qualifier("dbToXmlStep") Step step) throws Exception {
        return this.jobBuilderFactory
            .get(Constants.JOB_NAME_DB_TO_XML)
            //.validator(dbToXmlJobValidator())
            .start(step)
            .build();
    }

    @Bean("dbToXmlStep")
    public Step dbToXmlStep(@Qualifier("jpaReader") ItemReader<PatientEntity> itemReader,
                     @Qualifier("dbToXmlProcessor")Function<PatientEntity, PatientRecord> processor,
                     @Qualifier("xmlWriter")ItemWriter<PatientRecord> writer) throws Exception {
        return this.stepBuilderFactory
            .get(Constants.STEP_NAME_DB_TO_XML)
            .<PatientEntity, PatientRecord>chunk(2)
            .reader(itemReader)
            .processor(processor)
            .writer(writer)
            .build();
    }

    @Bean("jpaReader")
    @StepScope
    public JpaPagingItemReader<PatientEntity> dbReader() throws Exception {
        String jpqlQuery = "SELECT p from PatientEntity p";

        JpaPagingItemReader<PatientEntity> reader = new JpaPagingItemReader<>();
        reader.setQueryString(jpqlQuery);
        reader.setEntityManagerFactory(batchEntityManagerFactory);
        reader.setPageSize(100);
        reader.afterPropertiesSet();
        reader.setSaveState(true);

        return reader;
    }

    @Bean("dbToXmlProcessor")
    @StepScope
    public Function<PatientEntity, PatientRecord> xmlProcessor() {
        return (patientRecord) ->  {
            return new PatientRecord(
                patientRecord.getSourceId(),
                patientRecord.getFirstName(),
                patientRecord.getMiddleInitial(),
                patientRecord.getLastName(),
                patientRecord.getEmailAddress(),
                patientRecord.getPhoneNumber(),
                patientRecord.getStreet(),
                patientRecord.getCity(),
                patientRecord.getState(),
                patientRecord.getZipCode(),
                patientRecord.getBirthDate().toString(),
                patientRecord.getSocialSecurityNumber());
        };
    }

    @Bean("xmlWriter")
    @StepScope
    public StaxEventItemWriter<PatientRecord> xmlWriter() {

        StaxEventItemWriter<PatientRecord> xmlFileWriter = new StaxEventItemWriter<>();

        String exportFilePath = "file:xml/patients.xml";
        Path file = Paths.get(applicationProperties.getBatch().getInputPath() + 
                File.separator + "output.xml");
        xmlFileWriter.setResource(new FileSystemResource(file));
        xmlFileWriter.setRootTagName("employees");

        Jaxb2Marshaller empMarshaller = new Jaxb2Marshaller();
        empMarshaller.setClassesToBeBound(PatientRecord.class);
        xmlFileWriter.setMarshaller(empMarshaller);
        System.out.println("marshalling");;
        return xmlFileWriter;

    }
</code>

谢谢, 巴拉尚达尔

4

1 回答 1

0

请提供指导以完成第二部分。

  1. 此外,需要转换为 XML 的字段(表的列)应该是配置驱动的(属性文件或配置表)。

您可以将所需的列放在属性文件中,例如application.properties

columns=column1,column5,column42

并使用它们配置您的阅读器:

@Configuration
@EnableBatchProcessing
@PropertySource("classpath:application.properties")
class MyConfigClass {

   @StepScope
   public JpaPagingItemReader<PatientEntity> dbReader(@Value("${columns}") String columns) throws Exception {
      String[] columnsArray = columns.split(",");
      // use columns to dynamically create your query
   }

   // the rest of your config

}
于 2020-04-27T10:38:20.150 回答