表結構中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);
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM