001/* 002 * Copyright 2002-2018 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.jdbc.core.namedparam; 018 019import java.beans.PropertyDescriptor; 020import java.util.ArrayList; 021import java.util.List; 022 023import org.springframework.beans.BeanWrapper; 024import org.springframework.beans.NotReadablePropertyException; 025import org.springframework.beans.PropertyAccessor; 026import org.springframework.beans.PropertyAccessorFactory; 027import org.springframework.jdbc.core.StatementCreatorUtils; 028import org.springframework.util.StringUtils; 029 030/** 031 * {@link SqlParameterSource} implementation that obtains parameter values 032 * from bean properties of a given JavaBean object. The names of the bean 033 * properties have to match the parameter names. 034 * 035 * <p>Uses a Spring BeanWrapper for bean property access underneath. 036 * 037 * @author Thomas Risberg 038 * @author Juergen Hoeller 039 * @since 2.0 040 * @see NamedParameterJdbcTemplate 041 * @see org.springframework.beans.BeanWrapper 042 */ 043public class BeanPropertySqlParameterSource extends AbstractSqlParameterSource { 044 045 private final BeanWrapper beanWrapper; 046 047 private String[] propertyNames; 048 049 050 /** 051 * Create a new BeanPropertySqlParameterSource for the given bean. 052 * @param object the bean instance to wrap 053 */ 054 public BeanPropertySqlParameterSource(Object object) { 055 this.beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(object); 056 } 057 058 059 @Override 060 public boolean hasValue(String paramName) { 061 return this.beanWrapper.isReadableProperty(paramName); 062 } 063 064 @Override 065 public Object getValue(String paramName) throws IllegalArgumentException { 066 try { 067 return this.beanWrapper.getPropertyValue(paramName); 068 } 069 catch (NotReadablePropertyException ex) { 070 throw new IllegalArgumentException(ex.getMessage()); 071 } 072 } 073 074 /** 075 * Derives a default SQL type from the corresponding property type. 076 * @see org.springframework.jdbc.core.StatementCreatorUtils#javaTypeToSqlParameterType 077 */ 078 @Override 079 public int getSqlType(String paramName) { 080 int sqlType = super.getSqlType(paramName); 081 if (sqlType != TYPE_UNKNOWN) { 082 return sqlType; 083 } 084 Class<?> propType = this.beanWrapper.getPropertyType(paramName); 085 return StatementCreatorUtils.javaTypeToSqlParameterType(propType); 086 } 087 088 /** 089 * Provide access to the property names of the wrapped bean. 090 * Uses support provided in the {@link PropertyAccessor} interface. 091 * @return an array containing all the known property names 092 */ 093 public String[] getReadablePropertyNames() { 094 if (this.propertyNames == null) { 095 List<String> names = new ArrayList<String>(); 096 PropertyDescriptor[] props = this.beanWrapper.getPropertyDescriptors(); 097 for (PropertyDescriptor pd : props) { 098 if (this.beanWrapper.isReadableProperty(pd.getName())) { 099 names.add(pd.getName()); 100 } 101 } 102 this.propertyNames = StringUtils.toStringArray(names); 103 } 104 return this.propertyNames; 105 } 106 107}