當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
基于SpringJDBC 实现关键功能-ClassMappings
生活随笔
收集整理的這篇文章主要介紹了
基于SpringJDBC 实现关键功能-ClassMappings
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
public 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);elsename = 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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 搭建基础架构-Order
- 下一篇: 基于SpringJDBC 实现关键功能-