我必须从数据库表中读取数据并将其转换为 XML 文件。
此外,需要转换为 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>
谢谢, 巴拉尚达尔