如何在spring batch xstream中读取自定义xml标记读取器
我是新来的。我必须使用spring批处理过程将XML解析到数据库中,如果定义的dto与XML标记具有相同的值,它就可以正常工作。但如果我有一个dto中没有定义的附加标记(我应该跳过它),它就不起作用了 batchconfiguration.java:如何在spring batch xstream中读取自定义xml标记读取器,xml,spring,spring-batch,xmlreader,xstream,Xml,Spring,Spring Batch,Xmlreader,Xstream,我是新来的。我必须使用spring批处理过程将XML解析到数据库中,如果定义的dto与XML标记具有相同的值,它就可以正常工作。但如果我有一个dto中没有定义的附加标记(我应该跳过它),它就不起作用了 batchconfiguration.java: import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.HashMap; import java.util.Map; impo
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.database.ItemPreparedStatementSetter;
import org.springframework.batch.item.database.JdbcBatchItemWriter;
import org.springframework.batch.item.xml.StaxEventItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.oxm.xstream.XStreamMarshaller;
@Configuration
@EnableBatchProcessing
public class BatchConfiguration {
@Autowired
public JobBuilderFactory jobBuilderFactory;
@Autowired
public StepBuilderFactory stepBuilderFactory;
@Autowired
public DataSource dataSource;
@Bean
public DataSource dataSource() {
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost/springbatch");
dataSource.setUsername("root");
dataSource.setPassword("admin");
return dataSource;
}
@Bean
public StaxEventItemReader<User> reader() {
StaxEventItemReader<User> reader = new StaxEventItemReader<User>();
reader.setResource(new ClassPathResource("user.xml"));
reader.setFragmentRootElementName("user");
Map<String, String> aliases = new HashMap<String, String>();
aliases.put("user", "com.example.demo.User");
XStreamMarshaller xStreamMarshaller = new XStreamMarshaller();
xStreamMarshaller.setAliases(aliases);
reader.setUnmarshaller(xStreamMarshaller);
return reader;
}
@Bean
public JdbcBatchItemWriter<User> writer() {
JdbcBatchItemWriter<User> writer = new JdbcBatchItemWriter<User>();
writer.setDataSource(dataSource);
writer.setSql("insert into user(id,name) values(?, ?)");
writer.setItemPreparedStatementSetter(new UserItemPreparedStmSetter());
return writer;
}
private class UserItemPreparedStmSetter implements ItemPreparedStatementSetter<User> {
@Override
public void setValues(User user, PreparedStatement ps) throws SQLException {
ps.setInt(1, user.getId());
ps.setString(2, user.getName());
}
}
@Bean
public Step step1() {
return stepBuilderFactory.get("step1").<User, User>chunk(10).reader(reader()).writer(writer()).build();
}
@Bean
public Job importUserJob() {
return jobBuilderFactory.get("importUserJob").incrementer(new RunIdIncrementer()).flow(step1()).end().build();
}
Spring批处理依赖于Spring OXM来处理/解组XML内容。在您的例子中,
statxeventitemreader
与org.springframework.oxm.Unmarshaller
一起工作。因此,这实际上不是一个Spring批处理问题,而是一个XML解组问题(特别与xstream相关,因为您在实现时使用它)。这可能会有帮助:谢谢,我会试试的
package com.example.demo;
public class User {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}