表结构中updated_time设计为ON UPDATE CURRENT_TIMESTAMP时,使用过程的一个坑

一、mysql表结构中存在如下设计时

表结构中updated_time设计为ON UPDATE CURRENT_TIMESTAMP时,如下

`updated_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间';

二、使用过程的一个坑即存在的问题:

当update整个dao实体对象时,又没有将该对象中的updateTime字段设置为null或new date();

  update time不会更新为最新时间

三、解决方式
1.调用xxxDAO的update方法时,update的对象中存在update time(更新时间)时,请手动set一个 new Date() 或 null;

2.  BeanUtils.copyProperties(form, tConsultant,"updateTime"); copy的复制可以指定要忽略的updateTime字段

spring中该方法的源码如下:

/**
     * Copy the property values of the given source bean into the given target bean,
     * ignoring the given "ignoreProperties".
     * <p>Note: The source and target classes do not have to match or even be derived
     * from each other, as long as the properties match. Any bean properties that the
     * source bean exposes but the target bean does not will silently be ignored.
     * <p>This is just a convenience method. For more complex transfer needs,
     * consider using a full BeanWrapper.
     * @param source the source bean
     * @param target the target bean
     * @param ignoreProperties array of property names to ignore
     * @throws BeansException if the copying failed
     * @see BeanWrapper
     */
    public static void copyProperties(Object source, Object target, String... ignoreProperties) throws BeansException {
        copyProperties(source, target, null, ignoreProperties);
    }

猜你喜欢

转载自www.cnblogs.com/756623607-zhang/p/10420697.html