mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
optimize
This commit is contained in:
parent
93a3e1b925
commit
2116dff818
@ -23,9 +23,7 @@ import com.mybatisflex.core.dialect.DbType;
|
||||
import org.apache.ibatis.session.Configuration;
|
||||
import org.apache.ibatis.session.SqlSessionFactory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
@ -166,14 +164,35 @@ public class FlexGlobalConfig {
|
||||
* @param entityClass 实体class
|
||||
* @return UpdateListener
|
||||
*/
|
||||
public List<SetListener> getSupportedSetListener(Class<?> entityClass) {
|
||||
List<SetListener> list = new ArrayList<>();
|
||||
for (Class<?> registerClass : entitySetListeners.keySet()) {
|
||||
if (registerClass.isAssignableFrom(entityClass)) {
|
||||
list.add(entitySetListeners.get(registerClass));
|
||||
}
|
||||
public List<SetListener> getSupportedSetListener(Class<?> entityClass, boolean interfaceOnly) {
|
||||
|
||||
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;
|
||||
while (setListener == null && childClass != null) {
|
||||
setListener = entitySetListeners.get(childClass);
|
||||
childClass = childClass.getSuperclass();
|
||||
}
|
||||
|
||||
if (setListener != null) {
|
||||
listeners.put(childClass, setListener);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
@ -188,14 +207,35 @@ public class FlexGlobalConfig {
|
||||
* @param entityClass 实体class
|
||||
* @return UpdateListener
|
||||
*/
|
||||
public List<UpdateListener> getSupportedUpdateListener(Class<?> entityClass) {
|
||||
List<UpdateListener> list = new ArrayList<>();
|
||||
for (Class<?> registerClass : entityUpdateListeners.keySet()) {
|
||||
if (registerClass.isAssignableFrom(entityClass)) {
|
||||
list.add(entityUpdateListeners.get(registerClass));
|
||||
}
|
||||
public List<UpdateListener> getSupportedUpdateListener(Class<?> entityClass, boolean interfaceOnly) {
|
||||
|
||||
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;
|
||||
while (updateListener == null && childClass != null) {
|
||||
updateListener = entityUpdateListeners.get(childClass);
|
||||
childClass = childClass.getSuperclass();
|
||||
}
|
||||
|
||||
if (updateListener != null) {
|
||||
listeners.put(childClass, updateListener);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
@ -210,16 +250,38 @@ public class FlexGlobalConfig {
|
||||
* @param entityClass 实体class
|
||||
* @return InsertListener
|
||||
*/
|
||||
public List<InsertListener> getSupportedInsertListener(Class<?> entityClass) {
|
||||
List<InsertListener> list = new ArrayList<>();
|
||||
for (Class<?> registerClass : entityInsertListeners.keySet()) {
|
||||
if (registerClass.isAssignableFrom(entityClass)) {
|
||||
list.add(entityInsertListeners.get(registerClass));
|
||||
}
|
||||
public List<InsertListener> getSupportedInsertListener(Class<?> entityClass, boolean interfaceOnly) {
|
||||
|
||||
Map<Class<?>, InsertListener> map = new HashMap<>();
|
||||
if (!interfaceOnly) {
|
||||
doGetSupportedInsertListener(entityClass, map);
|
||||
}
|
||||
return list;
|
||||
|
||||
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;
|
||||
while (insertListener == null && childClass != null) {
|
||||
insertListener = entityInsertListeners.get(childClass);
|
||||
childClass = childClass.getSuperclass();
|
||||
}
|
||||
|
||||
if (insertListener != null) {
|
||||
listeners.put(childClass, insertListener);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public Object getNormalValueOfLogicDelete() {
|
||||
return normalValueOfLogicDelete;
|
||||
}
|
||||
|
||||
@ -36,8 +36,10 @@ import org.apache.ibatis.reflection.Reflector;
|
||||
import org.apache.ibatis.reflection.ReflectorFactory;
|
||||
import org.apache.ibatis.session.Configuration;
|
||||
import org.apache.ibatis.type.TypeHandler;
|
||||
import org.apache.ibatis.util.MapUtil;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class TableInfo {
|
||||
@ -768,27 +770,47 @@ public class TableInfo {
|
||||
}
|
||||
|
||||
|
||||
private static Map<Class<?>, List<InsertListener>> insertListenerCache = new ConcurrentHashMap<>();
|
||||
|
||||
public void invokeOnInsertListener(Object entity) {
|
||||
List<InsertListener> globalListeners = FlexGlobalConfig.getDefaultConfig().getSupportedInsertListener(entityClass);
|
||||
List<InsertListener> allInsertListeners = CollectionUtil.merge(onInsertListeners, globalListeners);
|
||||
Collections.sort(allInsertListeners);
|
||||
allInsertListeners.forEach(insertListener -> insertListener.onInsert(entity));
|
||||
List<InsertListener> listeners = MapUtil.computeIfAbsent(insertListenerCache, entityClass, aClass -> {
|
||||
List<InsertListener> globalListeners = FlexGlobalConfig.getDefaultConfig()
|
||||
.getSupportedInsertListener(entityClass, CollectionUtil.isNotEmpty(onInsertListeners));
|
||||
List<InsertListener> allListeners = CollectionUtil.merge(onInsertListeners, globalListeners);
|
||||
Collections.sort(allListeners);
|
||||
return allListeners;
|
||||
});
|
||||
listeners.forEach(insertListener -> insertListener.onInsert(entity));
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static Map<Class<?>, List<UpdateListener>> updateListenerCache = new ConcurrentHashMap<>();
|
||||
|
||||
public void invokeOnUpdateListener(Object entity) {
|
||||
List<UpdateListener> globalListeners = FlexGlobalConfig.getDefaultConfig().getSupportedUpdateListener(entityClass);
|
||||
List<UpdateListener> allUpdateListeners = CollectionUtil.merge(onUpdateListeners, globalListeners);
|
||||
Collections.sort(allUpdateListeners);
|
||||
allUpdateListeners.forEach(insertListener -> insertListener.onUpdate(entity));
|
||||
List<UpdateListener> listeners = MapUtil.computeIfAbsent(updateListenerCache, entityClass, aClass -> {
|
||||
List<UpdateListener> globalListeners = FlexGlobalConfig.getDefaultConfig()
|
||||
.getSupportedUpdateListener(entityClass, CollectionUtil.isNotEmpty(onUpdateListeners));
|
||||
List<UpdateListener> allListeners = CollectionUtil.merge(onUpdateListeners, globalListeners);
|
||||
Collections.sort(allListeners);
|
||||
return allListeners;
|
||||
});
|
||||
listeners.forEach(insertListener -> insertListener.onUpdate(entity));
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static Map<Class<?>, List<SetListener>> setListenerCache = new ConcurrentHashMap<>();
|
||||
|
||||
public Object invokeOnSetListener(Object entity, String property, Object value) {
|
||||
List<SetListener> globalListeners = FlexGlobalConfig.getDefaultConfig().getSupportedSetListener(entityClass);
|
||||
List<SetListener> allSetListeners = CollectionUtil.merge(onSetListeners, globalListeners);
|
||||
Collections.sort(allSetListeners);
|
||||
for (SetListener setListener : allSetListeners) {
|
||||
List<SetListener> listeners = MapUtil.computeIfAbsent(setListenerCache, entityClass, aClass -> {
|
||||
List<SetListener> globalListeners = FlexGlobalConfig.getDefaultConfig()
|
||||
.getSupportedSetListener(entityClass, CollectionUtil.isNotEmpty(onSetListeners));
|
||||
List<SetListener> allListeners = CollectionUtil.merge(onSetListeners, globalListeners);
|
||||
Collections.sort(allListeners);
|
||||
return allListeners;
|
||||
});
|
||||
for (SetListener setListener : listeners) {
|
||||
value = setListener.onSet(entity, property, value);
|
||||
}
|
||||
return value;
|
||||
|
||||
@ -56,7 +56,7 @@ public class ArrayUtil {
|
||||
public static <T> T[] concat(T[] first, T[] second) {
|
||||
if (first == null && second == null) {
|
||||
throw new IllegalArgumentException("not allow first and second are null.");
|
||||
} else if (isEmpty(first)) {
|
||||
} else if (isEmpty(first) && second != null) {
|
||||
return second;
|
||||
} else if (isEmpty(second)) {
|
||||
return first;
|
||||
|
||||
@ -47,7 +47,7 @@ public class CollectionUtil {
|
||||
public static <T> List<T> merge(List<T> list, List<T> other) {
|
||||
if (list == null && other == null) {
|
||||
return new ArrayList<>();
|
||||
} else if (isEmpty(other)) {
|
||||
} else if (isEmpty(other) && list != null) {
|
||||
return list;
|
||||
} else if (isEmpty(list)) {
|
||||
return other;
|
||||
|
||||
@ -0,0 +1,8 @@
|
||||
package com.mybatisflex.test;
|
||||
|
||||
public class JavaTester {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
}
|
||||
}
|
||||
@ -1,6 +1,5 @@
|
||||
package com.mybatisflex.test;
|
||||
|
||||
import com.mybatisflex.annotation.AbstractInsertListener;
|
||||
import com.mybatisflex.core.FlexGlobalConfig;
|
||||
import com.mybatisflex.core.MybatisFlexBootstrap;
|
||||
import org.apache.ibatis.logging.stdout.StdOutImpl;
|
||||
@ -30,22 +29,6 @@ public class ListenerTest implements WithAssertions {
|
||||
// 注册全局监听器
|
||||
FlexGlobalConfig defaultConfig = FlexGlobalConfig.getDefaultConfig();
|
||||
defaultConfig.registerInsertListener(new AgeHandleListener(), AgeAware.class);
|
||||
defaultConfig.registerInsertListener(new AbstractInsertListener<AgeAware>() {
|
||||
@Override
|
||||
public Class<AgeAware> supportType() {
|
||||
return AgeAware.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doInsert(AgeAware entity) {
|
||||
entity.setAge(entity.getAge() + 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int order() {
|
||||
return 20;
|
||||
}
|
||||
}, Account.class);
|
||||
|
||||
MybatisFlexBootstrap bootstrap = MybatisFlexBootstrap.getInstance()
|
||||
.setLogImpl(StdOutImpl.class)
|
||||
@ -61,6 +44,7 @@ public class ListenerTest implements WithAssertions {
|
||||
accountMapper.insert(account);
|
||||
|
||||
Account one = accountMapper.selectOneById(account.getId());
|
||||
assertThat(one.getAge()).isEqualTo(1);
|
||||
System.out.println(one);
|
||||
// assertThat(one.getAge()).isEqualTo(1);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user