mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
feat: 支持全局注册多个监听器。https://gitee.com/mybatis-flex/mybatis-flex/issues/I9FF24
This commit is contained in:
parent
c7cc85500d
commit
6afc5f1f7b
@ -27,6 +27,7 @@ import org.apache.ibatis.session.Configuration;
|
||||
import org.apache.ibatis.session.SqlSessionFactory;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
@ -66,9 +67,9 @@ public class FlexGlobalConfig {
|
||||
/**
|
||||
* entity 的监听器
|
||||
*/
|
||||
private Map<Class<?>, SetListener> entitySetListeners = new ConcurrentHashMap<>();
|
||||
private Map<Class<?>, UpdateListener> entityUpdateListeners = new ConcurrentHashMap<>();
|
||||
private Map<Class<?>, InsertListener> entityInsertListeners = new ConcurrentHashMap<>();
|
||||
private Map<Class<?>, List<SetListener>> entitySetListeners = new ConcurrentHashMap<>();
|
||||
private Map<Class<?>, List<UpdateListener>> entityUpdateListeners = new ConcurrentHashMap<>();
|
||||
private Map<Class<?>, List<InsertListener>> entityInsertListeners = new ConcurrentHashMap<>();
|
||||
|
||||
|
||||
/**
|
||||
@ -147,49 +148,49 @@ public class FlexGlobalConfig {
|
||||
this.keyConfig = keyConfig;
|
||||
}
|
||||
|
||||
public Map<Class<?>, SetListener> getEntitySetListeners() {
|
||||
public Map<Class<?>, List<SetListener>> getEntitySetListeners() {
|
||||
return entitySetListeners;
|
||||
}
|
||||
|
||||
public void setEntitySetListeners(Map<Class<?>, SetListener> entitySetListeners) {
|
||||
public void setEntitySetListeners(Map<Class<?>, List<SetListener>> entitySetListeners) {
|
||||
this.entitySetListeners = entitySetListeners;
|
||||
}
|
||||
|
||||
public Map<Class<?>, UpdateListener> getEntityUpdateListeners() {
|
||||
public Map<Class<?>, List<UpdateListener>> getEntityUpdateListeners() {
|
||||
return entityUpdateListeners;
|
||||
}
|
||||
|
||||
public void setEntityUpdateListeners(Map<Class<?>, UpdateListener> entityUpdateListeners) {
|
||||
public void setEntityUpdateListeners(Map<Class<?>, List<UpdateListener>> entityUpdateListeners) {
|
||||
this.entityUpdateListeners = entityUpdateListeners;
|
||||
}
|
||||
|
||||
public Map<Class<?>, InsertListener> getEntityInsertListeners() {
|
||||
public Map<Class<?>, List<InsertListener>> getEntityInsertListeners() {
|
||||
return entityInsertListeners;
|
||||
}
|
||||
|
||||
public void setEntityInsertListeners(Map<Class<?>, InsertListener> entityInsertListeners) {
|
||||
public void setEntityInsertListeners(Map<Class<?>, List<InsertListener>> entityInsertListeners) {
|
||||
this.entityInsertListeners = entityInsertListeners;
|
||||
}
|
||||
|
||||
public void registerSetListener(SetListener listener, Class<?>... classes) {
|
||||
for (Class<?> aClass : classes) {
|
||||
entitySetListeners.put(aClass, listener);
|
||||
entitySetListeners.computeIfAbsent(aClass, k -> new ArrayList<>()).add(listener);
|
||||
}
|
||||
}
|
||||
|
||||
public void registerUpdateListener(UpdateListener listener, Class<?>... classes) {
|
||||
for (Class<?> aClass : classes) {
|
||||
entityUpdateListeners.put(aClass, listener);
|
||||
entityUpdateListeners.computeIfAbsent(aClass, k -> new ArrayList<>()).add(listener);
|
||||
}
|
||||
}
|
||||
|
||||
public void registerInsertListener(InsertListener listener, Class<?>... classes) {
|
||||
for (Class<?> aClass : classes) {
|
||||
entityInsertListeners.put(aClass, listener);
|
||||
entityInsertListeners.computeIfAbsent(aClass, k -> new ArrayList<>()).add(listener);
|
||||
}
|
||||
}
|
||||
|
||||
public SetListener getSetListener(Class<?> entityClass) {
|
||||
public List<SetListener> getSetListener(Class<?> entityClass) {
|
||||
return entitySetListeners.get(entityClass);
|
||||
}
|
||||
|
||||
@ -204,21 +205,23 @@ public class FlexGlobalConfig {
|
||||
return this.findSupportedListeners(entityClass, this.entitySetListeners);
|
||||
}
|
||||
|
||||
public UpdateListener getUpdateListener(Class<?> entityClass) {
|
||||
public List<UpdateListener> getUpdateListener(Class<?> entityClass) {
|
||||
return entityUpdateListeners.get(entityClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查找支持该 {@code entityClass} 的监听器
|
||||
*
|
||||
* @param entityClass 实体class
|
||||
* @param listenerMap 监听器map
|
||||
* @return 符合条件的监听器
|
||||
* @param <T> 监听器类型
|
||||
* @return 符合条件的监听器
|
||||
*/
|
||||
public <T extends Listener> List<T> findSupportedListeners(Class<?> entityClass, Map<Class<?>, T> listenerMap) {
|
||||
return listenerMap.entrySet().stream()
|
||||
public <T extends Listener> List<T> findSupportedListeners(Class<?> entityClass, Map<Class<?>, List<T>> listenerMap) {
|
||||
return listenerMap.entrySet()
|
||||
.stream()
|
||||
.filter(entry -> entry.getKey().isAssignableFrom(entityClass))
|
||||
.map(Map.Entry::getValue)
|
||||
.flatMap(e -> e.getValue().stream())
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@ -234,7 +237,7 @@ public class FlexGlobalConfig {
|
||||
}
|
||||
|
||||
|
||||
public InsertListener getInsertListener(Class<?> entityClass) {
|
||||
public List<InsertListener> getInsertListener(Class<?> entityClass) {
|
||||
return entityInsertListeners.get(entityClass);
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user