自学内容网 自学内容网

前端不填写时间 后端写入数据库的时候利用mybatis自动填入修改时间和创建时间

前端修改或者添加页面

image.png

后端数据库表数据

image.png

后端实体类字段
package com.orchids.springmybatisplus.model.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;

import com.orchids.springmybatisplus.model.enums.Sex;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

/**
 * @Author qwh
 * @Date 2024/5/31 16:53
 */
@Data
@Schema(description = "学生信息表")
@TableName(value = "student")
public class Student extends BaseEntity {
    private static final long serialVersionUID = 1L;

    @Schema(description = "名字")
    @TableField(value = "name")
    private String name;

    @Schema(description = "年龄")
    @TableField(value = "age")
    private Integer age;

    @Schema(description = "性别")
    @TableField(value = "sex")
    private Sex sex;

}

后端实体类基类
package com.orchids.springmybatisplus.model.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;

/**
 * @Author qwh
 * @Date 2024/5/31 16:54
 */
@Data
public class BaseEntity implements Serializable {

    @Schema(description = "主键")
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    @Schema(description = "创建时间")
    @TableField(value = "create_time")
    @JsonIgnore
    private Date createTime;

    @Schema(description = "更新时间")
    @TableField(value = "update_time")
    @JsonIgnore
    private Date updateTime;

    @Schema(description = "逻辑删除")
    @JsonIgnore
    @TableLogic()
    @TableField("is_deleted")
    private Byte isDeleted;

}

解决方法的在基类 @TableField()注解内添加条件 fill = FieldFill.INSERT

package com.orchids.springmybatisplus.model.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;

/**
 * @Author qwh
 * @Date 2024/5/31 16:54
 */
@Data
public class BaseEntity implements Serializable {

    @Schema(description = "主键")
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    @Schema(description = "创建时间")
    @TableField(value = "create_time",fill = FieldFill.INSERT)
    @JsonIgnore
    private Date createTime;

    @Schema(description = "更新时间")
    @TableField(value = "update_time",fill = FieldFill.UPDATE)
    @JsonIgnore
    private Date updateTime;

    @Schema(description = "逻辑删除")
    @JsonIgnore
    @TableLogic()
    @TableField("is_deleted")
    private Byte isDeleted;

}


原文地址:https://blog.csdn.net/qq_62383709/article/details/139363247

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!