Example usage for org.springframework.batch.item.database JdbcBatchItemWriter JdbcBatchItemWriter

List of usage examples for org.springframework.batch.item.database JdbcBatchItemWriter JdbcBatchItemWriter

Introduction

In this page you can find the example usage for org.springframework.batch.item.database JdbcBatchItemWriter JdbcBatchItemWriter.

Prototype

JdbcBatchItemWriter

Source Link

Usage

From source file:de.codecentric.batch.jobs.FlatFileToDbNoSkipJobConfiguration.java

@Bean
public JdbcBatchItemWriter<Item> jdbcBatchItemWriter() {
    JdbcBatchItemWriter<Item> itemWriter = new JdbcBatchItemWriter<Item>();
    itemWriter.setSql(/*ww w .j av a  2  s  .  com*/
            "INSERT INTO ITEM (ID, DESCRIPTION, FIRST_ACTION, SECOND_ACTION) VALUES (:id,:description,:firstAction,:secondAction)");
    itemWriter.setDataSource(dataSource);
    itemWriter.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Item>());
    return itemWriter;
}

From source file:de.codecentric.batch.jobs.FlatFileToDbSkipJobConfiguration.java

@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;
}

From source file:com.github.jrrdev.mantisbtsync.core.jobs.enums.EnumsWritersConfiguration.java

/**
 * Build the JdbcBatchItemWriter for the given enums table.
 *
 * @param tableName// ww w.j  ava  2 s.  c  om
 *       Name of the destination table
 * @param dataSource
 *       The datasource
 * @return
 */
private JdbcBatchItemWriter<ObjectRef> getEnumWriter(final String tableName, final DataSource dataSource) {
    final JdbcBatchItemWriter<ObjectRef> writer = new JdbcBatchItemWriter<ObjectRef>();
    writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<ObjectRef>());
    writer.setSql(getMergeStatement(tableName));
    writer.setDataSource(dataSource);
    return writer;
}

From source file:com.github.jrrdev.mantisbtsync.core.jobs.projects.ProjectsWritersConfiguration.java

/**
 * Build the composite writer upserting the data related to the users of a project.
 * Perform upsert in the mantis_user_table and mantis_project_user_list_table tables.
 *
 * @param dataSource//  ww  w .  j ava2  s . c o  m
 *          The datasource
 * @return the writer upserting the data related to the users of a project
 */
@Bean
@StepScope
public CompositeItemWriter<AccountData> projectUsersWriter(final DataSource dataSource,
        @Value("#{jobExecutionContext['mantis.loop.project_id']}") final BigInteger projectId) {

    final JdbcBatchItemWriter<AccountData> writer1 = new JdbcBatchItemWriter<AccountData>();
    writer1.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<AccountData>());
    writer1.setSql("INSERT INTO mantis_user_table (id, name)\n" + " VALUES (:id, :name)\n"
            + " ON DUPLICATE KEY UPDATE name = :name");
    writer1.setDataSource(dataSource);
    writer1.afterPropertiesSet();

    final JdbcBatchItemWriter<AccountData> writer2 = new JdbcBatchItemWriter<AccountData>();
    writer2.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<AccountData>());
    writer2.setSql("INSERT INTO mantis_project_user_list_table (user_id, project_id)\n" + " VALUES (:id, "
            + projectId + ")\n" + " ON DUPLICATE KEY UPDATE project_id = project_id");
    writer2.setDataSource(dataSource);
    writer2.setAssertUpdates(false);
    writer2.afterPropertiesSet();

    final CompositeItemWriter<AccountData> compositeWriter = new CompositeItemWriter<AccountData>();
    final List<ItemWriter<? super AccountData>> writerList = new ArrayList<ItemWriter<? super AccountData>>();
    writerList.add(writer1);
    writerList.add(writer2);
    compositeWriter.setDelegates(writerList);

    return compositeWriter;
}

From source file:com.github.jrrdev.mantisbtsync.core.jobs.projects.ProjectsWritersConfiguration.java

/**
 * Build the writer upserting the data related to the versions of a project.
 * Perform upsert in the mantis_project_version_table table.
 *
 * @param dataSource/*  w w w  . j  a  v a  2 s. co  m*/
 *          The datasource
 * @return the writer upserting the data related to the versions of a project
 */
@Bean
@StepScope
public JdbcBatchItemWriter<ProjectVersionData> projectVersionsWriter(final DataSource dataSource) {

    final JdbcBatchItemWriter<ProjectVersionData> writer = new JdbcBatchItemWriter<ProjectVersionData>();
    writer.setItemSqlParameterSourceProvider(
            new BeanPropertyItemSqlParameterSourceProvider<ProjectVersionData>());
    writer.setSql("INSERT INTO mantis_project_version_table\n"
            + " (id, version, project_id, description, released, obsolete)"
            + " VALUES (:id, :name, :project_id, :description, :released, :obsolete)\n"
            + " ON DUPLICATE KEY UPDATE version = :name, project_id = :project_id,\n"
            + "   description = :description, released = :released, obsolete = :obsolete");
    writer.setDataSource(dataSource);
    return writer;
}

From source file:io.spring.batch.configuration.JobConfiguration.java

@Bean
@StepScope//from  w  w w  .  j  a va2s  .  co  m
public JdbcBatchItemWriter<Customer> customerItemWriter() {
    JdbcBatchItemWriter<Customer> itemWriter = new JdbcBatchItemWriter<>();

    itemWriter.setDataSource(this.dataSource);
    itemWriter.setSql("INSERT INTO NEW_CUSTOMER VALUES (:id, :firstName, :lastName, :birthdate)");
    itemWriter.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider());
    itemWriter.afterPropertiesSet();

    return itemWriter;
}

From source file:org.cloudfoundry.workers.stocks.batch.BatchConfiguration.java

/***
 * The Spring Batch {@link org.springframework.batch.item.ItemWriter item
 * writer} persists the {@link StockSymbolLookup} lookup results into a
 * table (<CODE>STOCKS_DATA</CODE>).
 * /*  w w  w.j a v  a  2  s.co  m*/
 * @param dateOfAnalysis
 */
@Bean(name = "writer")
@Scope("step")
public JdbcBatchItemWriter<StockSymbolLookup> writer(
        final @Value("#{jobParameters['date']}") Date dateOfAnalysis) {
    JdbcBatchItemWriter<StockSymbolLookup> jdbcBatchItemWriter = new JdbcBatchItemWriter<StockSymbolLookup>();
    jdbcBatchItemWriter.setSql(
            "INSERT INTO STOCKS_DATA( DATE_ANALYSED, HIGH_PRICE, LOW_PRICE,  CLOSING_PRICE, SYMBOL) VALUES ( :da, :hp, :lp,  :cp, :s ) ");
    jdbcBatchItemWriter.setDataSource(dsConfig.dataSource());
    jdbcBatchItemWriter
            .setItemSqlParameterSourceProvider(new ItemSqlParameterSourceProvider<StockSymbolLookup>() {
                @Override
                public SqlParameterSource createSqlParameterSource(StockSymbolLookup item) {
                    return new MapSqlParameterSource()
                            .addValue("da", new java.sql.Date(dateOfAnalysis.getTime()), Types.DATE)
                            .addValue("hp", item.getHighPrice(), Types.DOUBLE)
                            .addValue("lp", item.getLowPrice(), Types.DOUBLE).addValue("s", item.getTicker())
                            .addValue("si", item.getId(), Types.BIGINT)
                            .addValue("cp", item.getLastValueWhileOpen(), Types.DOUBLE);

                }
            });
    return jdbcBatchItemWriter;
}

From source file:uk.ac.kcl.batch.JobConfiguration.java

@Bean
@StepScope/*from   w  w w . j  a v  a  2  s  .  c o  m*/
@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.getProperty("target.Sql"));
    writer.setDataSource(jdbcDocumentTarget);
    return writer;
}

From source file:uk.ac.kcl.batch.JobConfiguration.java

@Bean
@StepScope//from ww w.ja  v a  2s  .c  o  m
@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.getProperty("target.Sql"));
    writer.setDataSource(jdbcDocumentTarget);
    return writer;
}