|
| 1 | +/* |
| 2 | + * Copyright 2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.batch.samples.petclinic; |
| 17 | + |
| 18 | +import javax.sql.DataSource; |
| 19 | + |
| 20 | +import org.springframework.batch.core.Job; |
| 21 | +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; |
| 22 | +import org.springframework.batch.core.job.builder.JobBuilder; |
| 23 | +import org.springframework.batch.core.repository.JobRepository; |
| 24 | +import org.springframework.batch.core.step.builder.StepBuilder; |
| 25 | +import org.springframework.batch.item.database.JdbcCursorItemReader; |
| 26 | +import org.springframework.batch.item.database.builder.JdbcCursorItemReaderBuilder; |
| 27 | +import org.springframework.batch.item.file.FlatFileItemWriter; |
| 28 | +import org.springframework.batch.item.file.builder.FlatFileItemWriterBuilder; |
| 29 | +import org.springframework.batch.samples.common.DataSourceConfiguration; |
| 30 | +import org.springframework.context.annotation.Bean; |
| 31 | +import org.springframework.context.annotation.Configuration; |
| 32 | +import org.springframework.context.annotation.Import; |
| 33 | +import org.springframework.core.io.FileSystemResource; |
| 34 | +import org.springframework.jdbc.core.DataClassRowMapper; |
| 35 | +import org.springframework.jdbc.support.JdbcTransactionManager; |
| 36 | + |
| 37 | +@Configuration |
| 38 | +@EnableBatchProcessing |
| 39 | +@Import(DataSourceConfiguration.class) |
| 40 | +public class OwnersExportJobConfiguration { |
| 41 | + |
| 42 | + @Bean |
| 43 | + public JdbcCursorItemReader<Owner> ownersReader(DataSource dataSource) { |
| 44 | + return new JdbcCursorItemReaderBuilder<Owner>().name("ownersReader") |
| 45 | + .sql("SELECT * FROM OWNERS") |
| 46 | + .dataSource(dataSource) |
| 47 | + .rowMapper(new DataClassRowMapper<>(Owner.class)) |
| 48 | + .build(); |
| 49 | + } |
| 50 | + |
| 51 | + @Bean |
| 52 | + public FlatFileItemWriter<Owner> ownersWriter() { |
| 53 | + return new FlatFileItemWriterBuilder<Owner>().name("ownersWriter") |
| 54 | + .resource(new FileSystemResource("owners.csv")) |
| 55 | + .delimited() |
| 56 | + .names("id", "firstname", "lastname", "address", "city", "telephone") |
| 57 | + .build(); |
| 58 | + } |
| 59 | + |
| 60 | + @Bean |
| 61 | + public Job job(JobRepository jobRepository, JdbcTransactionManager transactionManager, |
| 62 | + JdbcCursorItemReader<Owner> ownersReader, FlatFileItemWriter<Owner> ownersWriter) { |
| 63 | + return new JobBuilder("ownersExportJob", jobRepository) |
| 64 | + .start(new StepBuilder("ownersExportStep", jobRepository).<Owner, Owner>chunk(5, transactionManager) |
| 65 | + .reader(ownersReader) |
| 66 | + .writer(ownersWriter) |
| 67 | + .build()) |
| 68 | + .build(); |
| 69 | + } |
| 70 | + |
| 71 | +} |
0 commit comments