mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
add global entity listener support
This commit is contained in:
parent
354f2efdb0
commit
0bb3a8115b
@ -15,11 +15,15 @@
|
||||
*/
|
||||
package com.mybatisflex.core;
|
||||
|
||||
import com.mybatisflex.annotation.InsertListener;
|
||||
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;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
@ -47,6 +51,11 @@ public class FlexGlobalConfig {
|
||||
private KeyConfig keyConfig;
|
||||
|
||||
|
||||
private Map<Class<?>, SetListener> entitySetListeners = new ConcurrentHashMap<>();
|
||||
private Map<Class<?>, UpdateListener> entityUpdateListeners = new ConcurrentHashMap<>();
|
||||
private Map<Class<?>, InsertListener> entityInsertListeners = new ConcurrentHashMap<>();
|
||||
|
||||
|
||||
public DbType getDbType() {
|
||||
return dbType;
|
||||
}
|
||||
@ -79,6 +88,73 @@ public class FlexGlobalConfig {
|
||||
this.keyConfig = keyConfig;
|
||||
}
|
||||
|
||||
public Map<Class<?>, SetListener> getEntitySetListeners() {
|
||||
return entitySetListeners;
|
||||
}
|
||||
|
||||
public void setEntitySetListeners(Map<Class<?>, SetListener> entitySetListeners) {
|
||||
this.entitySetListeners = entitySetListeners;
|
||||
}
|
||||
|
||||
public Map<Class<?>, UpdateListener> getEntityUpdateListeners() {
|
||||
return entityUpdateListeners;
|
||||
}
|
||||
|
||||
public void setEntityUpdateListeners(Map<Class<?>, UpdateListener> entityUpdateListeners) {
|
||||
this.entityUpdateListeners = entityUpdateListeners;
|
||||
}
|
||||
|
||||
public Map<Class<?>, InsertListener> getEntityInsertListeners() {
|
||||
return entityInsertListeners;
|
||||
}
|
||||
|
||||
public void setEntityInsertListeners(Map<Class<?>, InsertListener> entityInsertListeners) {
|
||||
this.entityInsertListeners = entityInsertListeners;
|
||||
}
|
||||
|
||||
|
||||
public void registerEntityListener(SetListener listener, Class<?>... classes) {
|
||||
for (Class<?> aClass : classes) {
|
||||
entitySetListeners.put(aClass, listener);
|
||||
}
|
||||
}
|
||||
|
||||
public void registerEntityListener(UpdateListener listener, Class<?>... classes) {
|
||||
for (Class<?> aClass : classes) {
|
||||
entityUpdateListeners.put(aClass, listener);
|
||||
}
|
||||
}
|
||||
|
||||
public void registerEntityListener(InsertListener listener, Class<?>... classes) {
|
||||
for (Class<?> aClass : classes) {
|
||||
entityInsertListeners.put(aClass, listener);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public SetListener getSetListener(Class<?> entityClass){
|
||||
return entitySetListeners.get(entityClass);
|
||||
}
|
||||
|
||||
|
||||
public UpdateListener getUpdateListener(Class<?> entityClass){
|
||||
return entityUpdateListeners.get(entityClass);
|
||||
}
|
||||
|
||||
|
||||
public InsertListener getInsertListener(Class<?> entityClass){
|
||||
return entityInsertListeners.get(entityClass);
|
||||
}
|
||||
|
||||
|
||||
public static ConcurrentHashMap<String, FlexGlobalConfig> getGlobalConfigs() {
|
||||
return globalConfigs;
|
||||
}
|
||||
|
||||
public static void setGlobalConfigs(ConcurrentHashMap<String, FlexGlobalConfig> globalConfigs) {
|
||||
FlexGlobalConfig.globalConfigs = globalConfigs;
|
||||
}
|
||||
|
||||
/**
|
||||
* 对应的是 注解 {@link com.mybatisflex.annotation.Id} 的配置
|
||||
*/
|
||||
@ -128,7 +204,7 @@ public class FlexGlobalConfig {
|
||||
public static FlexGlobalConfig getConfig(Configuration configuration) {
|
||||
return globalConfigs.get(configuration.getEnvironment().getId());
|
||||
}
|
||||
|
||||
|
||||
public static FlexGlobalConfig getConfig(String environmentId) {
|
||||
return globalConfigs.get(environmentId);
|
||||
}
|
||||
|
||||
@ -20,6 +20,7 @@ import com.mybatisflex.annotation.KeyType;
|
||||
import com.mybatisflex.annotation.SetListener;
|
||||
import com.mybatisflex.annotation.UpdateListener;
|
||||
import com.mybatisflex.core.FlexConsts;
|
||||
import com.mybatisflex.core.FlexGlobalConfig;
|
||||
import com.mybatisflex.core.exception.FlexExceptions;
|
||||
import com.mybatisflex.core.javassist.ModifyAttrsRecord;
|
||||
import com.mybatisflex.core.mybatis.TypeHandlerObject;
|
||||
@ -663,19 +664,39 @@ public class TableInfo {
|
||||
public void invokeOnInsertListener(Object entity) {
|
||||
if (onInsertListener != null) {
|
||||
onInsertListener.onInsert(entity);
|
||||
return;
|
||||
}
|
||||
|
||||
InsertListener globalInsertListener = FlexGlobalConfig.getDefaultConfig().getInsertListener(entityClass);
|
||||
if (globalInsertListener != null) {
|
||||
globalInsertListener.onInsert(entity);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void invokeOnUpdateListener(Object entity) {
|
||||
if (onUpdateListener != null) {
|
||||
onUpdateListener.onUpdate(entity);
|
||||
return;
|
||||
}
|
||||
|
||||
UpdateListener globalUpdateListener = FlexGlobalConfig.getDefaultConfig().getUpdateListener(entityClass);
|
||||
if (globalUpdateListener != null) {
|
||||
globalUpdateListener.onUpdate(entity);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public Object invokeOnSetListener(Object entity, String property, Object value) {
|
||||
if (onSetListener != null) {
|
||||
return onSetListener.onSet(entity, property, value);
|
||||
}
|
||||
|
||||
SetListener globalSetListener = FlexGlobalConfig.getDefaultConfig().getSetListener(entityClass);
|
||||
if (globalSetListener != null) {
|
||||
return globalSetListener.onSet(entity, property, value);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user