DynamicUpdate注解

DynamicUpdate注解的作用

mysql中有一个字段 updatetime

想每次操作更改数据表的时候更改update字段

首先修改该字段的属性
Alter Table product_category MODIFY COLUMN update_time TIMESTAMP NOT null DEFAULT CURRENT_TIMESTAMP
on UPDATE CURRENT_TIMESTAMP

在实体类在加入@updatetime注解

在测试单元中添加对字段的修改,这个自动一定要修改过,不然时间不会更改;

代码:
测试方法
 @Test
    public void insterProduct(){
        Optional<ProductCategory> productCategory=productCategoryRepository.findById(1);
        ProductCategory product=productCategory.get();
        System.out.println("************"+product.toString());
        product.setCategoryType(10);
        productCategoryRepository.save(product);

        log信息:Hibernate: update product_category set category_type=? where category_id=?

        需要注意 DynamicUpdate注解默认为true

        源码:

        @Target({ElementType.TYPE})
       @Retention(RetentionPolicy.RUNTIME)
        public @interface DynamicUpdate {
    boolean value() default true;
}
        当把属性值更改为false时
       log信息:Hibernate Hibernate: select productcat0_.category_id as category1_0_0_, productcat0_.category_name as category2_0_0_, productcat0_.category_type as category3_0_0_, productcat0_.create_time as create_t4_0_0_, productcat0_.update_time as update_t5_0_0_ from product_category productcat0_ where productcat0_.category_id=?
       此时只修改mysql字段为非空的属性,自动更新的字段updatetime不会更新
       

猜你喜欢

转载自blog.csdn.net/sinat_32856935/article/details/82050766