zoukankan      html  css  js  c++  java
  • mybatisPlus自动填充更新时间

    1.实体类上的注解

    @NoArgsConstructor
    @AllArgsConstructor
    @Data
    public class User {
    
        @TableId(type = IdType.AUTO)
        private Long id;
        private String name;
        private Integer age;
        private String email;
        @TableField(fill = FieldFill.INSERT)
        private Date createTime;
        @TableField(fill = FieldFill.INSERT_UPDATE)
        private Date updateTime;
    }

    2.对注解的处理

    @Component
    @Slf4j
    public class MyDetaObjectHander implements MetaObjectHandler {
        @Override
        public void insertFill(MetaObject metaObject) {
            log.info("come to insert fill .........");
            //setFieldValByName(String fieldName, Object fieldVal, MetaObject metaObject)
            this.setFieldValByName("createTime",new Date(),metaObject);
            this.setFieldValByName("updateTime",new Date(),metaObject);
        }
    
        @Override
        public void updateFill(MetaObject metaObject) {
            log.info("come to update fill .......");
    
            this.setFieldValByName("updateTime",new Date(),metaObject);
    
        }
    }

    3.进行添加和修改测试

     //添加用户
        @Test
        public void addUser(){
            User user = new User();
            user.setAge(23);
            user.setEmail("168@qq.com");
            user.setName("gwq");
            int insert = userDao.insert(user);
            System.out.println(insert);
        }
        @Test
        public void update(){
            User user = new User();
            user.setAge(23);
            user.setEmail("168@qq.com");
            user.setName("gwq");
            user.setId(1299641789988229125L);
    
            int i = userDao.updateById(user);
            System.out.println(i);
        }
  • 相关阅读:
    liunx下忘记mysql的密码
    oracle add_month函数
    oracle的LAST_DAY()函数
    JAVA方法中参数到底是值传递还是引用传递
    list=null和list.size=0的区别
    对python 函数的一些理解
    python 文件流
    html canvas 简单体验
    angular js 指令
    初识 angular js
  • 原文地址:https://www.cnblogs.com/ghwq/p/13583110.html
Copyright © 2011-2022 走看看