|
@@ -11,31 +11,32 @@ import java.util.regex.Pattern;
|
|
*
|
|
*
|
|
* @author hx
|
|
* @author hx
|
|
*/
|
|
*/
|
|
-public class BeanUtils extends org.springframework.beans.BeanUtils
|
|
|
|
-{
|
|
|
|
- /** Bean方法名中属性名开始的下标 */
|
|
|
|
|
|
+public class BeanUtils extends org.springframework.beans.BeanUtils {
|
|
|
|
+ /**
|
|
|
|
+ * Bean方法名中属性名开始的下标
|
|
|
|
+ */
|
|
private static final int BEAN_METHOD_PROP_INDEX = 3;
|
|
private static final int BEAN_METHOD_PROP_INDEX = 3;
|
|
|
|
|
|
- /** * 匹配getter方法的正则表达式 */
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 匹配getter方法的正则表达式
|
|
|
|
+ */
|
|
private static final Pattern GET_PATTERN = Pattern.compile("get(\\p{javaUpperCase}\\w*)");
|
|
private static final Pattern GET_PATTERN = Pattern.compile("get(\\p{javaUpperCase}\\w*)");
|
|
|
|
|
|
- /** * 匹配setter方法的正则表达式 */
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 匹配setter方法的正则表达式
|
|
|
|
+ */
|
|
private static final Pattern SET_PATTERN = Pattern.compile("set(\\p{javaUpperCase}\\w*)");
|
|
private static final Pattern SET_PATTERN = Pattern.compile("set(\\p{javaUpperCase}\\w*)");
|
|
|
|
|
|
/**
|
|
/**
|
|
* Bean属性复制工具方法。
|
|
* Bean属性复制工具方法。
|
|
*
|
|
*
|
|
* @param dest 目标对象
|
|
* @param dest 目标对象
|
|
- * @param src 源对象
|
|
|
|
|
|
+ * @param src 源对象
|
|
*/
|
|
*/
|
|
- public static void copyBeanProp(Object dest, Object src)
|
|
|
|
- {
|
|
|
|
- try
|
|
|
|
- {
|
|
|
|
|
|
+ public static void copyBeanProp(Object dest, Object src) {
|
|
|
|
+ try {
|
|
copyProperties(src, dest);
|
|
copyProperties(src, dest);
|
|
- }
|
|
|
|
- catch (Exception e)
|
|
|
|
- {
|
|
|
|
|
|
+ } catch (Exception e) {
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -46,8 +47,7 @@ public class BeanUtils extends org.springframework.beans.BeanUtils
|
|
* @param obj 对象
|
|
* @param obj 对象
|
|
* @return 对象的setter方法列表
|
|
* @return 对象的setter方法列表
|
|
*/
|
|
*/
|
|
- public static List<Method> getSetterMethods(Object obj)
|
|
|
|
- {
|
|
|
|
|
|
+ public static List<Method> getSetterMethods(Object obj) {
|
|
// setter方法列表
|
|
// setter方法列表
|
|
List<Method> setterMethods = new ArrayList<Method>();
|
|
List<Method> setterMethods = new ArrayList<Method>();
|
|
|
|
|
|
@@ -56,11 +56,9 @@ public class BeanUtils extends org.springframework.beans.BeanUtils
|
|
|
|
|
|
// 查找setter方法
|
|
// 查找setter方法
|
|
|
|
|
|
- for (Method method : methods)
|
|
|
|
- {
|
|
|
|
|
|
+ for (Method method : methods) {
|
|
Matcher m = SET_PATTERN.matcher(method.getName());
|
|
Matcher m = SET_PATTERN.matcher(method.getName());
|
|
- if (m.matches() && (method.getParameterTypes().length == 1))
|
|
|
|
- {
|
|
|
|
|
|
+ if (m.matches() && (method.getParameterTypes().length == 1)) {
|
|
setterMethods.add(method);
|
|
setterMethods.add(method);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -75,18 +73,15 @@ public class BeanUtils extends org.springframework.beans.BeanUtils
|
|
* @return 对象的getter方法列表
|
|
* @return 对象的getter方法列表
|
|
*/
|
|
*/
|
|
|
|
|
|
- public static List<Method> getGetterMethods(Object obj)
|
|
|
|
- {
|
|
|
|
|
|
+ public static List<Method> getGetterMethods(Object obj) {
|
|
// getter方法列表
|
|
// getter方法列表
|
|
List<Method> getterMethods = new ArrayList<Method>();
|
|
List<Method> getterMethods = new ArrayList<Method>();
|
|
// 获取所有方法
|
|
// 获取所有方法
|
|
Method[] methods = obj.getClass().getMethods();
|
|
Method[] methods = obj.getClass().getMethods();
|
|
// 查找getter方法
|
|
// 查找getter方法
|
|
- for (Method method : methods)
|
|
|
|
- {
|
|
|
|
|
|
+ for (Method method : methods) {
|
|
Matcher m = GET_PATTERN.matcher(method.getName());
|
|
Matcher m = GET_PATTERN.matcher(method.getName());
|
|
- if (m.matches() && (method.getParameterTypes().length == 0))
|
|
|
|
- {
|
|
|
|
|
|
+ if (m.matches() && (method.getParameterTypes().length == 0)) {
|
|
getterMethods.add(method);
|
|
getterMethods.add(method);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -103,8 +98,30 @@ public class BeanUtils extends org.springframework.beans.BeanUtils
|
|
* @return 属性名一样返回true,否则返回false
|
|
* @return 属性名一样返回true,否则返回false
|
|
*/
|
|
*/
|
|
|
|
|
|
- public static boolean isMethodPropEquals(String m1, String m2)
|
|
|
|
- {
|
|
|
|
|
|
+ public static boolean isMethodPropEquals(String m1, String m2) {
|
|
return m1.substring(BEAN_METHOD_PROP_INDEX).equals(m2.substring(BEAN_METHOD_PROP_INDEX));
|
|
return m1.substring(BEAN_METHOD_PROP_INDEX).equals(m2.substring(BEAN_METHOD_PROP_INDEX));
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 将实体类转换为Vo类
|
|
|
|
+ *
|
|
|
|
+ * @param list
|
|
|
|
+ * @param clazz
|
|
|
|
+ * @param <T>
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static <T> List<T> entityListToVOList(List<?> list, Class<T> clazz) {
|
|
|
|
+ List<T> result = new ArrayList<>(list.size());
|
|
|
|
+ for (Object source : list) {
|
|
|
|
+ T target;
|
|
|
|
+ try {
|
|
|
|
+ target = clazz.getDeclaredConstructor().newInstance();
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new RuntimeException();
|
|
|
|
+ }
|
|
|
|
+ BeanUtils.copyProperties(source, target);
|
|
|
|
+ result.add(target);
|
|
|
|
+ }
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
}
|
|
}
|