欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

mybatis?plus配置自動(dòng)create_time和update_time方式

 更新時(shí)間:2024年09月22日 10:25:44   作者:SuenDanny  
在處理數(shù)據(jù)時(shí),注意時(shí)間類型的轉(zhuǎn)換非常重要,不同編程環(huán)境和數(shù)據(jù)庫(kù)對(duì)時(shí)間數(shù)據(jù)的處理方式各異,因此在數(shù)據(jù)遷移或日常處理中需謹(jǐn)慎處理時(shí)間格式,個(gè)人經(jīng)驗(yàn)表明,了解常用的時(shí)間轉(zhuǎn)換函數(shù)和方法能有效避免錯(cuò)誤,提高工作效率,希望這些經(jīng)驗(yàn)?zāi)転榇蠹規(guī)?lái)幫助

mybatis plus配置自動(dòng)create_time和update_time

注意時(shí)間類型的轉(zhuǎn)化

package com.ruoyi.framework.handler;

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

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * 自定義元對(duì)象處理器,用于在數(shù)據(jù)庫(kù)操作中自動(dòng)填充創(chuàng)建時(shí)間和更新時(shí)間。
 * @Component 注解表示該類是一個(gè)Spring Bean,可以被其他Bean依賴注入。
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    /**
     * 在插入數(shù)據(jù)時(shí)自動(dòng)填充創(chuàng)建時(shí)間和更新時(shí)間。
     * @param metaObject 元對(duì)象,代表待插入的數(shù)據(jù)對(duì)象。
     * 使用反射機(jī)制,通過(guò)字段名設(shè)置字段值,實(shí)現(xiàn)創(chuàng)建時(shí)間和更新時(shí)間的自動(dòng)填充。
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        Date now = Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant());
        this.setFieldValByName("createTime", now, metaObject);
        this.setFieldValByName("updateTime", now, metaObject);
    }

    /**
     * 在更新數(shù)據(jù)時(shí)自動(dòng)填充更新時(shí)間。
     * @param metaObject 元對(duì)象,代表待更新的數(shù)據(jù)對(duì)象。
     * 使用反射機(jī)制,通過(guò)字段名設(shè)置字段值,實(shí)現(xiàn)更新時(shí)間的自動(dòng)填充。
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        Date now = Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant());
        this.setFieldValByName("updateTime", now, metaObject);
    }
}

package com.ruoyi.common.core.domain;

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

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;

/**
 * Entity基類
 * 
 * @author ruoyi
 */
public class BaseEntity implements Serializable
{
    private static final long serialVersionUID = 1L;

    /** 搜索值 */
    @JsonIgnore
    @TableField(exist=false)
    private String searchValue;

    /** 創(chuàng)建者 */
    private String createBy;

    /** 創(chuàng)建時(shí)間 TableField自動(dòng)維護(hù)*/
    @TableField(fill = FieldFill.INSERT)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    /** 更新者 */
    private String updateBy;

    /** 更新時(shí)間 TableField自動(dòng)維護(hù)*/
    @TableField(fill = FieldFill.INSERT_UPDATE)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date updateTime;

    /** 備注 */
    private String remark;

    /** 請(qǐng)求參數(shù) */
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    @TableField(exist=false)
    private Map<String, Object> params;

    public String getSearchValue()
    {
        return searchValue;
    }

    public void setSearchValue(String searchValue)
    {
        this.searchValue = searchValue;
    }

    public String getCreateBy()
    {
        return createBy;
    }

    public void setCreateBy(String createBy)
    {
        this.createBy = createBy;
    }

    public Date getCreateTime()
    {
        return createTime;
    }

    public void setCreateTime(Date createTime)
    {
        this.createTime = createTime;
    }

    public String getUpdateBy()
    {
        return updateBy;
    }

    public void setUpdateBy(String updateBy)
    {
        this.updateBy = updateBy;
    }

    public Date getUpdateTime()
    {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime)
    {
        this.updateTime = updateTime;
    }

    public String getRemark()
    {
        return remark;
    }

    public void setRemark(String remark)
    {
        this.remark = remark;
    }

    public Map<String, Object> getParams()
    {
        if (params == null)
        {
            params = new HashMap<>();
        }
        return params;
    }

    public void setParams(Map<String, Object> params)
    {
        this.params = params;
    }
}
package com.ruoyi.framework.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * Mybatis Plus 配置
 *
 * @author ruoyi
 */
@EnableTransactionManagement(proxyTargetClass = true)
@Configuration
public class MybatisPlusConfig
{
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor()
    {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 分頁(yè)插件
        interceptor.addInnerInterceptor(paginationInnerInterceptor());
        // 樂(lè)觀鎖插件
        interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor());
        // 阻斷插件
        interceptor.addInnerInterceptor(blockAttackInnerInterceptor());
        return interceptor;
    }

    /**
     * 分頁(yè)插件,自動(dòng)識(shí)別數(shù)據(jù)庫(kù)類型 https://baomidou.com/guide/interceptor-pagination.html
     */
    public PaginationInnerInterceptor paginationInnerInterceptor()
    {
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        // 設(shè)置數(shù)據(jù)庫(kù)類型為mysql
        paginationInnerInterceptor.setDbType(DbType.MYSQL);
        // 設(shè)置最大單頁(yè)限制數(shù)量,默認(rèn) 500 條,-1 不受限制
        paginationInnerInterceptor.setMaxLimit(-1L);
        return paginationInnerInterceptor;
    }

    /**
     * 樂(lè)觀鎖插件 https://baomidou.com/guide/interceptor-optimistic-locker.html
     */
    public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor()
    {
        return new OptimisticLockerInnerInterceptor();
    }

    /**
     * 如果是對(duì)全表的刪除或更新操作,就會(huì)終止該操作 https://baomidou.com/guide/interceptor-block-attack.html
     */
    public BlockAttackInnerInterceptor blockAttackInnerInterceptor()
    {
        return new BlockAttackInnerInterceptor();
    }
}

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java基礎(chǔ)第五篇 實(shí)施接口

    Java基礎(chǔ)第五篇 實(shí)施接口

    在public和private的封裝機(jī)制,我們實(shí)際上同時(shí)定義了類和接口,類和接口混合在一起。Java還提供了interface這一語(yǔ)法。這一語(yǔ)法將接口從類的具體定義中剝離出來(lái),構(gòu)成一個(gè)獨(dú)立的主體,下面文章內(nèi)容將為大家做詳細(xì)介紹
    2021-09-09
  • 詳解如何為SpringBoot Web應(yīng)用的日志方便追蹤

    詳解如何為SpringBoot Web應(yīng)用的日志方便追蹤

    在Web應(yīng)用程序領(lǐng)域,有效的請(qǐng)求監(jiān)控和可追溯性對(duì)于維護(hù)系統(tǒng)完整性和診斷問(wèn)題至關(guān)重要,SpringBoot是一種用于構(gòu)建Java應(yīng)用程序的流行框架,在本文中,我們探討了在SpringBoot中向日志添加唯一ID的重要性,需要的朋友可以參考下
    2023-11-11
  • Spring中AOP的切點(diǎn)、通知、切點(diǎn)表達(dá)式及知識(shí)要點(diǎn)整理

    Spring中AOP的切點(diǎn)、通知、切點(diǎn)表達(dá)式及知識(shí)要點(diǎn)整理

    這篇文章主要介紹了Spring中AOP的切點(diǎn)、通知、切點(diǎn)表達(dá)式及知識(shí)要點(diǎn)整理,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-03-03
  • Java多線程模擬銀行系統(tǒng)存錢問(wèn)題詳解

    Java多線程模擬銀行系統(tǒng)存錢問(wèn)題詳解

    本文將利用Java多線程模擬一個(gè)簡(jiǎn)單的銀行系統(tǒng),使用兩個(gè)不同的線程向同一個(gè)賬戶存錢。文中的示例代碼講解詳細(xì),感興趣的可以了解一下
    2022-09-09
  • MyBatis中動(dòng)態(tài)sql的實(shí)現(xiàn)方法示例

    MyBatis中動(dòng)態(tài)sql的實(shí)現(xiàn)方法示例

    這篇文章主要給大家介紹了關(guān)于MyBatis中動(dòng)態(tài)sql的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • Java內(nèi)存區(qū)域和內(nèi)存模型講解

    Java內(nèi)存區(qū)域和內(nèi)存模型講解

    今天小編就為大家分享一篇關(guān)于Java內(nèi)存區(qū)域和內(nèi)存模型講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-01-01
  • 詳解SpringBoot緩存的實(shí)例代碼(EhCache 2.x 篇)

    詳解SpringBoot緩存的實(shí)例代碼(EhCache 2.x 篇)

    這篇文章主要介紹了詳解SpringBoot緩存的實(shí)例代碼(EhCache 2.x 篇),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • java顯示當(dāng)前的系統(tǒng)時(shí)間

    java顯示當(dāng)前的系統(tǒng)時(shí)間

    這篇文章主要介紹了java如何顯示當(dāng)前的系統(tǒng)時(shí)間,代碼很簡(jiǎn)單,自己可以自定義顯示的系統(tǒng)時(shí)間的顏色和字體,需要的朋友可以參考下
    2015-10-10
  • 一篇文章帶你了解SpringMVC數(shù)據(jù)綁定

    一篇文章帶你了解SpringMVC數(shù)據(jù)綁定

    這篇文章主要給大家介紹了關(guān)于如何通過(guò)一篇文章弄懂Spring MVC的參數(shù)綁定,文中通過(guò)示例代碼以及圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-08-08
  • 在Spring Boot項(xiàng)目中引入本地JAR包的步驟和配置

    在Spring Boot項(xiàng)目中引入本地JAR包的步驟和配置

    本文探討了在Spring Boot項(xiàng)目中引入本地JAR包的步驟和必要的配置,通過(guò)使用Maven的system作用域,開(kāi)發(fā)者可以將自定義的本地庫(kù)或功能集成到Spring Boot應(yīng)用程序中,,需要的朋友可以參考下
    2023-10-10

最新評(píng)論