复制代码
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
99public class ClassMappings { private ClassMappings(){} static final Set<Class<?>> SUPPORTED_SQL_OBJECTS = new HashSet<Class<?>>(); static { //只要这里写了的,默认支持自动类型转换 Class<?>[] classes = { boolean.class, Boolean.class, short.class, Short.class, int.class, Integer.class, long.class, Long.class, float.class, Float.class, double.class, Double.class, String.class, Date.class, Timestamp.class, BigDecimal.class }; SUPPORTED_SQL_OBJECTS.addAll(Arrays.asList(classes)); } static boolean isSupportedSQLObject(Class<?> clazz) { return clazz.isEnum() || SUPPORTED_SQL_OBJECTS.contains(clazz); } public static Map<String, Method> findPublicGetters(Class<?> clazz) { Map<String, Method> map = new HashMap<String, Method>(); Method[] methods = clazz.getMethods(); for (Method method : methods) { if (Modifier.isStatic(method.getModifiers())) continue; if (method.getParameterTypes().length != 0) continue; if (method.getName().equals("getClass")) continue; Class<?> returnType = method.getReturnType(); if (void.class.equals(returnType)) continue; if(!isSupportedSQLObject(returnType)){ continue; } if ((returnType.equals(boolean.class) || returnType.equals(Boolean.class)) && method.getName().startsWith("is") && method.getName().length() > 2) { map.put(getGetterName(method), method); continue; } if ( ! method.getName().startsWith("get")) continue; if (method.getName().length() < 4) continue; map.put(getGetterName(method), method); } return map; } public static Field[] findFields(Class<?> clazz){ return clazz.getDeclaredFields(); } public static Map<String, Method> findPublicSetters(Class<?> clazz) { Map<String, Method> map = new HashMap<String, Method>(); Method[] methods = clazz.getMethods(); for (Method method : methods) { if (Modifier.isStatic(method.getModifiers())) continue; if ( ! void.class.equals(method.getReturnType())) continue; if (method.getParameterTypes().length != 1) continue; if ( ! method.getName().startsWith("set")) continue; if (method.getName().length() < 4) continue; if(!isSupportedSQLObject(method.getParameterTypes()[0])){ continue; } map.put(getSetterName(method), method); } return map; } public static String getGetterName(Method getter) { String name = getter.getName(); if (name.startsWith("is")) name = name.substring(2); else name = name.substring(3); return Character.toLowerCase(name.charAt(0)) + name.substring(1); } private static String getSetterName(Method setter) { String name = setter.getName().substring(3); return Character.toLowerCase(name.charAt(0)) + name.substring(1); } }
最后
以上就是丰富钢笔最近收集整理的关于基于SpringJDBC 实现关键功能-ClassMappings的全部内容,更多相关基于SpringJDBC内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复