package com.example.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 com.example.security.UserContext;
|
import org.apache.ibatis.reflection.MetaObject;
|
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Configuration;
|
|
import java.time.LocalDateTime;
|
|
@Configuration
|
public class MyBatisPlusConfig {
|
|
@Bean
|
public MybatisPlusInterceptor mybatisPlusInterceptor() {
|
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.ORACLE));
|
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
|
return interceptor;
|
}
|
|
@Bean
|
public MetaObjectHandler metaObjectHandler() {
|
return new MetaObjectHandler() {
|
@Override
|
public void insertFill(MetaObject metaObject) {
|
String username = UserContext.getCurrentUsername();
|
this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
|
this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
|
this.strictInsertFill(metaObject, "deleted", Integer.class, 0);
|
this.strictInsertFill(metaObject, "version", Integer.class, 1);
|
this.strictInsertFill(metaObject, "createBy", String.class, username != null ? username : "系统");
|
this.strictInsertFill(metaObject, "updateBy", String.class, username != null ? username : "系统");
|
}
|
|
@Override
|
public void updateFill(MetaObject metaObject) {
|
String username = UserContext.getCurrentUsername();
|
this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
|
this.strictUpdateFill(metaObject, "updateBy", String.class, username != null ? username : "系统");
|
}
|
};
|
}
|
}
|