问题
I'm using spring batch with java annotation.
This batch is required read file, process and insert into table.
Basically I have done itemReader
, itemProcessor
and userItemWriter
with commit-interval
equal to 1.
My question is:
- How to insert PK of
User
table toUserDetail
table?
@Bean
public UserItemWriter userItemWriter() {
UserItemWriter itemWriter = new UserItemWriter();
List<ItemWriter<? super UserBO>> delegates = new ArrayList<>();
delegates.add( userDelegates() );
delegates.add( userDetailDelegates() );
itemWriter.setDelegates( delegates );
return itemWriter;
}
@Bean
public JdbcBatchItemWriter<UserBO> userDelegates() {
JdbcBatchItemWriter<UserBO> jdbcWriter = new JdbcBatchItemWriter<>();
jdbcWriter.setDataSource( databaseConfig.dataSource() );
jdbcWriter.setItemPreparedStatementSetter( new UserPreparedStmtSetter() );
jdbcWriter.setSql( UserItemWriter.I_USER_SQL );
return jdbcWriter;
}
@Bean
public JdbcBatchItemWriter<UserBO> userDetailDelegates() {
JdbcBatchItemWriter<UserBO> jdbcWriter = new JdbcBatchItemWriter<>();
jdbcWriter.setDataSource( databaseConfig.dataSource() );
jdbcWriter.setItemPreparedStatementSetter( new UserDetailPreparedStmtSetter() );
jdbcWriter.setSql( UserItemWriter.I_USERDETAIL_SQL );
return jdbcWriter;
}
SQL:
public static final String I_USER_SQL = "INSERT INTO USER(USERID, NAME, STATUS)VALUES(USER_IDSEQ.NEXTVAL,?,?)";
public static final String I_USER_DETAIL_SQL = "INSERT INTO USERDETAIL(USERDETAILID,ADDRESS1,USERID)VALUES(USERDETAIL_IDSEQ.NEXTVAL,?,?)";
来源:https://stackoverflow.com/questions/58299578/compositeitemwriter-by-using-delegates-insertjdbcbatchitemwriter-into-2-tables