ocgn

导航

重写org.springframework.beans.BeanUtils的copyProperties方法,能在实体映射的时候把纯数字格式的日期转格式

就是在拷贝的时候加个正则的校验,如果是纯数字的日期 就转成yyyy-MM-dd HH:mm:ss的格式
原本想直接用注解在实体转格式,但是那样实体会变成日期格式,所以放弃了,直接重写拷贝的方法比较简单

public class BeanUtilsEx extends BeanUtils {
/**
* 从org.springframework.beans.BeanUtils类中直接复制过来
* @param source
* @param target
* @throws BeansException
*/
public static void copyProperties(Object source, Object target) throws BeansException {
copyProperties(source, target, null, (String[]) null);
}

/**
* 重写方法,如果传入参数是纯数字的日期,转换后赋值
* @param source
* @param target
* @throws BeansException
*/
private static void copyProperties(Object source, Object target, Class<?> editable, String... ignoreProperties)
throws BeansException {

Assert.notNull(source, "Source must not be null");
Assert.notNull(target, "Target must not be null");
//年月日时分秒
String regexp1 = "^([0-9]{4})(0?[1-9]|1[0-2])((0?[1-9])|((1|2)[0-9])|30|31)([0-2][0-9])([0-6][0-9])([0-6][0-9])$";
//年月日
String regexp2 = "^([0-9]{4})(0?[1-9]|1[0-2])((0?[1-9])|((1|2)[0-9])|30|31)$";
//年月
String regexp3 = "^([0-9]{4})(0?[1-9]|1[0-2])$";



Class<?> actualEditable = target.getClass();
if (editable != null) {
if (!editable.isInstance(target)) {
throw new IllegalArgumentException("Target class [" + target.getClass().getName() +
"] not assignable to Editable class [" + editable.getName() + "]");
}
actualEditable = editable;
}
PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);
List<String> ignoreList = (ignoreProperties != null ? Arrays.asList(ignoreProperties) : null);

for (PropertyDescriptor targetPd : targetPds) {
Method writeMethod = targetPd.getWriteMethod();
if (writeMethod != null && (ignoreList == null || !ignoreList.contains(targetPd.getName()))) {
PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName());
if (sourcePd != null) {
Method readMethod = sourcePd.getReadMethod();
if (readMethod != null &&
ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) {
try {
if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
readMethod.setAccessible(true);
}
Object value = readMethod.invoke(source);
//将纯数字时间格式转换为标准格式
if (value != null) {
String dateStr = (String) value;
if (dateStr.matches(regexp1)) {
Date date = DateUtils.getDate(dateStr, "yyyyMMddHHmmss");
String dateFormat = DateUtils.formatDate(date, "yyyy-MM-dd HH:mm:ss");
value = dateFormat;
}
if (dateStr.matches(regexp2)) {
Date date = DateUtils.getDate(dateStr, "yyyyMMdd");
String dateFormat = DateUtils.formatDate(date, "yyyy-MM-dd");
value = dateFormat;
}
if (dateStr.matches(regexp3)) {
Date date = DateUtils.getDate(dateStr, "yyyyMM");
String dateFormat = DateUtils.formatDate(date, "yyyy-MM");
value = dateFormat;
}
}
if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
writeMethod.setAccessible(true);
}
writeMethod.invoke(target, value);
}
catch (Throwable ex) {
throw new FatalBeanException(
"Could not copy property '" + targetPd.getName() + "' from source to target", ex);
}
}
}
}
}
}


}

posted on 2021-02-05 17:07  ocgn  阅读(275)  评论(0编辑  收藏  举报