11package top .cadecode .uniboot .framework .config ;
22
3+ import com .baomidou .mybatisplus .core .handlers .MetaObjectHandler ;
34import com .baomidou .mybatisplus .extension .plugins .MybatisPlusInterceptor ;
45import com .baomidou .mybatisplus .extension .plugins .inner .PaginationInnerInterceptor ;
6+ import org .apache .ibatis .reflection .MetaObject ;
57import org .springframework .context .annotation .Bean ;
68import org .springframework .context .annotation .Configuration ;
9+ import top .cadecode .uniboot .framework .security .TokenAuthHolder ;
10+ import top .cadecode .uniboot .system .bean .dto .SysUserDto .SysUserDetailsDto ;
11+
12+ import java .util .Date ;
13+ import java .util .Optional ;
714
815/**
916 * mybatis 配置类
1320 */
1421@ Configuration
1522public class MybatisConfig {
23+
1624 /**
1725 * Mybatis Plus 插件配置
1826 */
@@ -22,4 +30,30 @@ public MybatisPlusInterceptor mybatisPlusInterceptor() {
2230 interceptor .addInnerInterceptor (new PaginationInnerInterceptor ());
2331 return interceptor ;
2432 }
33+
34+ @ Bean
35+ public MetaObjectHandler metaObjectHandler () {
36+ return new MetaObjectHandler () {
37+ @ Override
38+ public void insertFill (MetaObject metaObject ) {
39+ this .setFieldValByName ("createTime" , new Date (), metaObject );
40+ updateUser (metaObject );
41+ }
42+
43+ @ Override
44+ public void updateFill (MetaObject metaObject ) {
45+ this .setFieldValByName ("updateTime" , new Date (), metaObject );
46+ updateUser (metaObject );
47+ }
48+
49+ public void updateUser (MetaObject metaObject ) {
50+ SysUserDetailsDto userDetails = TokenAuthHolder .getUserDetails (null );
51+ String updateUser = Optional .ofNullable (userDetails )
52+ .map (SysUserDetailsDto ::getUsername )
53+ .map (String ::valueOf )
54+ .orElse (null );
55+ this .setFieldValByName ("updateUser" , updateUser , metaObject );
56+ }
57+ };
58+ }
2559}
0 commit comments