@
magicdu #14
```
/**
* 处理新增和更新的基础数据填充,配合 BaseEntity 和 MyBatisPlusConfig 使用
*/
@
Componentpublic class MetaHandler implements MetaObjectHandler {
/**
* 新增数据执行
* @
param metaObject
*/
@
Override public void insertFill(MetaObject metaObject) {
UserDetails user;
try {
user = SecurityUtils.getUserDetails();
this.setFieldValByName("crtUserName", user.getUsername(), metaObject);
this.setFieldValByName("crtUserId", SecurityUtils.getUserId(), metaObject);
this.setFieldValByName("updUserName", user.getUsername(), metaObject);
this.setFieldValByName("updUserId", SecurityUtils.getUserId(), metaObject);
} catch (Exception e) {
}
this.setFieldValByName("crtTime", new Date(), metaObject);
this.setFieldValByName("updTime", new Date(), metaObject);
}
/**
* 更新数据执行
* @
param metaObject
*/
@
Override public void updateFill(MetaObject metaObject) {
UserDetails user;
try {
user = SecurityUtils.getUserDetails();
this.setFieldValByName("updUserName", user.getUsername(), metaObject);
this.setFieldValByName("updUserId", SecurityUtils.getUserId(), metaObject);
} catch (Exception e) {
}
this.setFieldValByName("updTime", new Date(), metaObject);
}
}
```
配合 BaseEntity 和 MyBatisPlusConfig 使用
```
@
Configurationpublic class MyBatisPlusConfig {
/**
* 自动填充功能
* @
return */
@
Bean public GlobalConfig globalConfig() {
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setMetaObjectHandler(new MetaHandler());
return globalConfig;
}
}
```