zoukankan      html  css  js  c++  java
  • MybatisPlus自动填充时间

    MybatisPlus自动填充时间

    基于这篇博客修改

    [整合MybatisPlus测试]

    数据库新建两个时间字段

    user类

    package com.xiang.pojo;
    
    import com.baomidou.mybatisplus.annotation.FieldFill;
    import com.baomidou.mybatisplus.annotation.IdType;
    import com.baomidou.mybatisplus.annotation.TableField;
    import com.baomidou.mybatisplus.annotation.TableId;
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    import java.util.Date;
    
    /**
     * Created by IntelliJ IDEA.
     * User: xiang
     * Date: 2021/10/21 2:50
     */
    
    /**
     * `id`       int NOT NULL AUTO_INCREMENT,
     * `username` varchar(12) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
     * `sex`      varchar(2) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
     * `age`      int NULL DEFAULT NULL,
     * `birthday` date NULL DEFAULT NULL,
     */
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class User {
        //    @TableId(type = IdType.ID_WORKER) //数字类型的id使用,当类型为Long时默认使用,生成19位的Id值,
    //    @TableId(type = IdType.ID_WORKER_STR)//字符串类型的id使用,需要写明注解,生成19位的Id值
        //同时需要设置数据库的字段id的类型为Bigint,因为int的长度只有11位
    
        private Long id;
        private String username;
        private String sex;
        private int age;
        private Date birthday;
    
        /***
         * 使用mybatisPlus自动填充时间
         *
         *     第一:添加注解
         *     第二:实现MetaObjectHandler接口
         *     第三:重写inserFill和updateFill方法
         *     第四:调用setFieldValByName方法
         */
    
        @TableField(fill = FieldFill.INSERT)//进行添加操作时有值
        private Date createDate;
    
        @TableField(fill = FieldFill.INSERT_UPDATE)////进行添加和修改操作时有值
        private Date updateDate;
    }
    
    
    

    创建MeatObjectHandler类

    package com.xiang.handler;
    
    import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
    import org.apache.ibatis.reflection.MetaObject;
    import org.springframework.stereotype.Component;
    
    import java.util.Date;
    
    /**
     * Created by IntelliJ IDEA.
     * User: xiang
     * Date: 2021/10/22 22:50
     */
    @Component
    public class MeatObjectHandler implements MetaObjectHandler {
    
    
        @Override
        //使用MybatisPlus实现添加操作时,该方法会执行
        public void insertFill(MetaObject metaObject) {
            //参数:需要设置的属性;设置的时间;元数据(理解:表中的数据)
            this.setFieldValByName("createTime", new Date(), metaObject);
            this.setFieldValByName("updateTime", new Date(), metaObject);
    
        }
    
        @Override
        //使用MybatisPlus实现修改操作时,该方法会执行
        public void updateFill(MetaObject metaObject) {
            this.setFieldValByName("updateTime", new Date(), metaObject);
        }
    }
    
    

    测试类

    package com.xiang;
    
    import com.xiang.mapper.UserMapper;
    import com.xiang.pojo.User;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    
    import java.util.Date;
    import java.util.List;
    
    /**
     * Created by IntelliJ IDEA.
     * User: xiang
     * Date: 2021/10/22 23:10
     */
    @SpringBootTest
    public class FillingTime {
        @Autowired
        UserMapper userMapper;
    
        /**
         * MybatisPlus自动填充时间
         */
    
        @Test
        /**
         * 插入
         */
        public void insertTime() {
            User user = new User();
            user.setUsername("向向");
            user.setSex("女");
            user.setAge(20);
            user.setBirthday(new Date());
           /* user.setCreateDate(new Date());
            user.setUpdateDate(new Date());*/
    
            int insert = userMapper.insert(user);
            if (insert > 0) {
                System.out.println("插入成功");
            } else {
                System.out.println("fail");
            }
            List<User> list = userMapper.selectList(null);
            for (User listUser : list) {
                System.out.println(listUser);
            }
        }
    
    
        @Test
        /**
         * 修改
         */
        void updateTime() {
            User user = new User();
            user.setId(1451573602674176001l);
            user.setAge(18);
            userMapper.updateById(user);
            List<User> list = userMapper.selectList(null);
            for (User listUser : list) {
                System.out.println(listUser);
            }
        }
    }
    
    

    运行结果

    插入
    JDBC Connection [HikariProxyConnection@1979825302 wrapping com.mysql.cj.jdbc.ConnectionImpl@48a0c8aa] will not be managed by Spring
    ==>  Preparing: INSERT INTO user ( id, username, sex, age, birthday, create_time, update_time ) VALUES ( ?, ?, ?, ?, ?, ?, ? )
    ==> Parameters: 1451573602674176001(Long), 向向(String), 女(String), 20(Integer), 2021-10-22 23:38:10.417(Timestamp), 2021-10-22 23:38:10.468(Timestamp), 2021-10-22 23:38:10.468(Timestamp)
    <==    Updates: 1
    Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2da3b078]
    插入成功
    JDBC Connection [HikariProxyConnection@1578026015 wrapping com.mysql.cj.jdbc.ConnectionImpl@48a0c8aa] will not be managed by Spring
    ==>  Preparing: SELECT id,username,sex,age,birthday,create_time,update_time FROM user
    ==> Parameters: 
    <==    Columns: id, username, sex, age, birthday, create_time, update_time
    <==        Row: 1, xiang, 男, 18, 2021-10-03, null, null
    <==        Row: 559, 小向, 男, 18, 2021-10-04, null, null
    <==        Row: 602, admin, 女, 18, 2021-10-20, null, null
    <==        Row: 603, testbox, 女, 18, 2021-10-02, null, null
    <==        Row: 604, 小一, 男, 18, 2021-10-16, null, null
    <==        Row: 605, 小二, 女, 18, null, null, null
    <==        Row: 607, 小四, 女, 21, null, null, null
    <==        Row: 609, 小五, 女, 0, null, null, null
    <==        Row: 1451097869404868609, 向某, 男, 0, null, null, null
    <==        Row: 1451098975287668738, 周某, 男, 0, null, null, null
    <==        Row: 1451572730934198273, 向向, 女, 20, 2021-10-22, 2021-10-22 23:34:43, 2021-10-22 23:34:43
    <==        Row: 1451573602674176001, 向向, 女, 20, 2021-10-22, 2021-10-22 23:38:10, 2021-10-22 23:38:10
    <==      Total: 12
    
    修改
    JDBC Connection [HikariProxyConnection@1986001684 wrapping com.mysql.cj.jdbc.ConnectionImpl@57562473] will not be managed by Spring
    ==>  Preparing: UPDATE user SET age=?, update_time=? WHERE id=?
    ==> Parameters: 18(Integer), 2021-10-22 23:46:10.247(Timestamp), 1451573602674176001(Long)
    <==    Updates: 1
    JDBC Connection [HikariProxyConnection@173070089 wrapping com.mysql.cj.jdbc.ConnectionImpl@57562473] will not be managed by Spring
    ==>  Preparing: SELECT id,username,sex,age,birthday,create_time,update_time FROM user
    ==> Parameters: 
    <==    Columns: id, username, sex, age, birthday, create_time, update_time
    <==        Row: 1, xiang, 男, 18, 2021-10-03, null, null
    <==        Row: 559, 小向, 男, 18, 2021-10-04, null, null
    <==        Row: 602, admin, 女, 18, 2021-10-20, null, null
    <==        Row: 603, testbox, 女, 18, 2021-10-02, null, null
    <==        Row: 604, 小一, 男, 18, 2021-10-16, null, null
    <==        Row: 605, 小二, 女, 18, null, null, null
    <==        Row: 607, 小四, 女, 21, null, null, null
    <==        Row: 609, 小五, 女, 0, null, null, null
    <==        Row: 1451097869404868609, 向某, 男, 0, null, null, null
    <==        Row: 1451098975287668738, 周某, 男, 0, null, null, null
    <==        Row: 1451572730934198273, 向向, 女, 20, 2021-10-22, 2021-10-22 23:34:43, 2021-10-22 23:34:43
    <==        Row: 1451573602674176001, 向向, 女, 18, 2021-10-22, 2021-10-22 23:38:10, 2021-10-22 23:46:10
    <==      Total: 12
    
  • 相关阅读:
    图解测试之稳定性-如何开始稳定性测试
    系统稳定性保障
    系统稳定性评测
    分布式架构的架构稳定性
    app测试--稳定性测试
    服务器稳定性测试方法汇总
    服务端稳定性测试
    发票问题
    android x86 固件定制
    Nim游戏博弈(收集完全版)
  • 原文地址:https://www.cnblogs.com/d534/p/15441085.html
Copyright © 2011-2022 走看看