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: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 ww.j  a v  a  2  s.c  o  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: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/*from  w w  w  .  j  av a2  s .  c  om*/
 *          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:com.github.jrrdev.mantisbtsync.core.jobs.projects.ProjectsWritersConfiguration.java

/**
 * Build the writer upserting the data related to the categories of a project.
 * Perform upsert in the mantis_category_table table.
 *
 * @param dataSource//  w  ww .j  av a2 s .  c om
 *          The datasource
 * @return the writer upserting the data related to the categories of a project
 */
@Bean
@StepScope
public JdbcBatchItemWriter<ProjectCategoryBean> projectCategoriesWriter(final DataSource dataSource) {

    final JdbcBatchItemWriter<ProjectCategoryBean> writer = new JdbcBatchItemWriter<ProjectCategoryBean>();
    writer.setItemSqlParameterSourceProvider(
            new BeanPropertyItemSqlParameterSourceProvider<ProjectCategoryBean>());
    writer.setSql(
            "INSERT INTO mantis_category_table (name, project_id)\n" + " SELECT :name, :projectId FROM dual\n"
                    + " WHERE NOT EXISTS (SELECT 1 FROM mantis_category_table dest\n"
                    + "         WHERE dest.name = :name AND dest.project_id = :projectId)");
    writer.setDataSource(dataSource);
    writer.setAssertUpdates(false);
    return writer;
}

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

/**
 * Build the composite writer upserting the data related to the custom fields of a project.
 * Perform upsert in the mantis_custom_field_table and mantis_custom_field_project_table tables.
 *
 * @param dataSource/*ww w .ja  v a 2  s .  c o m*/
 *          The datasource
 * @return the writer upserting the data related to the custom fields of a project
 */
@Bean
@StepScope
public CompositeItemWriter<ProjectCustomFieldBean> projectCustomFieldsWriter(final DataSource dataSource) {

    final JdbcBatchItemWriter<ProjectCustomFieldBean> writer1 = new JdbcBatchItemWriter<ProjectCustomFieldBean>();
    writer1.setItemSqlParameterSourceProvider(
            new BeanPropertyItemSqlParameterSourceProvider<ProjectCustomFieldBean>());
    writer1.setSql("INSERT INTO mantis_custom_field_table\n"
            + " (id, name, type_id, possible_values, default_value, valid_regexp)\n"
            + " VALUES (:id, :name, :typeId, :possibleValues, :defaultValue, :validRegexp)\n"
            + " ON DUPLICATE KEY UPDATE name = :name, type_id = :typeId, possible_values = :possibleValues,\n"
            + " default_value = :defaultValue, valid_regexp = :validRegexp");
    writer1.setDataSource(dataSource);
    writer1.afterPropertiesSet();

    final JdbcBatchItemWriter<ProjectCustomFieldBean> writer2 = new JdbcBatchItemWriter<ProjectCustomFieldBean>();
    writer2.setItemSqlParameterSourceProvider(
            new BeanPropertyItemSqlParameterSourceProvider<ProjectCustomFieldBean>());
    writer2.setSql("INSERT INTO mantis_custom_field_project_table (field_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<ProjectCustomFieldBean> compositeWriter = new CompositeItemWriter<ProjectCustomFieldBean>();
    final List<ItemWriter<? super ProjectCustomFieldBean>> writerList = new ArrayList<ItemWriter<? super ProjectCustomFieldBean>>();
    writerList.add(writer1);
    writerList.add(writer2);
    compositeWriter.setDelegates(writerList);

    return compositeWriter;
}

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

@Bean
@StepScope// ww w.ja va  2s  .  c  om
@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;
}

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

@Bean
@StepScope//  w ww .  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;
}