@Bean
public JdbcBatchItemWriter<MyObject[]> jdbcBatchItemWriter(){
JdbcBatchItemWriter<MyObject[]> itemWriter = new
JdbcBatchItemWriter<MyObject[]>();
itemWriter.setSql("INSERT INTO ITEM (ID, DESCRIPTION) VALUES
(?, ?)");
itemWriter.setDataSource(dataSource);
itemWriter.setItemSqlParameterSourceProvider(new
BeanPropertyItemSqlParameterSourceProvider<MyObject[]>());
return itemWriter;
}
我有一个称为MyObject
的对象数组,我想将其插入数据库。
MyObject
的每个下标代表从数据库读取一行的列。
例如,MyObject [0]将是此实例的ID
列,MyObject[1]
将是DESCRIPTION
列。
我希望能够传递数组对象的下标,以在我的数据库中插入值。像这样
itemWriter.setSql("INSERT INTO ITEM (ID, DESCRIPTION) VALUES (0, 1)");
我应该如何批准?
查看这篇奇妙的文章https://www.petrikainulainen.net/programming/spring-framework/spring-batch-tutorial-writing-writing-information-to-a-a-database-with-with-with-with-with-with--with-with--JDBC/
尝试使用命名参数。在filliong参数时,给出类似以下值的值:
final class MyObjectPreparedStatementSetter implements ItemPreparedStatementSetter<MyObject> {
@Override
public void setValues(Myobject myObject,
PreparedStatement preparedStatement) throws SQLException {
preparedStatement.setString(1, myObject[0]);
preparedStatement.setString(2, myObject[1]);
preparedStatement.setString(3, myObject[2]);
}
}