-
Notifications
You must be signed in to change notification settings - Fork 0
/
BeanPropertyRowMapper.java
executable file
·121 lines (104 loc) · 4.15 KB
/
BeanPropertyRowMapper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package com.zy.jdbclib.utils;
import java.beans.PropertyDescriptor;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.zy.jdbclib.core.JDBCException;
import com.zy.jdbclib.core.RowMapper;
/**
* @version 1.0
* @since 1.0
*/
public class BeanPropertyRowMapper<T extends Object> implements RowMapper<T> {
protected final Log log = LogFactory.getLog(getClass());
protected Class<T> mappedClass;
private Map<String, PropertyDescriptor> mappedFields;
public BeanPropertyRowMapper(Class<T> mappedClass) {
initialize(mappedClass);
}
/**
* Initialize the mapping metadata for the given class.
*
* @param mappedClass the mapped class.
*/
protected void initialize(Class<T> mappedClass) {
this.mappedClass = mappedClass;
this.mappedFields = new HashMap<String, PropertyDescriptor>();
PropertyDescriptor[] pds = ReflectionUtils.getPropertyDescriptors(mappedClass);
for (int i = 0; i < pds.length; i++) {
PropertyDescriptor pd = pds[i];
if (pd.getWriteMethod() != null) {
this.mappedFields.put(pd.getName().toLowerCase(), pd);
String underscoredName = underscoreName(pd.getName());
if (!pd.getName().toLowerCase().equals(underscoredName)) {
this.mappedFields.put(underscoredName, pd);
}
}
}
}
/**
* Convert a name in camelCase to an underscored name in lower case. Any
* upper case letters are converted to lower case with a preceding
* underscore.
*
* @param name the string containing original name
* @return the converted name
*/
private String underscoreName(String name) {
StringBuffer result = new StringBuffer();
if (name != null && name.length() > 0) {
result.append(name.substring(0, 1).toLowerCase());
for (int i = 1; i < name.length(); i++) {
String s = name.substring(i, i + 1);
if (s.equals(s.toUpperCase())) {
result.append("_");
result.append(s.toLowerCase());
} else {
result.append(s);
}
}
}
return result.toString();
}
/**
* Get the class that we are mapping to.
*/
public final Class<T> getMappedClass() {
return this.mappedClass;
}
/**
*
*/
public T mapRow(ResultSet rs, int rowNumber) throws SQLException {
Assert.notNull(mappedClass, "Mapped class was not specified");
T mappedObject = ReflectionUtils.instantiateClass(this.mappedClass);
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
for (int index = 1; index <= columnCount; index++) {
String column = JdbcUtils.lookupColumnName(rsmd, index).toLowerCase();
PropertyDescriptor pd = (PropertyDescriptor)this.mappedFields.get(column);
if (pd != null) {
try {
Object value = getColumnValue(rs, index, pd);
if (log.isDebugEnabled() && rowNumber == 0) {
log.debug("Mapping column '" + column + "' to property '" + pd.getName()
+ "' of type " + pd.getPropertyType());
}
ReflectionUtils.setFieldValue(mappedObject, pd.getName(), value);
} catch (Exception ex) {
throw new JDBCException("Unable to map column " + column + " to property "
+ pd.getName(), ex);
}
}
}
return mappedObject;
}
protected Object getColumnValue(ResultSet rs, int index, PropertyDescriptor pd)
throws SQLException {
return JdbcUtils.getResultSetValue(rs, index, pd.getPropertyType());
}
}