重写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);
}
}
}
}
}
}
}
重写org.springframework.beans.BeanUtils的copyProperties方法,能在实体映射的时候把纯数字格式的日期转格式的更多相关文章
- org.springframework.beans.BeanUtils与org.apache.commons.beanutils.BeanUtils的copyProperties用法区别
知识点 org.springframework.beans.BeanUtils与org.apache.commons.beanutils.BeanUtils都提供了copyProperties方法,作 ...
- org.springframework.beans.BeanUtils
org.springframework.beans.BeanUtils的一个demo.可以很优雅的实现将父类字段的值copy到子类中 下面例子的输出结果(子类使用父类的toString方法,有点意思吧 ...
- BeanUtils的copyproPerties方法的用法
转自:Hassan Blog的博客 一.简介: BeanUtils提供对Java反射和自省API的包装.其主要目的是利用反射机制对JavaBean的属性进行处理.我们知道,一个JavaBean通常包 ...
- org.springframework.beans.BeanUtils的用法
s,t为对象BeanUtils.copyProperties(s, t); 将给定源bean的属性值复制到目标bean中 public static void copyProperties(Obje ...
- BeanUtils工具类copyProperties方法缺点及解决
使用类为spring-beans:4.3.13release包中的 org.springframework.beans.BeanUtils BeanUtils.copyProperties(Objec ...
- 解决org.openqa.selenium.WebDriverException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms org.springframework.beans.BeanInstantiation
解决方法为将selenium-server-standalone-2.37.0.jar升级至selenium-server-standalone-2.41.0.jar即可. 下载地址:http://s ...
- 开发Spring过程中几个常见异常(二):Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'a' define
本异常是小编在运行自己另外一篇博文中的例子时遇到的.(附博文:http://www.cnblogs.com/dudududu/p/8482487.html) 完整异常信息: 警告: Exception ...
- springmvc上传文件报错org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.web.multipart.MultipartFile]
在用springmvc+mybatis进行项目开发时,上传文件抛异常... org.springframework.beans.BeanInstantiationException: Could no ...
- 【spring mvc】后台spring mvc接收List参数报错如下:org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.util.List]: Specified class is an interface
后台spring mvc接收List参数报错如下:org.springframework.beans.BeanInstantiationException: Failed to instantiate ...
- org.springframework.beans.factory.BeanCreationException: 求教育!
2014-11-26 14:05:56 [org.springframework.web.context.support.XmlWebApplicationContext]-[WARN] Except ...
随机推荐
- vue组件传值 | 子父组件
一.子父组件 1.子组件获得父组件中的值:通过v-bind方式绑定在子组件中 // 子组件中定义propsprops: ['msg'] // 父组件中: <deleteBar :msg='thi ...
- mongodb对Obeject对象进行增删改操作
1.插入某个对象,如User对象 mongoTemplate.insert(user, "users");//第一个参数是实体类对象User, 第二个参数是mongodb对应的集合 ...
- 多线程事务回滚sqlSession, spring-mybatis 开启事务
@Resource SqlContext sqlContext; /** * 多线程事务. * @param employeeDOList */ @Override public void saveT ...
- HTML弹窗设计二
<!DOCTYPE html><html> <head> <title>模态框弹出层.html</title> <meta http- ...
- js内置方法
数组: 1.push()数组最后添加元素,pop()数组删除最后一个: unshift()数组开头添加元素,shift()删除数字第一个: 注意:push()和unshift()可以添加 ...
- XJTUOJ #1323. [L2-2]三倍满世界
题目 https://oj.xjtuicpc.com/problem/1323 思路 题目说明有点绕. 然后我们仔细观察一下,这个东西很显然是一个天然的树形结构. 我们可以把一个空间(或者一个数)当成 ...
- CF850F 题解
题意 传送门 有一袋 \(n\) 个颜色球,第 \(i\) 个颜色的球有 \(a_i\) 个. 当袋子里至少有两个不同颜色的球时,执行以下步骤: 一个接一个的按照顺序随机取出两个的球,这些球的颜色可能 ...
- 【C学习笔记】day2-3 求10 个整数中最大值
#include <stdio.h>#define n 10 int main() { int max=0; int a[n] = {12,15,16,546,165,654,612,23 ...
- Python_QT_量化投资/量化交易 基础入门课
量化投资/量化交易 基础入门课 https://www.bilibili.com/video/BV1n4411q7ic?from=search&seid=1127232108574476398 ...
- 【python_PAT_乙类】1013_数素数 ,Python运行超时解决方案
题目: 令 Pi 表示第 i 个素数.现任给两个正整数 M≤N≤104,请输出 PM 到 PN 的所有素数. 输入格式: 输入在一行中给出 M 和 N,其间以空格分隔. 输出格 ...