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

MyBatis-Plus自動(dòng)填充字段的詳細(xì)教程

 更新時(shí)間:2024年08月16日 08:40:29   作者:IT小輝同學(xué)  
今天編寫(xiě)一個(gè)詳細(xì)的教程來(lái)介紹如何在?Spring?Boot?項(xiàng)目中使用?MyBatis-Plus?實(shí)現(xiàn)自動(dòng)填充時(shí)間字段(如創(chuàng)建時(shí)間?createTime?和更新時(shí)間?updateTime),可以分為以下幾個(gè)部分,這個(gè)教程將涵蓋從項(xiàng)目配置到自動(dòng)填充的完整過(guò)程,需要的朋友可以參考下

1. 項(xiàng)目環(huán)境配置

首先,需要確保項(xiàng)目中已經(jīng)集成了 Spring Boot 和 MyBatis-Plus。下面是一些基本的配置步驟:

1.1 引入必要的依賴(lài)

在 pom.xml 中添加 MyBatis-Plus 的依賴(lài):

<dependencies>
    <!-- MyBatis-Plus -->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.5.1</version>
    </dependency>

    <!-- 其他必要依賴(lài) -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- MySQL 驅(qū)動(dòng) -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
</dependencies>

1.2 配置數(shù)據(jù)源

在 application.yml 或 application.properties 文件中配置數(shù)據(jù)庫(kù)連接:

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/your_database?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: your_username
    password: your_password
    driver-class-name: com.mysql.cj.jdbc.Driver

mybatis-plus:
  mapper-locations: classpath:/mapper/*.xml
  type-aliases-package: com.example.entity
  global-config:
    db-config:
      id-type: auto

2. 創(chuàng)建基礎(chǔ)實(shí)體類(lèi)

為數(shù)據(jù)庫(kù)中的表創(chuàng)建一個(gè)基礎(chǔ)實(shí)體類(lèi) BaseEntity,在該類(lèi)中定義創(chuàng)建時(shí)間和更新時(shí)間字段,并使用 MyBatis-Plus 的注解配置自動(dòng)填充。

package com.example.utils;

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

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

@Data
public class BaseEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 搜索值(暫時(shí)忽略)
     */
    @JsonIgnore
    @TableField(exist = false)
    private String searchValue;

    /**
     * 創(chuàng)建者
     */
    @TableField(fill = FieldFill.INSERT)
    private String createBy;

    /**
     * 創(chuàng)建時(shí)間
     */
    @TableField(fill = FieldFill.INSERT)
    private Date createTime;

    /**
     * 更新者
     */
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private String updateBy;

    /**
     * 更新時(shí)間
     */
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;

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

}

3. 實(shí)現(xiàn) MetaObjectHandler 接口

為了使 BaseEntity 中的自動(dòng)填充注解生效,我們需要實(shí)現(xiàn) MetaObjectHandler 接口,并將其配置為 Spring 的一個(gè) Bean。

3.1 創(chuàng)建 MyMetaObjectHandler 類(lèi)

package com.example.config;

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

import java.util.Date;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        // 填充創(chuàng)建時(shí)間和更新時(shí)間
        this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
        this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
        // 可以根據(jù)業(yè)務(wù)需求獲取當(dāng)前用戶(hù),填充創(chuàng)建者和更新者
        this.strictInsertFill(metaObject, "createBy", String.class, getCurrentUser());
        this.strictInsertFill(metaObject, "updateBy", String.class, getCurrentUser());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        // 更新時(shí)只填充更新時(shí)間和更新者
        this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
        this.strictUpdateFill(metaObject, "updateBy", String.class, getCurrentUser());
    }

    // 示例方法:獲取當(dāng)前用戶(hù)信息
    private String getCurrentUser() {
        // 這里可以集成 Spring Security 或其他上下文獲取實(shí)際的當(dāng)前用戶(hù)
        return "system"; // 這是一個(gè)占位符,實(shí)際應(yīng)用中會(huì)替換為真實(shí)用戶(hù)信息
    }
}

3.2 配置生效

通過(guò)在 MyMetaObjectHandler 類(lèi)上使用 @Component 注解,Spring 會(huì)自動(dòng)管理這個(gè)類(lèi),并在實(shí)體插入和更新時(shí)調(diào)用它來(lái)填充字段。

4. 實(shí)體類(lèi)繼承 BaseEntity

現(xiàn)在,你的具體實(shí)體類(lèi)可以繼承 BaseEntity,從而自動(dòng)獲得 createTimeupdateTime 字段的自動(dòng)填充功能。

package com.example.entity;

import com.baomidou.mybatisplus.annotation.TableName;
import com.example.utils.BaseEntity;
import lombok.Data;

@Data
@TableName("your_table")
public class YourEntity extends BaseEntity {
    // 其他字段
    private String name;
    private Integer age;
}

5. 處理數(shù)據(jù)插入與更新

在 Service 或 Mapper 層,執(zhí)行插入或更新操作時(shí),createTime 和 updateTime 字段會(huì)自動(dòng)填充。

5.1 Service 層示例

@Service
public class YourEntityService {

    @Autowired
    private YourEntityMapper yourEntityMapper;

    public void saveEntity(YourEntity entity) {
        yourEntityMapper.insert(entity);
    }

    public void updateEntity(YourEntity entity) {
        yourEntityMapper.updateById(entity);
    }
}

6. 驗(yàn)證自動(dòng)填充功能

通過(guò)簡(jiǎn)單的單元測(cè)試或集成測(cè)試,驗(yàn)證 createTimeupdateTime 字段在插入和更新時(shí)是否正確自動(dòng)填充。

7. 其他注意事項(xiàng)

  • 字段類(lèi)型: 確保數(shù)據(jù)庫(kù)中的 createTimeupdateTime 字段類(lèi)型與 Java 實(shí)體類(lèi)中的類(lèi)型相匹配(通常是 DATETIME 類(lèi)型)。
  • 時(shí)間格式: 如果需要統(tǒng)一時(shí)間格式,可以在配置文件中設(shè)置 Spring Boot 的全局時(shí)間格式,或使用 @JsonFormat 注解指定序列化時(shí)的格式。
spring:
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8

總結(jié)

通過(guò)本文的教程,你可以輕松地在 Spring Boot 項(xiàng)目中使用 MyBatis-Plus 自動(dòng)填充創(chuàng)建時(shí)間和更新時(shí)間字段。該方法充分利用了 MyBatis-Plus 的自動(dòng)填充機(jī)制,并結(jié)合 Spring Boot 的優(yōu)勢(shì),使開(kāi)發(fā)過(guò)程更加簡(jiǎn)潔高效。如果遇到問(wèn)題,務(wù)必檢查 MetaObjectHandler 是否正確注冊(cè),字段類(lèi)型是否匹配,以及數(shù)據(jù)庫(kù)配置是否正確。

以上就是MyBatis-Plus自動(dòng)填充字段的詳細(xì)教程的詳細(xì)內(nèi)容,更多關(guān)于MyBatis-Plus自動(dòng)填充字段的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • java新人基礎(chǔ)入門(mén)之遞歸調(diào)用

    java新人基礎(chǔ)入門(mén)之遞歸調(diào)用

    這篇文章主要給大家介紹了關(guān)于java新人基礎(chǔ)入門(mén)之遞歸調(diào)用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • Spring AOP 實(shí)現(xiàn)自定義注解的示例

    Spring AOP 實(shí)現(xiàn)自定義注解的示例

    這篇文章主要介紹了Spring AOP 實(shí)現(xiàn)自定義注解的示例,幫助大家更好的理解和學(xué)習(xí)使用spring框架,感興趣的朋友可以了解下
    2021-03-03
  • Java實(shí)現(xiàn)冒泡排序算法

    Java實(shí)現(xiàn)冒泡排序算法

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)冒泡排序算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Java NIO寫(xiě)大文件對(duì)比(win7和mac)

    Java NIO寫(xiě)大文件對(duì)比(win7和mac)

    這篇文章主要介紹了Java NIO寫(xiě)大文件對(duì)比(win7和mac),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07
  • 深入了解Java中Volatile關(guān)鍵字

    深入了解Java中Volatile關(guān)鍵字

    這篇文章主要介紹了Java中Volatile關(guān)鍵字的相關(guān)知識(shí),文章講解非常詳細(xì),代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • java.net.ConnectException: Connection refused問(wèn)題解決辦法

    java.net.ConnectException: Connection refused問(wèn)題解決辦法

    這篇文章主要介紹了java.net.ConnectException: Connection refused問(wèn)題解決辦法的相關(guān)資料,需要的朋友可以參考下
    2016-12-12
  • 淺析如何在SpringBoot中實(shí)現(xiàn)數(shù)據(jù)脫敏

    淺析如何在SpringBoot中實(shí)現(xiàn)數(shù)據(jù)脫敏

    脫敏是指在不改變?cè)瓟?shù)據(jù)結(jié)構(gòu)的前提下,通過(guò)某種方式處理數(shù)據(jù),使數(shù)據(jù)不能直接暴露用戶(hù)的真實(shí)信息,下面我們就來(lái)看看SpringBoot中實(shí)現(xiàn)數(shù)據(jù)脫敏的具體方法吧
    2024-03-03
  • JAVA自定義注解詳情

    JAVA自定義注解詳情

    這篇文章主要介紹了Java自定義注解,結(jié)合實(shí)例形式總結(jié)分析了java常見(jiàn)的自定義注解類(lèi)型、功能、用法及操作注意事項(xiàng),需要的朋友可以參考下
    2021-10-10
  • 詳解Java多線程tryLock()方法使用

    詳解Java多線程tryLock()方法使用

    本文主要介紹了Java多線程tryLock()方法,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • SpringBoot整合SpringSecurityOauth2實(shí)現(xiàn)鑒權(quán)動(dòng)態(tài)權(quán)限問(wèn)題

    SpringBoot整合SpringSecurityOauth2實(shí)現(xiàn)鑒權(quán)動(dòng)態(tài)權(quán)限問(wèn)題

    這篇文章主要介紹了SpringBoot整合SpringSecurityOauth2實(shí)現(xiàn)鑒權(quán)-動(dòng)態(tài)權(quán)限,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06

最新評(píng)論