• 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
    
  • 相关阅读:
    重新想象 Windows 8 Store Apps (15) 控件 UI: 字体继承, Style, ControlTemplate, SystemResource, VisualState, VisualStateManager
    重新想象 Windows 8 Store Apps (12) 控件之 GridView 特性: 拖动项, 项尺寸可变, 分组显示
    返璞归真 asp.net mvc (10) asp.net mvc 4.0 新特性之 Web API
    与众不同 windows phone (29) Communication(通信)之与 OData 服务通信
    与众不同 windows phone (33) Communication(通信)之源特定组播 SSM(Source Specific Multicast)
    与众不同 windows phone (27) Feature(特性)之搜索的可扩展性, 程序的生命周期和页面的生命周期, 页面导航, 系统状态栏
    与众不同 windows phone (30) Communication(通信)之基于 Socket TCP 开发一个多人聊天室
    返璞归真 asp.net mvc (12) asp.net mvc 4.0 新特性之移动特性
    重新想象 Windows 8 Store Apps (2) 控件之按钮控件: Button, HyperlinkButton, RepeatButton, ToggleButton, RadioButton, CheckBox, ToggleSwitch
    重新想象 Windows 8 Store Apps (10) 控件之 ScrollViewer 特性: Chaining, Rail, Inertia, Snap, Zoom
  • 原文地址:https://www.cnblogs.com/d534/p/15441085.html
Copyright © 2020-2023  润新知