content: package com.achobeta.config;
  
  import com.baomidou.mybatisplus.annotation.DbType;
  import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
  import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
  import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
  import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
  import org.apache.ibatis.reflection.MetaObject;
  import org.springframework.beans.factory.annotation.Value;
  import org.springframework.context.annotation.Bean;
  import org.springframework.stereotype.Component;
  import org.springframework.transaction.annotation.EnableTransactionManagement;
  
  import java.time.LocalDateTime;
  
  /**
   * <span>
   * Mybatis fill config
   * </span>
   *
   * @author jettcc in 2024/01/23
   * @version 1.0
   */
  @Component
  @EnableTransactionManagement
  public class MybatisFillConfig implements MetaObjectHandler {
  
      @Value("${mybatis-plus.db-type}")
      private DbType dbType;
  
      @Override
      public void insertFill(MetaObject metaObject) {
          this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
          this.strictInsertFill(metaObject, "version", Integer.class, 1);
          this.strictInsertFill(metaObject, "deleted", Integer.class, 0);
      }
  
      @Override
      public void updateFill(MetaObject metaObject) {
          this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
      }
  
      //乐观锁
      @Bean
      public MybatisPlusInterceptor mybatisPlusInterceptor() {
          MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
          interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
          // 添加分页插件
          interceptor.addInnerInterceptor(new PaginationInnerInterceptor(dbType)); // 如果配置多个插件, 切记分页最后添加
          return interceptor;
      }
  }