From 2388ceb4decc305e524b0c467dfc62d997316ecf Mon Sep 17 00:00:00 2001 From: Ice Date: Mon, 23 Oct 2023 23:42:19 +0800 Subject: [PATCH 1/3] =?UTF-8?q?add=EF=BC=9AListener=E7=BC=BA=E5=A4=B1?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AccountAgeInsertListener.java | 13 ++++ .../AccountAgeInsertListenerFlag.java | 5 ++ .../AccountTableAnnoInsertListener.java | 13 ++++ .../missionListenerFix/BaseLogicDelete.java | 18 +++++ .../LogicDeleteInsertListener.java | 12 ++++ .../LogicDeleteInsertListenerFlag.java | 5 ++ .../AccountMissingListenerTestModel.java | 61 ++++++++++++++++ .../mybatisflex/test/common/ListenerTest.java | 69 +++++++++++++++++++ 8 files changed, 196 insertions(+) create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/listener/missionListenerFix/AccountAgeInsertListener.java create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/listener/missionListenerFix/AccountAgeInsertListenerFlag.java create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/listener/missionListenerFix/AccountTableAnnoInsertListener.java create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/listener/missionListenerFix/BaseLogicDelete.java create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/listener/missionListenerFix/LogicDeleteInsertListener.java create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/listener/missionListenerFix/LogicDeleteInsertListenerFlag.java create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/AccountMissingListenerTestModel.java create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/common/ListenerTest.java diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/listener/missionListenerFix/AccountAgeInsertListener.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/listener/missionListenerFix/AccountAgeInsertListener.java new file mode 100644 index 00000000..f1be67b4 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/listener/missionListenerFix/AccountAgeInsertListener.java @@ -0,0 +1,13 @@ +package com.mybatisflex.test.listener.missionListenerFix; + +import com.mybatisflex.annotation.InsertListener; +import com.mybatisflex.test.model.AccountMissingListenerTestModel; + +public class AccountAgeInsertListener implements InsertListener { + + @Override + public void onInsert(Object entity) { + AccountMissingListenerTestModel model = (AccountMissingListenerTestModel) entity; + model.setAge(18); + } +} diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/listener/missionListenerFix/AccountAgeInsertListenerFlag.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/listener/missionListenerFix/AccountAgeInsertListenerFlag.java new file mode 100644 index 00000000..3ad58d0a --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/listener/missionListenerFix/AccountAgeInsertListenerFlag.java @@ -0,0 +1,5 @@ +package com.mybatisflex.test.listener.missionListenerFix; + +public interface AccountAgeInsertListenerFlag { + +} diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/listener/missionListenerFix/AccountTableAnnoInsertListener.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/listener/missionListenerFix/AccountTableAnnoInsertListener.java new file mode 100644 index 00000000..211c956f --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/listener/missionListenerFix/AccountTableAnnoInsertListener.java @@ -0,0 +1,13 @@ +package com.mybatisflex.test.listener.missionListenerFix; + +import com.mybatisflex.annotation.InsertListener; +import com.mybatisflex.test.model.AccountMissingListenerTestModel; + +public class AccountTableAnnoInsertListener implements InsertListener { + + @Override + public void onInsert(Object entity) { + AccountMissingListenerTestModel model = (AccountMissingListenerTestModel) entity; + model.setUserName("测试缺失的监听器-userName"); + } +} diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/listener/missionListenerFix/BaseLogicDelete.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/listener/missionListenerFix/BaseLogicDelete.java new file mode 100644 index 00000000..edbe1775 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/listener/missionListenerFix/BaseLogicDelete.java @@ -0,0 +1,18 @@ +package com.mybatisflex.test.listener.missionListenerFix; + +import com.mybatisflex.annotation.Column; + +public class BaseLogicDelete implements LogicDeleteInsertListenerFlag { + + @Column(isLogicDelete = true) + private Boolean isDelete; + + public Boolean getDelete() { + return isDelete; + } + + public void setDelete(Boolean delete) { + isDelete = delete; + } + +} diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/listener/missionListenerFix/LogicDeleteInsertListener.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/listener/missionListenerFix/LogicDeleteInsertListener.java new file mode 100644 index 00000000..b20484b0 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/listener/missionListenerFix/LogicDeleteInsertListener.java @@ -0,0 +1,12 @@ +package com.mybatisflex.test.listener.missionListenerFix; + +import com.mybatisflex.annotation.InsertListener; + +public class LogicDeleteInsertListener implements InsertListener { + + @Override + public void onInsert(Object entity) { + BaseLogicDelete logicDeleteEntity = (BaseLogicDelete) entity; + logicDeleteEntity.setDelete(true); + } +} diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/listener/missionListenerFix/LogicDeleteInsertListenerFlag.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/listener/missionListenerFix/LogicDeleteInsertListenerFlag.java new file mode 100644 index 00000000..48e1a2a5 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/listener/missionListenerFix/LogicDeleteInsertListenerFlag.java @@ -0,0 +1,5 @@ +package com.mybatisflex.test.listener.missionListenerFix; + +public interface LogicDeleteInsertListenerFlag { + +} diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/AccountMissingListenerTestModel.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/AccountMissingListenerTestModel.java new file mode 100644 index 00000000..12c48bee --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/AccountMissingListenerTestModel.java @@ -0,0 +1,61 @@ +package com.mybatisflex.test.model; + +import com.mybatisflex.annotation.*; +import com.mybatisflex.test.listener.missionListenerFix.*; + +/** + * 缺失的监听器测试 + * + * @author Ice 2023/10/23 + * @version 1.0 + */ +@Table(value = "tb_account", onInsert = AccountTableAnnoInsertListener.class) +public class AccountMissingListenerTestModel extends BaseLogicDelete implements AccountAgeInsertListenerFlag { + + /** + * 主键。 + */ + @Id(keyType = KeyType.Auto) + private Long id; + + private String userName; + + private Integer age; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } + +} + + + + + + + + + + + + diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/common/ListenerTest.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/common/ListenerTest.java new file mode 100644 index 00000000..ac47f913 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/common/ListenerTest.java @@ -0,0 +1,69 @@ +package com.mybatisflex.test.common; + +import com.mybatisflex.annotation.InsertListener; +import com.mybatisflex.core.FlexGlobalConfig; +import com.mybatisflex.core.exception.FlexAssert; +import com.mybatisflex.core.exception.FlexExceptions; +import com.mybatisflex.core.exception.locale.LocalizedFormats; +import com.mybatisflex.core.table.TableInfo; +import com.mybatisflex.core.table.TableInfoFactory; +import com.mybatisflex.core.util.CollectionUtil; +import com.mybatisflex.test.listener.missionListenerFix.*; +import com.mybatisflex.test.model.AccountMissingListenerTestModel; +import org.apache.ibatis.util.MapUtil; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.Collectors; + +/** + * 监听器测试 + * + * @author Ice 2023/10/23 + * @version 1.0 + */ +@SpringBootTest +class ListenerTest { + + @Test + void missingListenerTest() { + + AccountMissingListenerTestModel accountMissingListenerTestModel = new AccountMissingListenerTestModel(); + + //加入配置 + FlexGlobalConfig config = FlexGlobalConfig.getDefaultConfig(); + config.registerInsertListener(new LogicDeleteInsertListener(), LogicDeleteInsertListenerFlag.class); + config.registerInsertListener(new AccountAgeInsertListener(), AccountAgeInsertListenerFlag.class); + + //获取TableInfo + TableInfo tableInfo = TableInfoFactory.ofEntityClass(AccountMissingListenerTestModel.class); + + //执行测试 ===> Listener列表比对 + + Map, List> tempInsertListenerMap = new ConcurrentHashMap<>();//替代原本的缓存Map + + List insertListeners = MapUtil.computeIfAbsent(tempInsertListenerMap, AccountMissingListenerTestModel.class, aClass -> { + List globalListeners = FlexGlobalConfig.getDefaultConfig() + .getSupportedInsertListener(AccountMissingListenerTestModel.class, CollectionUtil.isNotEmpty(tableInfo.getOnInsertListeners())); + List allListeners = CollectionUtil.merge(tableInfo.getOnInsertListeners(), globalListeners); + Collections.sort(allListeners); + return allListeners; + }); + + List> resolvedInsertListeners = insertListeners.stream().map(insertListener -> insertListener.getClass()).collect(Collectors.toList()); + for (Class clazz : CollectionUtil.newArrayList(LogicDeleteInsertListener.class, AccountAgeInsertListener.class, AccountTableAnnoInsertListener.class)) { + if (!resolvedInsertListeners.contains(clazz)) { + throw FlexExceptions.wrap("缺失的InsertListener【%s】", clazz.getSimpleName()); + } + } + + //执行测试 ===> 插入结果比对 +// BaseMapper baseMapper = Mappers.ofEntityClass(accountMissingListenerTestModel.getClass()); +// baseMapper.insert(accountMissingListenerTestModel); + + } +} From 01a6ba68923617f327cf4f195ec5c90f910f9ef3 Mon Sep 17 00:00:00 2001 From: Ice Date: Mon, 23 Oct 2023 23:54:28 +0800 Subject: [PATCH 2/3] =?UTF-8?q?optimize=EF=BC=9AListener=E7=BC=BA=E5=A4=B1?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E6=8F=92=E5=85=A5=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../LogicDeleteInsertListener.java | 2 +- .../mybatisflex/test/common/ListenerTest.java | 23 +++++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/listener/missionListenerFix/LogicDeleteInsertListener.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/listener/missionListenerFix/LogicDeleteInsertListener.java index b20484b0..8ea4f327 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/listener/missionListenerFix/LogicDeleteInsertListener.java +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/listener/missionListenerFix/LogicDeleteInsertListener.java @@ -7,6 +7,6 @@ public class LogicDeleteInsertListener implements InsertListener { @Override public void onInsert(Object entity) { BaseLogicDelete logicDeleteEntity = (BaseLogicDelete) entity; - logicDeleteEntity.setDelete(true); + logicDeleteEntity.setDelete(false); } } diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/common/ListenerTest.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/common/ListenerTest.java index ac47f913..f2d3338a 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/common/ListenerTest.java +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/common/ListenerTest.java @@ -1,10 +1,12 @@ package com.mybatisflex.test.common; import com.mybatisflex.annotation.InsertListener; +import com.mybatisflex.core.BaseMapper; import com.mybatisflex.core.FlexGlobalConfig; import com.mybatisflex.core.exception.FlexAssert; import com.mybatisflex.core.exception.FlexExceptions; import com.mybatisflex.core.exception.locale.LocalizedFormats; +import com.mybatisflex.core.mybatis.Mappers; import com.mybatisflex.core.table.TableInfo; import com.mybatisflex.core.table.TableInfoFactory; import com.mybatisflex.core.util.CollectionUtil; @@ -44,9 +46,9 @@ class ListenerTest { //执行测试 ===> Listener列表比对 - Map, List> tempInsertListenerMap = new ConcurrentHashMap<>();//替代原本的缓存Map + Map, List> tempOnInsertListenerMap = new ConcurrentHashMap<>();//替代原本的缓存Map - List insertListeners = MapUtil.computeIfAbsent(tempInsertListenerMap, AccountMissingListenerTestModel.class, aClass -> { + List insertListeners = MapUtil.computeIfAbsent(tempOnInsertListenerMap, AccountMissingListenerTestModel.class, aClass -> { List globalListeners = FlexGlobalConfig.getDefaultConfig() .getSupportedInsertListener(AccountMissingListenerTestModel.class, CollectionUtil.isNotEmpty(tableInfo.getOnInsertListeners())); List allListeners = CollectionUtil.merge(tableInfo.getOnInsertListeners(), globalListeners); @@ -62,8 +64,21 @@ class ListenerTest { } //执行测试 ===> 插入结果比对 -// BaseMapper baseMapper = Mappers.ofEntityClass(accountMissingListenerTestModel.getClass()); -// baseMapper.insert(accountMissingListenerTestModel); + BaseMapper baseMapper = Mappers.ofEntityClass(accountMissingListenerTestModel.getClass()); + baseMapper.insert(accountMissingListenerTestModel); + AccountMissingListenerTestModel dbData = (AccountMissingListenerTestModel) baseMapper.selectOneById(accountMissingListenerTestModel.getId()); + + if (dbData.getDelete() == null || dbData.getDelete() != false) { + throw FlexExceptions.wrap("缺失的InsertListener【%s】", LogicDeleteInsertListener.class.getSimpleName()); + } + + if (dbData.getAge() == null || dbData.getAge() != 18) { + throw FlexExceptions.wrap("缺失的InsertListener【%s】", AccountAgeInsertListener.class.getSimpleName()); + } + + if (dbData.getUserName() == null || !dbData.getUserName().equals("测试缺失的监听器-userName")) { + throw FlexExceptions.wrap("缺失的InsertListener【%s】", AccountTableAnnoInsertListener.class.getSimpleName()); + } } } From 9fa2d5df2f18f2d9e58538956774d2517db2d137 Mon Sep 17 00:00:00 2001 From: Ice Date: Mon, 23 Oct 2023 22:29:10 +0800 Subject: [PATCH 3/3] =?UTF-8?q?fix=EF=BC=9A=E5=A4=9Alistener=E7=BC=BA?= =?UTF-8?q?=E5=A4=B1=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mybatisflex/core/FlexGlobalConfig.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/FlexGlobalConfig.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/FlexGlobalConfig.java index 5c68ccb6..ee4ad4fe 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/FlexGlobalConfig.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/FlexGlobalConfig.java @@ -221,13 +221,15 @@ public class FlexGlobalConfig { private void doGetSupportedSetListener(Class childClass, Map, SetListener> listeners) { SetListener setListener = null; + Class listenersMapKey = null; while (setListener == null && childClass != null) { setListener = entitySetListeners.get(childClass); + listenersMapKey = childClass.getSuperclass() == null ? childClass : childClass.getSuperclass(); childClass = childClass.getSuperclass(); } if (setListener != null) { - listeners.put(childClass, setListener); + listeners.put(listenersMapKey, setListener); } } @@ -264,13 +266,15 @@ public class FlexGlobalConfig { private void doGetSupportedUpdateListener(Class childClass, Map, UpdateListener> listeners) { UpdateListener updateListener = null; + Class listenersMapKey = null; while (updateListener == null && childClass != null) { updateListener = entityUpdateListeners.get(childClass); + listenersMapKey = childClass.getSuperclass() == null ? childClass : childClass.getSuperclass(); childClass = childClass.getSuperclass(); } if (updateListener != null) { - listeners.put(childClass, updateListener); + listeners.put(listenersMapKey, updateListener); } } @@ -307,13 +311,15 @@ public class FlexGlobalConfig { private void doGetSupportedInsertListener(Class childClass, Map, InsertListener> listeners) { InsertListener insertListener = null; + Class listenersMapKey = null; while (insertListener == null && childClass != null) { insertListener = entityInsertListeners.get(childClass); + listenersMapKey = childClass.getSuperclass() == null ? childClass : childClass.getSuperclass(); childClass = childClass.getSuperclass(); } if (insertListener != null) { - listeners.put(childClass, insertListener); + listeners.put(listenersMapKey, insertListener); } }