From 29cf1945bee467fbf0fd9357dc0fd4ea66138c1e Mon Sep 17 00:00:00 2001 From: snow Date: Fri, 28 Apr 2023 16:08:51 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20#17=20=E5=85=A8=E5=B1=80=E7=9B=91?= =?UTF-8?q?=E5=90=AC=E5=99=A8=E7=88=B6=E7=B1=BB=E6=B3=A8=E5=86=8C=E6=98=AF?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E6=97=B6=E6=97=A0=E6=B3=95=E8=A7=A6=E5=8F=91?= =?UTF-8?q?=E7=9B=91=E5=90=AC=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mybatisflex/core/FlexGlobalConfig.java | 33 ++++++++++++- .../com/mybatisflex/core/table/TableInfo.java | 19 +------- .../mybatis-flex-native-test/pom.xml | 6 +++ .../java/com/mybatisflex/test/Account.java | 8 ++-- .../java/com/mybatisflex/test/AgeAware.java | 14 ++++++ .../mybatisflex/test/AgeHandleListener.java | 23 +++++++++ .../com/mybatisflex/test/ListenerTest.java | 47 +++++++++++++++++++ 7 files changed, 129 insertions(+), 21 deletions(-) create mode 100644 mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/AgeAware.java create mode 100644 mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/AgeHandleListener.java create mode 100644 mybatis-flex-test/mybatis-flex-native-test/src/test/java/com/mybatisflex/test/ListenerTest.java 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 f8520229..31588bac 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 @@ -16,10 +16,10 @@ package com.mybatisflex.core; import com.mybatisflex.annotation.InsertListener; +import com.mybatisflex.annotation.KeyType; import com.mybatisflex.annotation.SetListener; import com.mybatisflex.annotation.UpdateListener; import com.mybatisflex.core.dialect.DbType; -import com.mybatisflex.annotation.KeyType; import org.apache.ibatis.session.Configuration; import org.apache.ibatis.session.SqlSessionFactory; @@ -162,11 +162,42 @@ public class FlexGlobalConfig { return entityUpdateListeners.get(entityClass); } + /** + * 获取支持该 {@code entityClass} 的update监听器 + *

当registerClass是entityClass的本身或其超类时,则视为支持

+ * + * @param entityClass 实体class + * @return UpdateListener + */ + public UpdateListener getSupportedUpdateListener(Class entityClass) { + for (Class registerClass : entityUpdateListeners.keySet()) { + if (registerClass.isAssignableFrom(entityClass)) { + return entityUpdateListeners.get(registerClass); + } + } + return null; + } + public InsertListener getInsertListener(Class entityClass) { return entityInsertListeners.get(entityClass); } + /** + * 获取支持该 {@code entityClass} 的insert监听器 + *

当registerClass是entityClass的本身或其超类时,则视为支持

+ * + * @param entityClass 实体class + * @return InsertListener + */ + public InsertListener getSupportedInsertListener(Class entityClass) { + for (Class registerClass : entityInsertListeners.keySet()) { + if (registerClass.isAssignableFrom(entityClass)) { + return entityInsertListeners.get(registerClass); + } + } + return null; + } public Object getNormalValueOfLogicDelete() { return normalValueOfLogicDelete; diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java index e3a1db55..6692d020 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java @@ -778,15 +778,7 @@ public class TableInfo { return; } - - InsertListener globalInsertListener = null; - Class registerClass = entityClass; - - while (globalInsertListener == null && registerClass != Object.class && registerClass != null) { - globalInsertListener = FlexGlobalConfig.getDefaultConfig().getInsertListener(registerClass); - registerClass = registerClass.getSuperclass(); - } - + InsertListener globalInsertListener = FlexGlobalConfig.getDefaultConfig().getSupportedInsertListener(entityClass); if (globalInsertListener != null) { globalInsertListener.onInsert(entity); } @@ -799,14 +791,7 @@ public class TableInfo { return; } - UpdateListener globalUpdateListener = null; - Class registerClass = entityClass; - - while (globalUpdateListener == null && registerClass != Object.class && registerClass != null) { - globalUpdateListener = FlexGlobalConfig.getDefaultConfig().getUpdateListener(registerClass); - registerClass = registerClass.getSuperclass(); - } - + UpdateListener globalUpdateListener = FlexGlobalConfig.getDefaultConfig().getSupportedUpdateListener(entityClass); if (globalUpdateListener != null) { globalUpdateListener.onUpdate(entity); } diff --git a/mybatis-flex-test/mybatis-flex-native-test/pom.xml b/mybatis-flex-test/mybatis-flex-native-test/pom.xml index ee0adee1..82e5cb1c 100644 --- a/mybatis-flex-test/mybatis-flex-native-test/pom.xml +++ b/mybatis-flex-test/mybatis-flex-native-test/pom.xml @@ -93,6 +93,12 @@ junit test + + org.assertj + assertj-core + 3.22.0 + test + \ No newline at end of file diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/Account.java b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/Account.java index 1375cf39..ecc1ca35 100644 --- a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/Account.java +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/Account.java @@ -10,8 +10,8 @@ import java.util.Date; import java.util.HashMap; import java.util.Map; -@Table(value = "tb_account",dataSource = "ds2",onSet = AccountOnSetListener.class) -public class Account extends BaseAccount implements Serializable { +@Table(value = "tb_account", dataSource = "ds2", onSet = AccountOnSetListener.class) +public class Account extends BaseAccount implements Serializable, AgeAware { private static final long serialVersionUID = 1L; @@ -26,7 +26,7 @@ public class Account extends BaseAccount implements Serializable { @NotBlank private Date birthday; - @Column(typeHandler = Fastjson2TypeHandler.class,isLarge = true) + @Column(typeHandler = Fastjson2TypeHandler.class, isLarge = true) private Map options; @Column(isLogicDelete = true) @@ -49,10 +49,12 @@ public class Account extends BaseAccount implements Serializable { this.userName = userName; } + @Override public int getAge() { return age; } + @Override public void setAge(int age) { this.age = age; } diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/AgeAware.java b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/AgeAware.java new file mode 100644 index 00000000..0fbbc019 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/AgeAware.java @@ -0,0 +1,14 @@ +package com.mybatisflex.test; + +/** + * 有年龄的 + * + * @author snow + * @since 2023/4/28 + */ +public interface AgeAware { + + void setAge(int age); + + int getAge(); +} diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/AgeHandleListener.java b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/AgeHandleListener.java new file mode 100644 index 00000000..4ef6ec83 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/AgeHandleListener.java @@ -0,0 +1,23 @@ +package com.mybatisflex.test; + +import com.mybatisflex.annotation.InsertListener; + +/** + * 年龄处理监听器 + * + * @author snow + * @since 2023/4/28 + */ +public class AgeHandleListener implements InsertListener { + + @Override + public void onInsert(Object entity) { + if (entity instanceof AgeAware) { + AgeAware ageAware = (AgeAware) entity; + int age = ageAware.getAge(); + if (age < 0) { + ageAware.setAge(0); + } + } + } +} diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/test/java/com/mybatisflex/test/ListenerTest.java b/mybatis-flex-test/mybatis-flex-native-test/src/test/java/com/mybatisflex/test/ListenerTest.java new file mode 100644 index 00000000..00d4878f --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-native-test/src/test/java/com/mybatisflex/test/ListenerTest.java @@ -0,0 +1,47 @@ +package com.mybatisflex.test; + +import com.mybatisflex.core.FlexGlobalConfig; +import com.mybatisflex.core.MybatisFlexBootstrap; +import org.assertj.core.api.WithAssertions; +import org.junit.Test; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; + +import javax.sql.DataSource; +import java.util.Date; + +/** + * 监听器测试 + * + * @author snow + * @since 2023/4/28 + */ +public class ListenerTest implements WithAssertions { + + // 注册父类接口监听器 + @Test + public void onInsertInterface() throws Exception { + DataSource dataSource = new EmbeddedDatabaseBuilder() + .setType(EmbeddedDatabaseType.H2) + .addScript("schema.sql") + .build(); + // 注册全局监听器 + FlexGlobalConfig defaultConfig = FlexGlobalConfig.getDefaultConfig(); + defaultConfig.registerInsertListener(new AgeHandleListener(), AgeAware.class); + + MybatisFlexBootstrap bootstrap = MybatisFlexBootstrap.getInstance() + .setDataSource(dataSource) + .addMapper(AccountMapper.class) + .start(); + + AccountMapper accountMapper = bootstrap.getMapper(AccountMapper.class); + Account account = new Account(); + account.setAge(-2); + account.setUserName("on insert"); + account.setBirthday(new Date()); + accountMapper.insert(account); + + Account one = accountMapper.selectOneById(account.getId()); + assertThat(one.getAge()).isEqualTo(0); + } +}