mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 09:38:26 +08:00
Merge pull request #279 from ruansheng8/fix-EntityListener
修复实体类实现多层级的接口时监听器无法匹配问题
This commit is contained in:
commit
a78a08ac73
@ -17,6 +17,7 @@ package com.mybatisflex.core;
|
|||||||
|
|
||||||
import com.mybatisflex.annotation.InsertListener;
|
import com.mybatisflex.annotation.InsertListener;
|
||||||
import com.mybatisflex.annotation.KeyType;
|
import com.mybatisflex.annotation.KeyType;
|
||||||
|
import com.mybatisflex.annotation.Listener;
|
||||||
import com.mybatisflex.annotation.SetListener;
|
import com.mybatisflex.annotation.SetListener;
|
||||||
import com.mybatisflex.annotation.UpdateListener;
|
import com.mybatisflex.annotation.UpdateListener;
|
||||||
import com.mybatisflex.core.datasource.FlexDataSource;
|
import com.mybatisflex.core.datasource.FlexDataSource;
|
||||||
@ -26,11 +27,10 @@ import org.apache.ibatis.session.Configuration;
|
|||||||
import org.apache.ibatis.session.SqlSessionFactory;
|
import org.apache.ibatis.session.SqlSessionFactory;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 全局配置文件
|
* 全局配置文件
|
||||||
@ -200,44 +200,28 @@ public class FlexGlobalConfig {
|
|||||||
* @param entityClass 实体class
|
* @param entityClass 实体class
|
||||||
* @return UpdateListener
|
* @return UpdateListener
|
||||||
*/
|
*/
|
||||||
public List<SetListener> getSupportedSetListener(Class<?> entityClass, boolean interfaceOnly) {
|
public List<SetListener> getSupportedSetListener(Class<?> entityClass) {
|
||||||
|
return this.findSupportedListeners(entityClass, this.entitySetListeners);
|
||||||
Map<Class<?>, SetListener> map = new HashMap<>();
|
|
||||||
if (!interfaceOnly) {
|
|
||||||
doGetSupportedSetListener(entityClass, map);
|
|
||||||
}
|
|
||||||
|
|
||||||
while (entityClass.getSuperclass() != null) {
|
|
||||||
Class<?>[] interfaces = entityClass.getInterfaces();
|
|
||||||
for (Class<?> interfaceClass : interfaces) {
|
|
||||||
doGetSupportedSetListener(interfaceClass, map);
|
|
||||||
}
|
|
||||||
entityClass = entityClass.getSuperclass();
|
|
||||||
}
|
|
||||||
|
|
||||||
return new ArrayList<>(map.values());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void doGetSupportedSetListener(Class<?> childClass, Map<Class<?>, 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(listenersMapKey, setListener);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public UpdateListener getUpdateListener(Class<?> entityClass) {
|
public UpdateListener getUpdateListener(Class<?> entityClass) {
|
||||||
return entityUpdateListeners.get(entityClass);
|
return entityUpdateListeners.get(entityClass);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查找支持该 {@code entityClass} 的监听器
|
||||||
|
* @param entityClass 实体class
|
||||||
|
* @param listenerMap 监听器map
|
||||||
|
* @return 符合条件的监听器
|
||||||
|
* @param <T> 监听器类型
|
||||||
|
*/
|
||||||
|
public <T extends Listener> List<T> findSupportedListeners(Class<?> entityClass, Map<Class<?>, T> listenerMap) {
|
||||||
|
return listenerMap.entrySet().stream()
|
||||||
|
.filter(entry -> entry.getKey().isAssignableFrom(entityClass))
|
||||||
|
.map(Map.Entry::getValue)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取支持该 {@code entityClass} 的update监听器
|
* 获取支持该 {@code entityClass} 的update监听器
|
||||||
* <p>当registerClass是entityClass的本身或其超类时,则视为支持</p>
|
* <p>当registerClass是entityClass的本身或其超类时,则视为支持</p>
|
||||||
@ -245,37 +229,8 @@ public class FlexGlobalConfig {
|
|||||||
* @param entityClass 实体class
|
* @param entityClass 实体class
|
||||||
* @return UpdateListener
|
* @return UpdateListener
|
||||||
*/
|
*/
|
||||||
public List<UpdateListener> getSupportedUpdateListener(Class<?> entityClass, boolean interfaceOnly) {
|
public List<UpdateListener> getSupportedUpdateListener(Class<?> entityClass) {
|
||||||
|
return this.findSupportedListeners(entityClass, this.entityUpdateListeners);
|
||||||
Map<Class<?>, UpdateListener> map = new HashMap<>();
|
|
||||||
if (!interfaceOnly) {
|
|
||||||
doGetSupportedUpdateListener(entityClass, map);
|
|
||||||
}
|
|
||||||
|
|
||||||
while (entityClass.getSuperclass() != null) {
|
|
||||||
Class<?>[] interfaces = entityClass.getInterfaces();
|
|
||||||
for (Class<?> interfaceClass : interfaces) {
|
|
||||||
doGetSupportedUpdateListener(interfaceClass, map);
|
|
||||||
}
|
|
||||||
entityClass = entityClass.getSuperclass();
|
|
||||||
}
|
|
||||||
|
|
||||||
return new ArrayList<>(map.values());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private void doGetSupportedUpdateListener(Class<?> childClass, Map<Class<?>, 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(listenersMapKey, updateListener);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -290,40 +245,10 @@ public class FlexGlobalConfig {
|
|||||||
* @param entityClass 实体class
|
* @param entityClass 实体class
|
||||||
* @return InsertListener
|
* @return InsertListener
|
||||||
*/
|
*/
|
||||||
public List<InsertListener> getSupportedInsertListener(Class<?> entityClass, boolean interfaceOnly) {
|
public List<InsertListener> getSupportedInsertListener(Class<?> entityClass) {
|
||||||
|
return this.findSupportedListeners(entityClass, this.entityInsertListeners);
|
||||||
Map<Class<?>, InsertListener> map = new HashMap<>();
|
|
||||||
if (!interfaceOnly) {
|
|
||||||
doGetSupportedInsertListener(entityClass, map);
|
|
||||||
}
|
|
||||||
|
|
||||||
while (entityClass.getSuperclass() != null) {
|
|
||||||
Class<?>[] interfaces = entityClass.getInterfaces();
|
|
||||||
for (Class<?> interfaceClass : interfaces) {
|
|
||||||
doGetSupportedInsertListener(interfaceClass, map);
|
|
||||||
}
|
|
||||||
entityClass = entityClass.getSuperclass();
|
|
||||||
}
|
|
||||||
|
|
||||||
return new ArrayList<>(map.values());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void doGetSupportedInsertListener(Class<?> childClass, Map<Class<?>, 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(listenersMapKey, insertListener);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public Object getNormalValueOfLogicDelete() {
|
public Object getNormalValueOfLogicDelete() {
|
||||||
return normalValueOfLogicDelete;
|
return normalValueOfLogicDelete;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1372,7 +1372,7 @@ public class TableInfo {
|
|||||||
public void invokeOnInsertListener(Object entity) {
|
public void invokeOnInsertListener(Object entity) {
|
||||||
List<InsertListener> listeners = MapUtil.computeIfAbsent(insertListenerCache, entityClass, aClass -> {
|
List<InsertListener> listeners = MapUtil.computeIfAbsent(insertListenerCache, entityClass, aClass -> {
|
||||||
List<InsertListener> globalListeners = FlexGlobalConfig.getDefaultConfig()
|
List<InsertListener> globalListeners = FlexGlobalConfig.getDefaultConfig()
|
||||||
.getSupportedInsertListener(entityClass, CollectionUtil.isNotEmpty(onInsertListeners));
|
.getSupportedInsertListener(entityClass);
|
||||||
List<InsertListener> allListeners = CollectionUtil.merge(onInsertListeners, globalListeners);
|
List<InsertListener> allListeners = CollectionUtil.merge(onInsertListeners, globalListeners);
|
||||||
Collections.sort(allListeners);
|
Collections.sort(allListeners);
|
||||||
return allListeners;
|
return allListeners;
|
||||||
@ -1386,7 +1386,7 @@ public class TableInfo {
|
|||||||
public void invokeOnUpdateListener(Object entity) {
|
public void invokeOnUpdateListener(Object entity) {
|
||||||
List<UpdateListener> listeners = MapUtil.computeIfAbsent(updateListenerCache, entityClass, aClass -> {
|
List<UpdateListener> listeners = MapUtil.computeIfAbsent(updateListenerCache, entityClass, aClass -> {
|
||||||
List<UpdateListener> globalListeners = FlexGlobalConfig.getDefaultConfig()
|
List<UpdateListener> globalListeners = FlexGlobalConfig.getDefaultConfig()
|
||||||
.getSupportedUpdateListener(entityClass, CollectionUtil.isNotEmpty(onUpdateListeners));
|
.getSupportedUpdateListener(entityClass);
|
||||||
List<UpdateListener> allListeners = CollectionUtil.merge(onUpdateListeners, globalListeners);
|
List<UpdateListener> allListeners = CollectionUtil.merge(onUpdateListeners, globalListeners);
|
||||||
Collections.sort(allListeners);
|
Collections.sort(allListeners);
|
||||||
return allListeners;
|
return allListeners;
|
||||||
@ -1400,7 +1400,7 @@ public class TableInfo {
|
|||||||
public Object invokeOnSetListener(Object entity, String property, Object value) {
|
public Object invokeOnSetListener(Object entity, String property, Object value) {
|
||||||
List<SetListener> listeners = MapUtil.computeIfAbsent(setListenerCache, entityClass, aClass -> {
|
List<SetListener> listeners = MapUtil.computeIfAbsent(setListenerCache, entityClass, aClass -> {
|
||||||
List<SetListener> globalListeners = FlexGlobalConfig.getDefaultConfig()
|
List<SetListener> globalListeners = FlexGlobalConfig.getDefaultConfig()
|
||||||
.getSupportedSetListener(entityClass, CollectionUtil.isNotEmpty(onSetListeners));
|
.getSupportedSetListener(entityClass);
|
||||||
List<SetListener> allListeners = CollectionUtil.merge(onSetListeners, globalListeners);
|
List<SetListener> allListeners = CollectionUtil.merge(onSetListeners, globalListeners);
|
||||||
Collections.sort(allListeners);
|
Collections.sort(allListeners);
|
||||||
return allListeners;
|
return allListeners;
|
||||||
|
|||||||
@ -0,0 +1,77 @@
|
|||||||
|
package com.mybatisflex.core;
|
||||||
|
|
||||||
|
import com.mybatisflex.annotation.UpdateListener;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 全局配置测试用例
|
||||||
|
*
|
||||||
|
* @author 阮胜
|
||||||
|
* @since 2024-02-19
|
||||||
|
*/
|
||||||
|
public class FlexGlobalConfigTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetSupportedUpdateListener1() {
|
||||||
|
FlexGlobalConfig flexGlobalConfig = new FlexGlobalConfig();
|
||||||
|
flexGlobalConfig.registerUpdateListener(new CustomEntityListener(), Staff.class);
|
||||||
|
List<UpdateListener> supportedUpdateListener = flexGlobalConfig.getSupportedUpdateListener(Staff.class);
|
||||||
|
Assert.assertEquals(supportedUpdateListener.size(), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetSupportedUpdateListener2() {
|
||||||
|
FlexGlobalConfig flexGlobalConfig = new FlexGlobalConfig();
|
||||||
|
flexGlobalConfig.registerUpdateListener(new CustomEntityListener(), IBaseEntity.class);
|
||||||
|
List<UpdateListener> supportedUpdateListener = flexGlobalConfig.getSupportedUpdateListener(Staff.class);
|
||||||
|
Assert.assertEquals(supportedUpdateListener.size(), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetSupportedUpdateListener3() {
|
||||||
|
FlexGlobalConfig flexGlobalConfig = new FlexGlobalConfig();
|
||||||
|
flexGlobalConfig.registerUpdateListener(new CustomEntityListener(), Dept.class);
|
||||||
|
List<UpdateListener> supportedUpdateListener = flexGlobalConfig.getSupportedUpdateListener(Dept.class);
|
||||||
|
Assert.assertEquals(supportedUpdateListener.size(), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetSupportedUpdateListener4() {
|
||||||
|
FlexGlobalConfig flexGlobalConfig = new FlexGlobalConfig();
|
||||||
|
flexGlobalConfig.registerUpdateListener(new CustomEntityListener(), IGeneralEntity.class);
|
||||||
|
List<UpdateListener> supportedUpdateListener = flexGlobalConfig.getSupportedUpdateListener(Dept.class);
|
||||||
|
Assert.assertEquals(supportedUpdateListener.size(), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetSupportedUpdateListener5() {
|
||||||
|
FlexGlobalConfig flexGlobalConfig = new FlexGlobalConfig();
|
||||||
|
flexGlobalConfig.registerUpdateListener(new CustomEntityListener(), IBaseEntity.class);
|
||||||
|
List<UpdateListener> supportedUpdateListener = flexGlobalConfig.getSupportedUpdateListener(Dept.class);
|
||||||
|
Assert.assertEquals(supportedUpdateListener.size(), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class CustomEntityListener implements UpdateListener {
|
||||||
|
@Override
|
||||||
|
public void onUpdate(Object entity) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface IBaseEntity {
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface IGeneralEntity extends IBaseEntity {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Staff implements IBaseEntity {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Dept implements IGeneralEntity {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@ -47,7 +47,7 @@ class ListenerTest {
|
|||||||
|
|
||||||
List<InsertListener> insertListeners = MapUtil.computeIfAbsent(tempOnInsertListenerMap, AccountMissingListenerTestModel.class, aClass -> {
|
List<InsertListener> insertListeners = MapUtil.computeIfAbsent(tempOnInsertListenerMap, AccountMissingListenerTestModel.class, aClass -> {
|
||||||
List<InsertListener> globalListeners = FlexGlobalConfig.getDefaultConfig()
|
List<InsertListener> globalListeners = FlexGlobalConfig.getDefaultConfig()
|
||||||
.getSupportedInsertListener(AccountMissingListenerTestModel.class, CollectionUtil.isNotEmpty(tableInfo.getOnInsertListeners()));
|
.getSupportedInsertListener(AccountMissingListenerTestModel.class);
|
||||||
List<InsertListener> allListeners = CollectionUtil.merge(tableInfo.getOnInsertListeners(), globalListeners);
|
List<InsertListener> allListeners = CollectionUtil.merge(tableInfo.getOnInsertListeners(), globalListeners);
|
||||||
Collections.sort(allListeners);
|
Collections.sort(allListeners);
|
||||||
return allListeners;
|
return allListeners;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user