From f1f4b4402de80e568d3a42be031d694d24130ca0 Mon Sep 17 00:00:00 2001 From: kings <963987632@qq.com> Date: Mon, 9 Dec 2024 02:31:42 +0000 Subject: [PATCH] =?UTF-8?q?update=20docs/zh/core/table.md.=20UpdateListene?= =?UTF-8?q?r=E4=B8=8EInsertListener=20=E4=BD=BF=E7=94=A8=E5=AE=9E=E9=99=85?= =?UTF-8?q?=E6=A1=88=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: kings <963987632@qq.com> --- docs/zh/core/table.md | 51 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/docs/zh/core/table.md b/docs/zh/core/table.md index 84567c77..057eac32 100644 --- a/docs/zh/core/table.md +++ b/docs/zh/core/table.md @@ -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); + + ); + } +} +```