Java Examples for org.springframework.batch.item.database.ItemSqlParameterSourceProvider
The following java examples will help you to understand the usage of org.springframework.batch.item.database.ItemSqlParameterSourceProvider. These source code samples are taken from different open source projects.
Example 1
| Project: spring-social-flickr-master File: BatchConfiguration.java View source code |
/**
* basically loads the photo albums from the Flickr service
* and loads them into the database
* <p/>
* <CODE>insert into photo_albums(title, user_id, description, album_id, url) values( :t, :ui, :d, :a, :u )</CODE>
*/
@Bean(name = "photoAlbumItemWriter")
@Inject
public JdbcBatchItemWriter<PhotoSet> writer(DataSource ds) {
String upsertPhotoAlbumsSql = "with new_values (title, user_id, description, album_id, url, count_photos, count_videos) as ( " + " values ( :t, :ui, :d, :a, :u, :cp, :cv ) ), " + "upsert as " + "( update photo_albums p set count_photos=nv.count_photos,count_videos=nv.count_videos, title = nv.title, user_id = nv.user_id, description = nv.description, album_id = nv.album_id, url = nv.url " + " FROM new_values nv WHERE p.album_id = nv.album_id RETURNING p.* ) " + "INSERT INTO photo_albums (title, user_id, description, album_id, url, count_photos, count_videos) " + "SELECT nv.* FROM new_values nv " + "WHERE NOT EXISTS (SELECT 1 FROM upsert up WHERE up.album_id = nv.album_id )";
JdbcBatchItemWriter<PhotoSet> jdbcBatchItemWriter = new JdbcBatchItemWriter<PhotoSet>();
jdbcBatchItemWriter.setSql(upsertPhotoAlbumsSql);
jdbcBatchItemWriter.setDataSource(ds);
// were doing upserts, so these writes may never take effect
jdbcBatchItemWriter.setAssertUpdates(false);
jdbcBatchItemWriter.setItemSqlParameterSourceProvider(new ItemSqlParameterSourceProvider<PhotoSet>() {
@Override
public SqlParameterSource createSqlParameterSource(PhotoSet item) {
return new MapSqlParameterSource().addValue("t", item.getTitle()).addValue("d", item.getDescription()).addValue("ui", item.getUserId()).addValue("a", item.getId()).addValue("cv", item.getCountOfVideos(), Types.INTEGER).addValue("cp", item.getCountOfPhotos(), Types.INTEGER).addValue("u", item.getUrl());
}
});
return jdbcBatchItemWriter;
}Example 2
| Project: spring-batch-master File: JdbcBatchItemWriterBuilder.java View source code |
/**
* Configures a {@link ItemSqlParameterSourceProvider} for use by the writer. This
* should only be used if {@link #beanMapped()} isn't called.
*
* @param itemSqlParameterSourceProvider The {@link ItemSqlParameterSourceProvider}
* @return The current instance of the builder for chaining
* @see JdbcBatchItemWriter#setItemSqlParameterSourceProvider(ItemSqlParameterSourceProvider)
*/
public JdbcBatchItemWriterBuilder<T> itemSqlParameterSourceProvider(ItemSqlParameterSourceProvider<T> itemSqlParameterSourceProvider) {
this.itemSqlParameterSourceProvider = itemSqlParameterSourceProvider;
return this;
}