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

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

Introduction

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

Prototype

public void setSql(String sql) 

Source Link

Document

Public setter for the query string to execute on write.

Usage

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

@Bean
protected JdbcBatchItemWriter<Customer> writer(DataSource dataSource) {
    JdbcBatchItemWriter<Customer> writer = new JdbcBatchItemWriter<>();
    writer.setDataSource(dataSource);/*from w  ww  .  j  a  v  a  2s . c o m*/
    writer.setSql("INSERT INTO CUSTOMER VALUES(:customerName, :qty)");
    writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Customer>());
    writer.afterPropertiesSet();

    return writer;
}

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

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

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.apress.prospringintegration.batch.JobConfiguration.java

@Bean
public JdbcBatchItemWriter dataWriter() {
    JdbcBatchItemWriter jdbcBatchItemWriter = new JdbcBatchItemWriter();
    jdbcBatchItemWriter.setAssertUpdates(true);
    jdbcBatchItemWriter.setDataSource(dataSource);
    jdbcBatchItemWriter.setSql("insert into USER_REGISTRATION(FIRST_NAME, LAST_NAME, COMPANY,"
            + "ADDRESS, CITY, STATE, ZIP, COUNTY, URL, PHONE_NUMBER, FAX )"
            + "values (:firstName, :lastName, :company, :address, :city ,"
            + ":state, :zip, :county, :url, :phoneNumber, :fax )");

    jdbcBatchItemWriter.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider());

    return jdbcBatchItemWriter;
}

From source file:io.spring.marchmadness.MooreStatConfiguration.java

@Bean
public ItemWriter<MooreNcaaStat> writer(DataSource dataSource) {
    JdbcBatchItemWriter<MooreNcaaStat> writer = new JdbcBatchItemWriter<MooreNcaaStat>();
    writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<MooreNcaaStat>());
    writer.setSql("INSERT INTO MOORE_NCAA_STATS (YEAR, RANK, " + "NAME, " + "WIN, " + "LOSS, " + "TIE, "
            + "SOS, " + "PR ) VALUES (:year, :rank, :name, :win, :loss, :tie, :sos, :pr)");
    writer.setDataSource(dataSource);//from   ww w  . j  av  a 2 s .c  om
    return writer;
}

From source file:io.spring.marchmadness.NcaaStatConfiguration.java

@Bean
public ItemWriter<NcaaStats> writer(DataSource dataSource) {
    JdbcBatchItemWriter<NcaaStats> writer = new JdbcBatchItemWriter<NcaaStats>();
    writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<NcaaStats>());
    writer.setSql("INSERT INTO NCAA_STATS (YEAR," + "RANK, " + "NAME, " + "RATING, " + "WIN, " + "LOSS, "
            + "SCHEDL, " + "SCHEDL_RANK, " + "WIN_TOP_25, " + "LOSS_TOP_25, " + "WIN_TOP_50, " + "LOSS_TOP_50, "
            + "PREDICTOR, " + "PREDICTOR_RANK, " + "GOLDEN_MEAN, " + "GOLDEN_MEAN_RANK, " + "RECENT,"
            + "RECENT_RANK) VALUES (:year, :rank, :name, :rating, :win, :loss, "
            + ":schedl, :schedlRank, :winTop25, :lossTop25, :winTop50, :lossTop50, "
            + ":predictor, :predictorRank, :goldenMean, :goldenMeanRank, :recent, " + ":recentRank)");
    writer.setDataSource(dataSource);/*  w  w w  .  j  a v  a  2s  .  c  o  m*/
    return writer;
}

From source file:com.github.jrrdev.mantisbtsync.core.jobs.issues.IssuesWritersConfiguration.java

/**
 * Build the writer upserting the data related to an issue.
 * Perform upsert in the mantis_bug_table table.
 *
 * @param dataSource/*  w w  w.  j av  a 2  s .  com*/
 *          The datasource
 * @return the writer upserting the data related to an issue
 */
@Bean
@StepScope
public JdbcBatchItemWriter<BugBean> bugsWriter(final DataSource dataSource) {

    final JdbcBatchItemWriter<BugBean> writer = new JdbcBatchItemWriter<BugBean>();
    writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<BugBean>());
    writer.setSql("INSERT INTO mantis_bug_table (id, project_id, reporter_id, handler_id, priority_id,\n"
            + "    severity_id, status_id, resolution_id, description, steps_to_reproduce,\n"
            + "    additional_information, platform, version, fixed_in_version, target_version,\n"
            + "    summary, category, date_submitted, last_updated, last_sync)\n"
            + " VALUES (:id, :projectId, :reporterId, :handlerId, :priorityId,\n"
            + "    :severityId, :statusId, :resolutionId, :description, :stepsToReproduce,\n"
            + "    :additionalInformation, :platform, :version, :fixedInVersion, :targetVersion,\n"
            + "    :summary, :category, :dateSubmitted, :lastUpdated, sysdate())\n"
            + " ON DUPLICATE KEY UPDATE project_id = :projectId, reporter_id = :reporterId,\n"
            + "     handler_id = :handlerId, priority_id = :priorityId,\n"
            + "    severity_id = :severityId, status_id = :statusId, resolution_id = :resolutionId,\n"
            + "    description = :description, steps_to_reproduce = :stepsToReproduce,\n"
            + "    additional_information = :additionalInformation, platform = :platform,\n"
            + "    version = :version, fixed_in_version = :fixedInVersion, target_version = :targetVersion,\n"
            + "    summary = :summary, category = :category, date_submitted = :dateSubmitted,\n"
            + "    last_updated = :lastUpdated, last_sync = sysdate()");
    writer.setDataSource(dataSource);
    writer.setAssertUpdates(false);
    return writer;
}

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

@Bean
@StepScope//from  w  ww.j ava2  s.  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: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/*  w w  w. jav  a  2s  . c  om*/
 *          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.enums.EnumsWritersConfiguration.java

/**
 * Build the JdbcBatchItemWriter for the given enums table.
 *
 * @param tableName/*from w w w .j av a 2s  .  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;
}