@Bean JdbcBatchItemWriter<Contact> jdbcWriter(DataSource dataSource) { JdbcBatchItemWriter<Contact> batchItemWriter = new JdbcBatchItemWriter<>(); batchItemWriter.setItemSqlParameterSourceProvider( new BeanPropertyItemSqlParameterSourceProvider<>()); batchItemWriter.setDataSource(dataSource); batchItemWriter.setSql("insert into contact ( full_name, email) values ( :fullName, :email )"); return batchItemWriter; }
@Bean public ItemWriter<Partner> writer() { JdbcBatchItemWriter<Partner> itemWriter = new JdbcBatchItemWriter<Partner>(); itemWriter.setSql("INSERT INTO PARTNER (NAME, EMAIL) VALUES (:name,:email)"); itemWriter.setDataSource(infrastructureConfiguration.dataSource()); itemWriter.setItemSqlParameterSourceProvider( new BeanPropertyItemSqlParameterSourceProvider<Partner>()); return itemWriter; }
// 3:写数据 @Bean public ItemWriter<Person> writer(DataSource dataSource) { JdbcBatchItemWriter<Person> writer = new JdbcBatchItemWriter<Person>(); writer.setItemSqlParameterSourceProvider( new BeanPropertyItemSqlParameterSourceProvider<Person>()); writer.setSql(PERSON_INSERT); writer.setDataSource(dataSource); return writer; }
/** * @param dataSource * @return dummy item writer custom */ @Bean public ItemWriter<CustomPojo> writer(DataSource dataSource) { if ("hsqldb".equals(this.mode)) { // hsqldb writer using JdbcBatchItemWriter (the difference is the // datasource) JdbcBatchItemWriter<CustomPojo> writer = new JdbcBatchItemWriter<CustomPojo>(); writer.setItemSqlParameterSourceProvider( new BeanPropertyItemSqlParameterSourceProvider<CustomPojo>()); writer.setSql("INSERT INTO pojo (id, description) VALUES (:id, :description)"); writer.setDataSource(dataSource); return writer; } else if ("mysql".equals(this.mode)) { JdbcBatchItemWriter<CustomPojo> writer = new JdbcBatchItemWriter<CustomPojo>(); writer.setSql("INSERT INTO pojo (id, description) VALUES (:id, :description)"); writer.setDataSource(dataSource); writer.setItemSqlParameterSourceProvider( new BeanPropertyItemSqlParameterSourceProvider<CustomPojo>()); return writer; } /* * else if ("mongo".equals(this.mode)) { // a writer using the * MongoItemWriter MongoItemWriter<CustomPojo> writer = new * MongoItemWriter<CustomPojo>(); * writer.setCollection("testCollection"); return writer; } */ else if ("flat".equals(this.mode)) { // FlatFileItemWriter writer FlatFileItemWriter<CustomPojo> writer = new CustomFlatFileItemWriter(); writer.setResource(new ClassPathResource("output.csv")); BeanWrapperFieldExtractor<CustomPojo> fieldExtractor = new CustomFieldExtractor(); fieldExtractor.setNames(new String[] {"id", "description"}); DelimitedLineAggregator<CustomPojo> delLineAgg = new CustomDelimitedAggregator(); delLineAgg.setDelimiter(","); delLineAgg.setFieldExtractor(fieldExtractor); writer.setLineAggregator(delLineAgg); return writer; } else { // dummy custom writer CustomItemWriter writer = new CustomItemWriter(); return writer; } }