CustomJdbcTemplateRowMapper.java
3.58 KB
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
package com.taover.repository.mapper;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.ResultSet;
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 org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.rowset.ResultSetWrappingSqlRowSetMetaData;
public class CustomJdbcTemplateRowMapper <E> implements RowMapper<E>{
Log log = LogFactory.getLog(this.getClass());
private Map<String, Field> beanFielNameToField = new HashMap<String, Field>();
private Method beforeMethod = null;
private Method afterMethod = null;
private Class<E> classInfo;
private Map<String, String> tableToBeanMap;
public CustomJdbcTemplateRowMapper(Class<E> classInfo, Map<String, String> tableToBeanMap) {
this.classInfo = classInfo;
this.tableToBeanMap = tableToBeanMap;
Field[] fields = this.classInfo.getDeclaredFields();
for(Field item: fields) {
try {
item.setAccessible(true);
this.beanFielNameToField.put(item.getName(), item);
}catch (Exception e) {
log.error("set field accessible:"+e.getMessage());
}
}
boolean hasImplementPointCut = false;
Class[] interfaceArr = this.classInfo.getInterfaces();
for(int i=0; i<interfaceArr.length; ++i){
if(interfaceArr[i].getName().equals("com.taover.repository.advice.EntityPointCut")){
hasImplementPointCut = true;
break;
}
}
if(hasImplementPointCut){
try{
beforeMethod = this.classInfo.getDeclaredMethod("before");
beforeMethod.setAccessible(true);
}catch(Exception e){
log.error("set before method exception:"+e.getMessage());
}
try{
afterMethod = this.classInfo.getDeclaredMethod("after");
afterMethod.setAccessible(true);
}catch(Exception e){
log.error("set after method exception:"+e.getMessage());
}
}
}
@Override
public E mapRow(ResultSet rs, int index) throws SQLException {
E targetObj;
try {
targetObj = this.classInfo.newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
if(this.beforeMethod != null){
try{
beforeMethod.invoke(targetObj);
}catch(Exception e){
log.error("invoke before exception:"+e.getMessage());
}
}
ResultSetWrappingSqlRowSetMetaData wapping = new ResultSetWrappingSqlRowSetMetaData(rs.getMetaData());
int columnCount = wapping.getColumnCount();
for (int i = 1; i<=columnCount; i++) {
String tableFieldName = wapping.getColumnLabel(i);
String beanFieldName = this.tableToBeanMap.get(tableFieldName);
Object value = rs.getObject(i);
Field beanField = this.beanFielNameToField.get(beanFieldName);
if(null == value || beanFieldName == null && beanField == null){
continue;
}
try {
beanField.set(targetObj, value);
} catch (IllegalArgumentException e) {
if("Integer".equals(value.getClass().getSimpleName()) && "Long".equals(beanField.getType().getSimpleName())) {
try {
beanField.set(targetObj, Long.valueOf(value.toString()));
} catch (IllegalArgumentException | IllegalAccessException e1) {
log.error("map set object field error -> Integer convert Long:"+e.getMessage());
}
}else {
log.error("map set object field error:"+e.getMessage());
}
} catch (IllegalAccessException e) {
log.error("map set object field error:"+e.getMessage());
}
}
if(this.afterMethod != null){
try {
afterMethod.invoke(targetObj);
}catch (Exception e) {
log.error("invoke after exception:"+e.getMessage());
}
}
return targetObj;
}
}