Compartilhamento de tecnologia

MybatisPlus implementa configuração automática de tempo para inserção/modificação de dados

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

introdução

Ao inserir dados, a hora atual é definida automaticamente e, quando os dados são atualizados, a data é automaticamente modificada para a data da modificação.

Use a interface estendida MetaObjectHandler do MybatisPlus

etapa

  1. implementar interface

  2. Anotação de classe de entidade

implementar interface

package com.example.vueelementson.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.util.Date;

/**
 * @BelongsProject: blog-springboot
 * @BelongsPackage: com.example.vueelementson.common
 * @Author: Zww
 * @CreateTime: 2024-07-07  16:38
 * @Description: TODO
 * @Version: 1.0
 */
@Component
public class ExtraDataHandle implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        // 第一个值填入你要设置的字段名,第二个值为每次自动填充的值
        metaObject.setValue("publishTime", new Date());
        metaObject.setValue("updateTime", new Date());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateTime", new Date(), metaObject);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

Adicionar anotação

imagem-20240707175012082

No entanto, a data aqui é fornecida ao front end em formato de carimbo de data/hora.

imagem-20240707181308362

, adicionamos um js para conversão de timestamp no front end, introduzimos e chamamos no método

Carimbo de data e hora JS para código de tempo

// 时间戳:1637244864707
/* 时间戳转换为时间 */
export default {
    timestampToTime(value, type = 0) {
        var time = new Date(value);// 2023-07-07 17:07:44
        var year = time.getFullYear();// 2023年
        var month = time.getMonth() + 1; // 0-11 +1表示1-12月
        var date = time.getDate();// 1-31号
        var hour = time.getHours();// 0-23时
        var minute = time.getMinutes();// 0-59分
        var second = time.getSeconds();// 0-59秒
        month = month < 10 ? "0" + month : month;// 如果小于10的月份,前面补0
        date = date < 10 ? "0" + date : date;
        hour = hour < 10 ? "0" + hour : hour;
        minute = minute < 10 ? "0" + minute : minute;
        second = second < 10 ? "0" + second : second;
        var arr = [
            year + "-" + month + "-" + date,
            year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second,
            year + "年" + month + "月" + date,
            year + "年" + month + "月" + date + "日" + " " + hour + ":" + minute + ":" + second,
            hour + ":" + minute + ":" + second
        ]
        return arr[type];
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

Use este js

imagem-20240707184712328

resultado