CompositeItemWriter by using delegates insert(JdbcBatchItemWriter) into 2 tables with FK relation

左心房为你撑大大i 提交于 2019-12-11 15:29:02

问题


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:

  1. How to insert PK of User table to UserDetail 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!