View Javadoc

1   /*
2    * Copyright 2006-2012 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    *      http://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  
17  package org.springframework.batch.sample.domain.football.internal;
18  
19  import java.util.List;
20  
21  import javax.sql.DataSource;
22  
23  import org.springframework.batch.item.ItemWriter;
24  import org.springframework.batch.sample.domain.football.PlayerSummary;
25  import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
26  import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
27  import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
28  
29  public class JdbcPlayerSummaryDao implements ItemWriter<PlayerSummary> {
30  
31  	private static final String INSERT_SUMMARY = "INSERT into PLAYER_SUMMARY(ID, YEAR_NO, COMPLETES, ATTEMPTS, PASSING_YARDS, PASSING_TD, "
32  			+ "INTERCEPTIONS, RUSHES, RUSH_YARDS, RECEPTIONS, RECEPTIONS_YARDS, TOTAL_TD) "
33  			+ "values(:id, :year, :completes, :attempts, :passingYards, :passingTd, "
34  			+ ":interceptions, :rushes, :rushYards, :receptions, :receptionYards, :totalTd)";
35  
36      private NamedParameterJdbcOperations namedParameterJdbcTemplate;
37  
38  	public void write(List<? extends PlayerSummary> summaries) {
39  
40  		for (PlayerSummary summary : summaries) {
41  
42  			MapSqlParameterSource args = new MapSqlParameterSource().addValue("id", summary.getId()).addValue("year",
43  					summary.getYear()).addValue("completes", summary.getCompletes()).addValue("attempts",
44  					summary.getAttempts()).addValue("passingYards", summary.getPassingYards()).addValue("passingTd",
45  					summary.getPassingTd()).addValue("interceptions", summary.getInterceptions()).addValue("rushes",
46  					summary.getRushes()).addValue("rushYards", summary.getRushYards()).addValue("receptions",
47  					summary.getReceptions()).addValue("receptionYards", summary.getReceptionYards()).addValue(
48  					"totalTd", summary.getTotalTd());
49  
50              namedParameterJdbcTemplate.update(INSERT_SUMMARY, args);
51  		}
52  	}
53  
54      public void setDataSource(DataSource dataSource) {
55          this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
56      }
57  }