mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-06 16:48:24 +08:00
update docs/zh/core/table.md.
UpdateListener与InsertListener 使用实际案例 Signed-off-by: kings <963987632@qq.com>
This commit is contained in:
parent
c1e16abc0a
commit
f1f4b4402d
@ -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);
|
||||
|
||||
);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user