Mybatis plusを使用して、postgresqlパブリックフィールドの自動挿入と変更を実現します

リレーショナルデータベースを使用する場合、基本的にすべてのテーブルには、created_time、modified_time、created_by、modified_byの4つのフィールドが含まれます。通常、これらの4つのフィールドには初期値があります。たとえば、データを挿入する場合、最初の2つのフィールドは現在の時刻です。次の2つのフィールドは、通常、開発者名またはプロジェクト名などです。modified_timeは、データが変更された現在の時刻でもあります。したがって、mybatis plusの自動入力機能を使用して、これら4つの値の値を設定できます。次のケースはpostgresqlに基づいています。
まず pomファイルに必要な2つの依存関係は次のとおりです。

		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-boot-starter</artifactId>
			<version>3.3.1</version>
		</dependency>
		<dependency>
			<groupId>org.postgresql</groupId>
			<artifactId>postgresql</artifactId>
		</dependency>
		

次に、これら4つのフィールドをBaseにカプセル化します

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;

import java.time.LocalDateTime;

@Data
public class Base {
    
    
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private String createdBy;

    @TableField(fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime createdTime;

    @TableField(fill = FieldFill.INSERT_UPDATE)
    private String modifiedBy;

    @TableField(fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime modifiedTime;
}

次に、ハンドラーを作成します。このステップは自動充填の鍵であり、スプリングコンテナーで管理する必要があるため、@ Componentを追加する必要があります。

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

import java.time.LocalDateTime;

/**
 * @author :channing
 * @date :Created in 5/9/2020 4:58 PM
 */
@Slf4j
@Component
public class Af4pgMetaObjectHandler implements MetaObjectHandler {
    
    

    @Override
    public void insertFill(MetaObject metaObject) {
    
    
        log.info("start auto fill ....");
        this.setFieldValByName("createdTime", LocalDateTime.now(), metaObject);
        this.setFieldValByName("createdBy", "gbms", metaObject);
        this.setFieldValByName("modifiedTime", LocalDateTime.now(), metaObject);
        this.setFieldValByName("modifiedBy", "gbms", metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
    
    
        log.info("start auto update ....");
        this.setFieldValByName("modifiedTime", LocalDateTime.now(), metaObject);
    }
}

マッパーレイヤーを定義する

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.channing.af.entity.Goods;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface GoodsMapper extends BaseMapper<Goods> {
    
    

}

構成ファイル

spring:
  datasource:
    driver-class-name: org.postgresql.Driver
    url: jdbc:postgresql://localhost:5432/af4pg
    username: postgres
    password: postgres
    platform: POSTGRESQL

mybatis-plus:
  global-config:
    db-config:
      id-type: assign_id
      capital-mode: true

logging:
  level:
    com.baomidou.samples.metainfo: debug

ソースコード:https//github.com/igdnss/AF4PG.git

この記事はニー先生の説明に言及していますhttps//gitee.com/baomidou/mybatis-plus-samples.git

おすすめ

転載: blog.csdn.net/hongyinanhai00/article/details/106007530