Java Code Examples for org.springframework.batch.item.database.JdbcBatchItemWriter#setItemSqlParameterSourceProvider()

The following examples show how to use org.springframework.batch.item.database.JdbcBatchItemWriter#setItemSqlParameterSourceProvider() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DatabaseItemWriterDemo.java    From SpringAll with MIT License 6 votes vote down vote up
private ItemWriter<TestData> dataSourceItemWriter() {
    // ItemWriter的实现类之一,mysql数据库数据写入使用JdbcBatchItemWriter,
    // 其他实现:MongoItemWriter,Neo4jItemWriter等
    JdbcBatchItemWriter<TestData> writer = new JdbcBatchItemWriter<>();
    writer.setDataSource(dataSource); // 设置数据源

    String sql = "insert into TEST(id,field1,field2,field3) values (:id,:field1,:field2,:field3)";
    writer.setSql(sql); // 设置插入sql脚本

    // 映射TestData对象属性到占位符中的属性
    BeanPropertyItemSqlParameterSourceProvider<TestData> provider = new BeanPropertyItemSqlParameterSourceProvider<>();
    writer.setItemSqlParameterSourceProvider(provider);

    writer.afterPropertiesSet(); // 设置一些额外属性
    return writer;
}
 
Example 2
Source File: PeopleWriter.java    From spring-cloud with Apache License 2.0 5 votes vote down vote up
@Bean
@Qualifier("jdbcBatchItemWriter")
public JdbcBatchItemWriter<People> jdbcBatchItemWriter() {
    JdbcBatchItemWriter<People> writer = new JdbcBatchItemWriter<>();
    writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>());
    writer.setSql("insert into people (person_id, first_name, last_name) VALUES (uuid(), :firstName, :lastName)");
    writer.setDataSource(w_dataSource);
    return writer;
}
 
Example 3
Source File: BatchConfiguration.java    From Software-Architecture-with-Spring-5.0 with MIT License 5 votes vote down vote up
@Bean(name = DATA_WRITER)
public JdbcBatchItemWriter<JavaChampion> writer(DataSource dataSource) throws Exception {
    JdbcBatchItemWriter<JavaChampion> jdbcBatchItemWriter = new JdbcBatchItemWriter<JavaChampion>();
    jdbcBatchItemWriter.setAssertUpdates(true);
    jdbcBatchItemWriter.setDataSource(dataSource);
    jdbcBatchItemWriter.setSql(
            " INSERT INTO java_champion( first_name, last_name, country, year )" +
                    " VALUES ( :firstName , :lastName, :country, :year ) ");
    jdbcBatchItemWriter.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>());
    return jdbcBatchItemWriter;
}
 
Example 4
Source File: JobConfiguration.java    From CogStack-Pipeline with Apache License 2.0 5 votes vote down vote up
@Bean
@StepScope
@Qualifier("simpleJdbcItemWriter")
@Profile("jdbc_out")
public ItemWriter<Document> simpleJdbcItemWriter(
        @Qualifier("targetDataSource") DataSource jdbcDocumentTarget) {
    JdbcBatchItemWriter<Document> writer = new JdbcBatchItemWriter<>();
    writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>());
    writer.setSql(env.getRequiredProperty("target.Sql"));
    writer.setDataSource(jdbcDocumentTarget);
    return writer;
}
 
Example 5
Source File: JobConfiguration.java    From CogStack-Pipeline with Apache License 2.0 5 votes vote down vote up
@Bean
@StepScope
@Qualifier("mapJdbcItemWriter")
@Profile("jdbc_out_map")
public ItemWriter<Document> mapJdbcItemWriter(
        @Qualifier("targetDataSource") DataSource jdbcDocumentTarget) {
    JdbcBatchItemWriter<Document> writer = new JdbcBatchItemWriter<>();
    writer.setItemSqlParameterSourceProvider(new MapItemSqlParameterSourceProvider<Document>());
    writer.setSql(env.getRequiredProperty("target.Sql"));
    writer.setDataSource(jdbcDocumentTarget);
    return writer;
}
 
Example 6
Source File: BatchConfiguration.java    From building-microservices with Apache License 2.0 5 votes vote down vote up
@Bean
JdbcBatchItemWriter<Person> jdbcBatchItemWriter(DataSource h2) {
    JdbcBatchItemWriter<Person> w = new JdbcBatchItemWriter<>();
    w.setDataSource(h2);
    w.setSql("insert into PEOPLE( first, last, email) values ( :first, :last, :email )");
    w.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>());
    return w;
}
 
Example 7
Source File: AddressMigrationJobConfiguration.java    From spring-batch-lightmin with Apache License 2.0 5 votes vote down vote up
@Bean
public JdbcBatchItemWriter<BatchTaskAddress> addressWriter(final DataSource dataSource) {
    final JdbcBatchItemWriter<BatchTaskAddress> writer = new JdbcBatchItemWriter<>();
    writer.setDataSource(dataSource);
    writer.setSql(UPDATE_ADDRESS_STATEMENT);
    writer.setItemSqlParameterSourceProvider(item -> {
        final MapSqlParameterSource sqlParameterSource = new MapSqlParameterSource();
        sqlParameterSource.addValue("processing_state", item.getProcessingState());
        sqlParameterSource.addValue("batch_task_id", item.getBatchTaskId());
        return sqlParameterSource;
    });
    writer.afterPropertiesSet();
    return writer;
}
 
Example 8
Source File: AddressPrinterJobConfiguration.java    From spring-batch-lightmin with Apache License 2.0 5 votes vote down vote up
@Bean
public JdbcBatchItemWriter<Long> addressBatchTaskDeletionWriter(final DataSource dataSource) {
    final JdbcBatchItemWriter<Long> writer = new JdbcBatchItemWriter<>();
    writer.setDataSource(dataSource);
    writer.setSql(DELETE_BATCH_TASK_ADDRESS_STATEMENT);
    writer.setItemSqlParameterSourceProvider(batchTaskId -> {
        final MapSqlParameterSource mapSqlParameterSource = new MapSqlParameterSource();
        mapSqlParameterSource.addValue("batch_task_id", batchTaskId);
        return mapSqlParameterSource;
    });
    return writer;
}
 
Example 9
Source File: FlatFileToDbNoSkipJobConfiguration.java    From spring-boot-starter-batch-web with Apache License 2.0 5 votes vote down vote up
@Bean
public JdbcBatchItemWriter<Item> jdbcBatchItemWriter() {
	JdbcBatchItemWriter<Item> itemWriter = new JdbcBatchItemWriter<Item>();
	itemWriter.setSql(
			"INSERT INTO ITEM (ID, DESCRIPTION, FIRST_ACTION, SECOND_ACTION) VALUES (:id,:description,:firstAction,:secondAction)");
	itemWriter.setDataSource(dataSource);
	itemWriter.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Item>());
	return itemWriter;
}
 
Example 10
Source File: FlatFileToDbSkipReaderTransactionalJobConfiguration.java    From spring-boot-starter-batch-web with Apache License 2.0 5 votes vote down vote up
@Bean
public JdbcBatchItemWriter<Item> jdbcBatchItemWriter() {
	JdbcBatchItemWriter<Item> itemWriter = new JdbcBatchItemWriter<Item>();
	itemWriter.setSql("INSERT INTO ITEM (ID, DESCRIPTION) VALUES (:id,:description)");
	itemWriter.setDataSource(dataSource);
	itemWriter.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Item>());
	return itemWriter;
}
 
Example 11
Source File: FlatFileToDbSkipProcessorNonTransactionalJobConfiguration.java    From spring-boot-starter-batch-web with Apache License 2.0 5 votes vote down vote up
@Bean
public JdbcBatchItemWriter<Item> jdbcBatchItemWriter() {
	JdbcBatchItemWriter<Item> itemWriter = new JdbcBatchItemWriter<Item>();
	itemWriter.setSql("INSERT INTO ITEM (ID, DESCRIPTION) VALUES (:id,:description)");
	itemWriter.setDataSource(dataSource);
	itemWriter.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Item>());
	return itemWriter;
}
 
Example 12
Source File: FlatFileToDbSkipJobConfiguration.java    From spring-boot-starter-batch-web with Apache License 2.0 5 votes vote down vote up
@Bean
public JdbcBatchItemWriter<Item> jdbcBatchItemWriter() {
	JdbcBatchItemWriter<Item> itemWriter = new JdbcBatchItemWriter<Item>();
	itemWriter.setSql("INSERT INTO ITEM (ID, DESCRIPTION) VALUES (:id,:description)");
	itemWriter.setDataSource(dataSource);
	itemWriter.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Item>());
	return itemWriter;
}