update docs/zh/core/table.md.

UpdateListener与InsertListener 使用实际案例

Signed-off-by: kings <963987632@qq.com>
This commit is contained in:
kings 2024-12-09 02:31:42 +00:00 committed by Gitee
parent c1e16abc0a
commit f1f4b4402d
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F

View File

@ -178,3 +178,54 @@ config.registerUpdateListener(updateListener, Entity1.class, Entity2.class);
//为 Entity1 和 Entity2 注册 setListener
config.registerSetListener(setListener, Entity1.class, Entity2.class);
```
## 案例
### 案例 1设置创建和更新者的用户名
需要自建一个**BaseEntity**前提是涉及到的类需要继承BaseEntity
```java
public class MybatisUpdateListener implements UpdateListener {
@Override
public void onUpdate(Object o) {
Object username = StpUtil.getExtra("username"); //此处获取用户名
if (username != null && o instanceof BaseEntity entity) {
entity.setUpdateBy(username.toString());
}
}
}
```
```java
public class MybatisInsertListener implements InsertListener {
@Override
public void onInsert(Object o) {
Object username = StpUtil.getExtra("username"); //此处获取用户名
if (username != null && o instanceof BaseEntity entity) {
entity.setCreateBy(username.toString());
}
}
}
```
配置
```java
@Configuration
public class MyBatisFlexConfiguration {
public MyBatisFlexConfiguration() {
MybatisInsertListener mybatisInsertListener = new MybatisInsertListener();
MybatisUpdateListener mybatisUpdateListener = new MybatisUpdateListener();
FlexGlobalConfig config = FlexGlobalConfig.getDefaultConfig();
//设置BaseEntity类启用
config.registerInsertListener(mybatisInsertListener, BaseEntity.class);
config.registerUpdateListener(mybatisUpdateListener, BaseEntity.class);
);
}
}
```