MyBatisPlusConfig.java
1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package vion.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.PaginationInnerInterceptor;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.time.LocalDateTime;
/**
* @author HlQ
* @date 2022/7/20
*/
@Configuration
public class MyBatisPlusConfig implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
this.fillHasGetter(metaObject, "createTime", LocalDateTime.now());
this.fillHasGetter(metaObject, "modifyTime", LocalDateTime.now());
this.fillHasGetter(metaObject, "updateTime", LocalDateTime.now());
}
@Override
public void updateFill(MetaObject metaObject) {
this.fillHasGetter(metaObject, "modifyTime", LocalDateTime.now());
this.fillHasGetter(metaObject, "updateTime", LocalDateTime.now());
}
protected void fillHasGetter(MetaObject metaObject, String fieldName, Object fieldVal) {
if (metaObject.hasGetter(fieldName)) {
this.fillStrategy(metaObject, fieldName, fieldVal);
}
}
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.POSTGRE_SQL));
return interceptor;
}
}