001/* 002 * Copyright 2006-2007 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.batch.sample.domain.trade.internal; 018 019import java.math.BigDecimal; 020import java.sql.ResultSet; 021import java.sql.SQLException; 022import java.util.List; 023 024import org.springframework.batch.sample.domain.trade.CustomerCredit; 025import org.springframework.batch.sample.domain.trade.CustomerDao; 026import org.springframework.jdbc.core.RowMapper; 027import org.springframework.jdbc.core.support.JdbcDaoSupport; 028import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer; 029 030/** 031 * @author Lucas Ward 032 * 033 */ 034public class JdbcCustomerDao extends JdbcDaoSupport implements CustomerDao{ 035 036 private static final String GET_CUSTOMER_BY_NAME = "SELECT ID, NAME, CREDIT from CUSTOMER where NAME = ?"; 037 private static final String INSERT_CUSTOMER = "INSERT into CUSTOMER(ID, NAME, CREDIT) values(?,?,?)"; 038 private static final String UPDATE_CUSTOMER = "UPDATE CUSTOMER set CREDIT = ? where NAME = ?"; 039 040 private DataFieldMaxValueIncrementer incrementer; 041 042 public void setIncrementer(DataFieldMaxValueIncrementer incrementer) { 043 this.incrementer = incrementer; 044 } 045 046 @Override 047 public CustomerCredit getCustomerByName(String name) { 048 049 List<CustomerCredit> customers = getJdbcTemplate().query(GET_CUSTOMER_BY_NAME, new Object[]{name}, 050 051 new RowMapper<CustomerCredit>(){ 052 053 @Override 054 public CustomerCredit mapRow(ResultSet rs, int rowNum) throws SQLException { 055 CustomerCredit customer = new CustomerCredit(); 056 customer.setName(rs.getString("NAME")); 057 customer.setId(rs.getInt("ID")); 058 customer.setCredit(rs.getBigDecimal("CREDIT")); 059 return customer; 060 } 061 062 }); 063 064 if(customers.size() == 0){ 065 return null; 066 } 067 else{ 068 return customers.get(0); 069 } 070 071 } 072 073 @Override 074 public void insertCustomer(String name, BigDecimal credit) { 075 076 getJdbcTemplate().update(INSERT_CUSTOMER, new Object[]{incrementer.nextIntValue(), name, credit}); 077 } 078 079 @Override 080 public void updateCustomer(String name, BigDecimal credit) { 081 getJdbcTemplate().update(UPDATE_CUSTOMER, new Object[]{credit, name}); 082 } 083 084}