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.Configuration;
|
||||||
import org.apache.ibatis.session.SqlSessionFactory;
|
import org.apache.ibatis.session.SqlSessionFactory;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -166,14 +164,35 @@ public class FlexGlobalConfig {
|
|||||||
* @param entityClass 实体class
|
* @param entityClass 实体class
|
||||||
* @return UpdateListener
|
* @return UpdateListener
|
||||||
*/
|
*/
|
||||||
public List<SetListener> getSupportedSetListener(Class<?> entityClass) {
|
public List<SetListener> getSupportedSetListener(Class<?> entityClass, boolean interfaceOnly) {
|
||||||
List<SetListener> list = new ArrayList<>();
|
|
||||||
for (Class<?> registerClass : entitySetListeners.keySet()) {
|
Map<Class<?>, SetListener> map = new HashMap<>();
|
||||||
if (registerClass.isAssignableFrom(entityClass)) {
|
if (!interfaceOnly) {
|
||||||
list.add(entitySetListeners.get(registerClass));
|
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
|
* @param entityClass 实体class
|
||||||
* @return UpdateListener
|
* @return UpdateListener
|
||||||
*/
|
*/
|
||||||
public List<UpdateListener> getSupportedUpdateListener(Class<?> entityClass) {
|
public List<UpdateListener> getSupportedUpdateListener(Class<?> entityClass, boolean interfaceOnly) {
|
||||||
List<UpdateListener> list = new ArrayList<>();
|
|
||||||
for (Class<?> registerClass : entityUpdateListeners.keySet()) {
|
Map<Class<?>, UpdateListener> map = new HashMap<>();
|
||||||
if (registerClass.isAssignableFrom(entityClass)) {
|
if (!interfaceOnly) {
|
||||||
list.add(entityUpdateListeners.get(registerClass));
|
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
|
* @param entityClass 实体class
|
||||||
* @return InsertListener
|
* @return InsertListener
|
||||||
*/
|
*/
|
||||||
public List<InsertListener> getSupportedInsertListener(Class<?> entityClass) {
|
public List<InsertListener> getSupportedInsertListener(Class<?> entityClass, boolean interfaceOnly) {
|
||||||
List<InsertListener> list = new ArrayList<>();
|
|
||||||
for (Class<?> registerClass : entityInsertListeners.keySet()) {
|
Map<Class<?>, InsertListener> map = new HashMap<>();
|
||||||
if (registerClass.isAssignableFrom(entityClass)) {
|
if (!interfaceOnly) {
|
||||||
list.add(entityInsertListeners.get(registerClass));
|
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() {
|
public Object getNormalValueOfLogicDelete() {
|
||||||
return normalValueOfLogicDelete;
|
return normalValueOfLogicDelete;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -36,8 +36,10 @@ import org.apache.ibatis.reflection.Reflector;
|
|||||||
import org.apache.ibatis.reflection.ReflectorFactory;
|
import org.apache.ibatis.reflection.ReflectorFactory;
|
||||||
import org.apache.ibatis.session.Configuration;
|
import org.apache.ibatis.session.Configuration;
|
||||||
import org.apache.ibatis.type.TypeHandler;
|
import org.apache.ibatis.type.TypeHandler;
|
||||||
|
import org.apache.ibatis.util.MapUtil;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class TableInfo {
|
public class TableInfo {
|
||||||
@ -768,27 +770,47 @@ public class TableInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static Map<Class<?>, List<InsertListener>> insertListenerCache = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
public void invokeOnInsertListener(Object entity) {
|
public void invokeOnInsertListener(Object entity) {
|
||||||
List<InsertListener> globalListeners = FlexGlobalConfig.getDefaultConfig().getSupportedInsertListener(entityClass);
|
List<InsertListener> listeners = MapUtil.computeIfAbsent(insertListenerCache, entityClass, aClass -> {
|
||||||
List<InsertListener> allInsertListeners = CollectionUtil.merge(onInsertListeners, globalListeners);
|
List<InsertListener> globalListeners = FlexGlobalConfig.getDefaultConfig()
|
||||||
Collections.sort(allInsertListeners);
|
.getSupportedInsertListener(entityClass, CollectionUtil.isNotEmpty(onInsertListeners));
|
||||||
allInsertListeners.forEach(insertListener -> insertListener.onInsert(entity));
|
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) {
|
public void invokeOnUpdateListener(Object entity) {
|
||||||
List<UpdateListener> globalListeners = FlexGlobalConfig.getDefaultConfig().getSupportedUpdateListener(entityClass);
|
List<UpdateListener> listeners = MapUtil.computeIfAbsent(updateListenerCache, entityClass, aClass -> {
|
||||||
List<UpdateListener> allUpdateListeners = CollectionUtil.merge(onUpdateListeners, globalListeners);
|
List<UpdateListener> globalListeners = FlexGlobalConfig.getDefaultConfig()
|
||||||
Collections.sort(allUpdateListeners);
|
.getSupportedUpdateListener(entityClass, CollectionUtil.isNotEmpty(onUpdateListeners));
|
||||||
allUpdateListeners.forEach(insertListener -> insertListener.onUpdate(entity));
|
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) {
|
public Object invokeOnSetListener(Object entity, String property, Object value) {
|
||||||
List<SetListener> globalListeners = FlexGlobalConfig.getDefaultConfig().getSupportedSetListener(entityClass);
|
List<SetListener> listeners = MapUtil.computeIfAbsent(setListenerCache, entityClass, aClass -> {
|
||||||
List<SetListener> allSetListeners = CollectionUtil.merge(onSetListeners, globalListeners);
|
List<SetListener> globalListeners = FlexGlobalConfig.getDefaultConfig()
|
||||||
Collections.sort(allSetListeners);
|
.getSupportedSetListener(entityClass, CollectionUtil.isNotEmpty(onSetListeners));
|
||||||
for (SetListener setListener : allSetListeners) {
|
List<SetListener> allListeners = CollectionUtil.merge(onSetListeners, globalListeners);
|
||||||
|
Collections.sort(allListeners);
|
||||||
|
return allListeners;
|
||||||
|
});
|
||||||
|
for (SetListener setListener : listeners) {
|
||||||
value = setListener.onSet(entity, property, value);
|
value = setListener.onSet(entity, property, value);
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
|
|||||||
@ -56,7 +56,7 @@ public class ArrayUtil {
|
|||||||
public static <T> T[] concat(T[] first, T[] second) {
|
public static <T> T[] concat(T[] first, T[] second) {
|
||||||
if (first == null && second == null) {
|
if (first == null && second == null) {
|
||||||
throw new IllegalArgumentException("not allow first and second are null.");
|
throw new IllegalArgumentException("not allow first and second are null.");
|
||||||
} else if (isEmpty(first)) {
|
} else if (isEmpty(first) && second != null) {
|
||||||
return second;
|
return second;
|
||||||
} else if (isEmpty(second)) {
|
} else if (isEmpty(second)) {
|
||||||
return first;
|
return first;
|
||||||
|
|||||||
@ -47,7 +47,7 @@ public class CollectionUtil {
|
|||||||
public static <T> List<T> merge(List<T> list, List<T> other) {
|
public static <T> List<T> merge(List<T> list, List<T> other) {
|
||||||
if (list == null && other == null) {
|
if (list == null && other == null) {
|
||||||
return new ArrayList<>();
|
return new ArrayList<>();
|
||||||
} else if (isEmpty(other)) {
|
} else if (isEmpty(other) && list != null) {
|
||||||
return list;
|
return list;
|
||||||
} else if (isEmpty(list)) {
|
} else if (isEmpty(list)) {
|
||||||
return other;
|
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;
|
package com.mybatisflex.test;
|
||||||
|
|
||||||
import com.mybatisflex.annotation.AbstractInsertListener;
|
|
||||||
import com.mybatisflex.core.FlexGlobalConfig;
|
import com.mybatisflex.core.FlexGlobalConfig;
|
||||||
import com.mybatisflex.core.MybatisFlexBootstrap;
|
import com.mybatisflex.core.MybatisFlexBootstrap;
|
||||||
import org.apache.ibatis.logging.stdout.StdOutImpl;
|
import org.apache.ibatis.logging.stdout.StdOutImpl;
|
||||||
@ -30,22 +29,6 @@ public class ListenerTest implements WithAssertions {
|
|||||||
// 注册全局监听器
|
// 注册全局监听器
|
||||||
FlexGlobalConfig defaultConfig = FlexGlobalConfig.getDefaultConfig();
|
FlexGlobalConfig defaultConfig = FlexGlobalConfig.getDefaultConfig();
|
||||||
defaultConfig.registerInsertListener(new AgeHandleListener(), AgeAware.class);
|
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()
|
MybatisFlexBootstrap bootstrap = MybatisFlexBootstrap.getInstance()
|
||||||
.setLogImpl(StdOutImpl.class)
|
.setLogImpl(StdOutImpl.class)
|
||||||
@ -61,6 +44,7 @@ public class ListenerTest implements WithAssertions {
|
|||||||
accountMapper.insert(account);
|
accountMapper.insert(account);
|
||||||
|
|
||||||
Account one = accountMapper.selectOneById(account.getId());
|
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