mirror of
https://gitee.com/orangeform/orange-admin.git
synced 2026-01-17 18:46:36 +08:00
commit:1.5多应用版本
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
package com.orange.demo.common.core.annotation;
|
||||
|
||||
import com.orange.demo.common.core.util.DataSourceResolver;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 基于自定义解析规则的多数据源注解。主要用于标注Service的实现类。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-09-24
|
||||
*/
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface MyDataSourceResolver {
|
||||
|
||||
/**
|
||||
* 多数据源路由键解析接口的Class。
|
||||
* @return 多数据源路由键解析接口的Class。
|
||||
*/
|
||||
Class<? extends DataSourceResolver> resolver();
|
||||
|
||||
/**
|
||||
* DataSourceResolver.resovle方法的入参。
|
||||
* @return DataSourceResolver.resovle方法的入参。
|
||||
*/
|
||||
String arg() default "";
|
||||
}
|
||||
@@ -1,82 +0,0 @@
|
||||
package com.orange.demo.common.core.aop;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.orange.demo.common.core.constant.ApplicationConstant;
|
||||
import com.orange.demo.common.core.util.MyCommonUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Pointcut;
|
||||
import org.slf4j.MDC;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 记录接口的链路traceId、请求参数、应答数据、错误信息和调用时长。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-09-24
|
||||
*/
|
||||
@Aspect
|
||||
@Component
|
||||
@Order(1)
|
||||
@Slf4j
|
||||
public class AccessLogAspect {
|
||||
|
||||
/**
|
||||
* 所有controller方法。
|
||||
*/
|
||||
@Pointcut("execution(public * com.orange.demo..controller..*(..))")
|
||||
public void controllerPointCut() {
|
||||
// 空注释,避免sonar警告
|
||||
}
|
||||
|
||||
@Around("controllerPointCut()")
|
||||
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||
HttpServletRequest request =
|
||||
((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
|
||||
// 请求流水号
|
||||
String traceId = MyCommonUtil.generateUuid();
|
||||
HttpServletResponse response =
|
||||
((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getResponse();
|
||||
response.setHeader(ApplicationConstant.HTTP_HEADER_TRACE_ID, traceId);
|
||||
MDC.put(ApplicationConstant.HTTP_HEADER_TRACE_ID, traceId);
|
||||
long start = System.currentTimeMillis();
|
||||
// 获取方法参数
|
||||
List<Object> httpReqArgs = new ArrayList<>();
|
||||
Object[] args = joinPoint.getArgs();
|
||||
for (Object object : args) {
|
||||
if (!(object instanceof HttpServletRequest)
|
||||
&& !(object instanceof HttpServletResponse)
|
||||
&& !(object instanceof MultipartFile)) {
|
||||
httpReqArgs.add(object);
|
||||
}
|
||||
}
|
||||
String url = request.getRequestURI();
|
||||
String params = JSON.toJSONString(httpReqArgs);
|
||||
log.info("开始请求,traceId={}, url={}, reqData={}", traceId, url, params);
|
||||
Object result = null;
|
||||
try {
|
||||
// 调用原来的方法
|
||||
result = joinPoint.proceed();
|
||||
} catch (Exception e) {
|
||||
log.error("请求报错,traceId={}, url={}, reqData={}, error={}", traceId, url, params, e.getMessage());
|
||||
throw e;
|
||||
} finally {
|
||||
// 获取应答报文及接口处理耗时
|
||||
String respData = result == null ? null : JSON.toJSONString(result);
|
||||
log.info("请求完成, traceId={}, url={},elapse={}ms, respData={}",
|
||||
traceId, url, (System.currentTimeMillis() - start), respData);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.orange.demo.common.core.aop;
|
||||
|
||||
import com.orange.demo.common.core.annotation.MyDataSource;
|
||||
import com.orange.demo.common.core.config.DataSourceContextHolder;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Pointcut;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 多数据源AOP切面处理类。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-09-24
|
||||
*/
|
||||
@Aspect
|
||||
@Component
|
||||
@Order(1)
|
||||
@Slf4j
|
||||
public class DataSourceAspect {
|
||||
|
||||
/**
|
||||
* 所有配置MyDataSource注解的Service实现类。
|
||||
*/
|
||||
@Pointcut("execution(public * com.orange.demo..service..*(..)) " +
|
||||
"&& @target(com.orange.demo.common.core.annotation.MyDataSource)")
|
||||
public void datasourcePointCut() {
|
||||
// 空注释,避免sonar警告
|
||||
}
|
||||
|
||||
@Around("datasourcePointCut()")
|
||||
public Object around(ProceedingJoinPoint point) throws Throwable {
|
||||
Class<?> clazz = point.getTarget().getClass();
|
||||
MyDataSource ds = clazz.getAnnotation(MyDataSource.class);
|
||||
// 通过判断 DataSource 中的值来判断当前方法应用哪个数据源
|
||||
DataSourceContextHolder.setDataSourceType(ds.value());
|
||||
log.debug("set datasource is " + ds.value());
|
||||
try {
|
||||
return point.proceed();
|
||||
} finally {
|
||||
DataSourceContextHolder.clear();
|
||||
log.debug("clean datasource");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.orange.demo.common.core.aop;
|
||||
|
||||
import com.orange.demo.common.core.annotation.MyDataSourceResolver;
|
||||
import com.orange.demo.common.core.util.DataSourceResolver;
|
||||
import com.orange.demo.common.core.config.DataSourceContextHolder;
|
||||
import com.orange.demo.common.core.util.ApplicationContextHolder;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Pointcut;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 基于自定义解析规则的多数据源AOP切面处理类。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-09-24
|
||||
*/
|
||||
@Aspect
|
||||
@Component
|
||||
@Order(1)
|
||||
@Slf4j
|
||||
public class DataSourceResolveAspect {
|
||||
|
||||
private Map<Class<? extends DataSourceResolver>, DataSourceResolver> resolverMap = new HashMap<>();
|
||||
|
||||
/**
|
||||
* 所有配置 MyDataSource 注解的Service。
|
||||
*/
|
||||
@Pointcut("execution(public * com.orange.demo..service..*(..)) " +
|
||||
"&& @target(com.orange.demo.common.core.annotation.MyDataSourceResolver)")
|
||||
public void datasourceResolverPointCut() {
|
||||
// 空注释,避免sonar警告
|
||||
}
|
||||
|
||||
@Around("datasourceResolverPointCut()")
|
||||
public Object around(ProceedingJoinPoint point) throws Throwable {
|
||||
Class<?> clazz = point.getTarget().getClass();
|
||||
MyDataSourceResolver dsr = clazz.getAnnotation(MyDataSourceResolver.class);
|
||||
Class<? extends DataSourceResolver> resolverClass = dsr.resolver();
|
||||
DataSourceResolver resolver = resolverMap.get(resolverClass);
|
||||
if (resolver == null) {
|
||||
resolver = ApplicationContextHolder.getBean(resolverClass);
|
||||
resolverMap.put(resolverClass, resolver);
|
||||
}
|
||||
int type = resolver.resolve(dsr.arg());
|
||||
// 通过判断 DataSource 中的值来判断当前方法应用哪个数据源
|
||||
DataSourceContextHolder.setDataSourceType(type);
|
||||
log.debug("set datasource is " + type);
|
||||
try {
|
||||
return point.proceed();
|
||||
} finally {
|
||||
DataSourceContextHolder.clear();
|
||||
log.debug("clean datasource");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,7 @@ import org.springframework.stereotype.Component;
|
||||
@Component
|
||||
@Order(Ordered.LOWEST_PRECEDENCE - 1)
|
||||
@Slf4j
|
||||
public class DictCacheSyncAop {
|
||||
public class DictCacheSyncAspect {
|
||||
|
||||
/**
|
||||
* BaseDictService 字典服务父类中的字典数据增删改的方法。
|
||||
@@ -4,6 +4,7 @@ import cn.hutool.core.util.ReflectUtil;
|
||||
import com.orange.demo.common.core.constant.GlobalDeletedFlag;
|
||||
import com.orange.demo.common.core.exception.MyRuntimeException;
|
||||
import com.orange.demo.common.core.cache.DictionaryCache;
|
||||
import com.orange.demo.common.core.object.TokenData;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
@@ -35,26 +36,6 @@ public abstract class BaseDictService<M, K> extends BaseService<M, K> implements
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否在服务启动的时候加载。子类可以重载该方法,并在需要的时候手工调用loadCachedData加载数据。
|
||||
*
|
||||
* @return true表示启动即可加载数据,false需要手动调用loadCachedData进行加载。
|
||||
*/
|
||||
@Override
|
||||
public boolean loadOnStartup() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 在系统启动时,加载全部数据到内存,缓存的key只能为映射表的主键。
|
||||
*/
|
||||
@Override
|
||||
public void loadCachedData() {
|
||||
if (loadOnStartup()) {
|
||||
reloadCachedData(false);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 重新加载数据库中所有当前表数据到系统内存。
|
||||
*
|
||||
@@ -88,6 +69,9 @@ public abstract class BaseDictService<M, K> extends BaseService<M, K> implements
|
||||
throw new MyRuntimeException(e);
|
||||
}
|
||||
}
|
||||
if (tenantIdField != null) {
|
||||
ReflectUtil.setFieldValue(data, tenantIdField, TokenData.takeFromRequest().getTenantId());
|
||||
}
|
||||
mapper().insert(data);
|
||||
return data;
|
||||
}
|
||||
@@ -102,6 +86,9 @@ public abstract class BaseDictService<M, K> extends BaseService<M, K> implements
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public boolean update(M data, M originalData) {
|
||||
if (tenantIdField != null) {
|
||||
ReflectUtil.setFieldValue(data, tenantIdField, TokenData.takeFromRequest().getTenantId());
|
||||
}
|
||||
if (deletedFlagFieldName != null) {
|
||||
try {
|
||||
setDeletedFlagMethod.invoke(data, GlobalDeletedFlag.NORMAL);
|
||||
@@ -259,6 +246,18 @@ public abstract class BaseDictService<M, K> extends BaseService<M, K> implements
|
||||
this.dictionaryCache.invalidate(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据字典对象将数据从缓存中删除。
|
||||
*
|
||||
* @param data 字典数据。
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public void removeDictionaryCacheByModel(M data) {
|
||||
K key = (K) ReflectUtil.getFieldValue(data, idFieldName);
|
||||
this.dictionaryCache.invalidate(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取缓存中的数据数量。
|
||||
*
|
||||
|
||||
@@ -57,7 +57,7 @@ public abstract class BaseService<M, K> implements IBaseService<M, K> {
|
||||
*/
|
||||
protected String idFieldName;
|
||||
/**
|
||||
* 当前Service关联的主数据表中数据字段名称。
|
||||
* 当前Service关联的主数据表中主键列名称。
|
||||
*/
|
||||
protected String idColumnName;
|
||||
/**
|
||||
@@ -68,6 +68,18 @@ public abstract class BaseService<M, K> implements IBaseService<M, K> {
|
||||
* 当前Service关联的主数据表中逻辑删除字段名称。
|
||||
*/
|
||||
protected String deletedFlagColumnName;
|
||||
/**
|
||||
* 当前Service关联的主Model对象租户Id字段。
|
||||
*/
|
||||
protected Field tenantIdField;
|
||||
/**
|
||||
* 当前Service关联的主Model对象租户Id字段名称。
|
||||
*/
|
||||
protected String tenantIdFieldName;
|
||||
/**
|
||||
* 当前Service关联的主数据表中租户Id列名称。
|
||||
*/
|
||||
protected String tenantIdColumnName;
|
||||
/**
|
||||
* 当前Job服务源主表Model对象最后更新时间字段名称。
|
||||
*/
|
||||
@@ -157,6 +169,12 @@ public abstract class BaseService<M, K> implements IBaseService<M, K> {
|
||||
setDeletedFlagMethod = ReflectUtil.getMethod(
|
||||
modelClass, "set" + StringUtils.capitalize(deletedFlagFieldName), Integer.class);
|
||||
}
|
||||
if (tenantIdFieldName == null && null != field.getAnnotation(TenantFilterColumn.class)) {
|
||||
tenantIdField = field;
|
||||
tenantIdFieldName = field.getName();
|
||||
Column c = field.getAnnotation(Column.class);
|
||||
tenantIdColumnName = c == null ? tenantIdFieldName : c.name();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -179,10 +197,12 @@ public abstract class BaseService<M, K> implements IBaseService<M, K> {
|
||||
return mapper().deleteByPrimaryKey(id) == 1;
|
||||
}
|
||||
try {
|
||||
Example e = new Example(modelClass);
|
||||
Example.Criteria c = e.createCriteria().andEqualTo(idFieldName, id);
|
||||
c.andEqualTo(deletedFlagFieldName, GlobalDeletedFlag.NORMAL);
|
||||
M data = modelClass.newInstance();
|
||||
setDeletedFlagMethod.invoke(data, GlobalDeletedFlag.DELETED);
|
||||
setIdFieldMethod.invoke(data, id);
|
||||
return mapper().updateByPrimaryKeySelective(data) == 1;
|
||||
return mapper().updateByExampleSelective(data, e) == 1;
|
||||
} catch (Exception ex) {
|
||||
log.error("Failed to call reflection method in BaseService.removeById.", ex);
|
||||
throw new MyRuntimeException(ex);
|
||||
@@ -543,6 +563,7 @@ public abstract class BaseService<M, K> implements IBaseService<M, K> {
|
||||
/**
|
||||
* 集成所有与主表实体对象相关的关联数据列表。包括一对一、字典、一对多和多对多聚合运算等。
|
||||
* 也可以根据实际需求,单独调用该函数所包含的各个数据集成函数。
|
||||
* NOTE: 该方法内执行的SQL将禁用数据权限过滤。
|
||||
*
|
||||
* @param resultList 主表实体对象列表。数据集成将直接作用于该对象列表。
|
||||
* @param relationParam 实体对象数据组装的参数构建器。
|
||||
@@ -552,30 +573,76 @@ public abstract class BaseService<M, K> implements IBaseService<M, K> {
|
||||
if (relationParam == null || CollectionUtils.isEmpty(resultList)) {
|
||||
return;
|
||||
}
|
||||
// 集成本地一对一和字段级别的数据关联。
|
||||
boolean buildOneToOne = relationParam.isBuildOneToOne() || relationParam.isBuildOneToOneWithDict();
|
||||
// 这里集成一对一关联。
|
||||
if (buildOneToOne) {
|
||||
this.buildOneToOneForDataList(resultList, relationParam.isBuildOneToOneWithDict());
|
||||
boolean dataFilterValue = GlobalThreadLocal.setDataFilter(false);
|
||||
try {
|
||||
// 集成本地一对一和字段级别的数据关联。
|
||||
boolean buildOneToOne = relationParam.isBuildOneToOne() || relationParam.isBuildOneToOneWithDict();
|
||||
// 这里集成一对一关联。
|
||||
if (buildOneToOne) {
|
||||
this.buildOneToOneForDataList(resultList, relationParam.isBuildOneToOneWithDict());
|
||||
}
|
||||
// 集成一对多关联
|
||||
if (relationParam.isBuildOneToMany()) {
|
||||
this.buildOneToManyForDataList(resultList);
|
||||
}
|
||||
// 这里集成字典关联
|
||||
if (relationParam.isBuildDict()) {
|
||||
// 构建常量字典关联关系
|
||||
this.buildConstDictForDataList(resultList);
|
||||
this.buildDictForDataList(resultList, buildOneToOne);
|
||||
}
|
||||
// 组装本地聚合计算关联数据
|
||||
if (relationParam.isBuildRelationAggregation()) {
|
||||
// 处理多对多场景下,根据主表的结果,进行从表聚合数据的计算。
|
||||
this.buildManyToManyAggregationForDataList(resultList, buildAggregationAdditionalWhereCriteria());
|
||||
// 处理多一多场景下,根据主表的结果,进行从表聚合数据的计算。
|
||||
this.buildOneToManyAggregationForDataList(resultList, buildAggregationAdditionalWhereCriteria());
|
||||
}
|
||||
} finally {
|
||||
GlobalThreadLocal.setDataFilter(dataFilterValue);
|
||||
}
|
||||
// 这里集成字典关联
|
||||
if (relationParam.isBuildDict()) {
|
||||
// 构建常量字典关联关系
|
||||
this.buildConstDictForDataList(resultList);
|
||||
this.buildDictForDataList(resultList, buildOneToOne);
|
||||
}
|
||||
|
||||
/**
|
||||
* 该函数主要用于对查询结果的批量导出。不同于支持分页的列表查询,批量导出没有分页机制,
|
||||
* 因此在导出数据量较大的情况下,很容易给数据库的内存、CPU和IO带来较大的压力。而通过
|
||||
* 我们的分批处理,可以极大的规避该问题的出现几率。调整batchSize的大小,也可以有效的
|
||||
* 改善运行效率。
|
||||
* 我们目前的处理机制是,先从主表取出所有符合条件的主表数据,这样可以避免分批处理时,
|
||||
* 后面几批数据,因为skip过多而带来的效率问题。因为是单表过滤,不会给数据库带来过大的压力。
|
||||
* 之后再在主表结果集数据上进行分批级联处理。
|
||||
* 集成所有与主表实体对象相关的关联数据列表。包括一对一、字典、一对多和多对多聚合运算等。
|
||||
* 也可以根据实际需求,单独调用该函数所包含的各个数据集成函数。
|
||||
* NOTE: 该方法内执行的SQL将禁用数据权限过滤。
|
||||
*
|
||||
* @param resultList 主表实体对象列表。数据集成将直接作用于该对象列表。
|
||||
* @param relationParam 实体对象数据组装的参数构建器。
|
||||
* @param batchSize 每批集成的记录数量。小于等于时将不做分批处理。
|
||||
*/
|
||||
@Override
|
||||
public void buildRelationForDataList(List<M> resultList, MyRelationParam relationParam, int batchSize) {
|
||||
if (CollectionUtils.isEmpty(resultList)) {
|
||||
return;
|
||||
}
|
||||
// 组装本地聚合计算关联数据
|
||||
if (relationParam.isBuildRelationAggregation()) {
|
||||
// 处理多对多场景下,根据主表的结果,进行从表聚合数据的计算。
|
||||
this.buildManyToManyAggregationForDataList(resultList, buildAggregationAdditionalWhereCriteria());
|
||||
// 处理多一多场景下,根据主表的结果,进行从表聚合数据的计算。
|
||||
this.buildOneToManyAggregationForDataList(resultList, buildAggregationAdditionalWhereCriteria());
|
||||
if (batchSize <= 0) {
|
||||
this.buildRelationForDataList(resultList, relationParam);
|
||||
return;
|
||||
}
|
||||
int totalCount = resultList.size();
|
||||
int fromIndex = 0;
|
||||
int toIndex = Math.min(batchSize, totalCount);
|
||||
while (toIndex > fromIndex) {
|
||||
List<M> subResultList = resultList.subList(fromIndex, toIndex);
|
||||
this.buildRelationForDataList(subResultList, relationParam);
|
||||
fromIndex = toIndex;
|
||||
toIndex = Math.min(batchSize + fromIndex, totalCount);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 集成所有与主表实体对象相关的关联数据对象。包括一对一、字典、一对多和多对多聚合运算等。
|
||||
* 也可以根据实际需求,单独调用该函数所包含的各个数据集成函数。
|
||||
* NOTE: 该方法内执行的SQL将禁用数据权限过滤。
|
||||
*
|
||||
* @param dataObject 主表实体对象。数据集成将直接作用于该对象。
|
||||
* @param relationParam 实体对象数据组装的参数构建器。
|
||||
@@ -586,26 +653,35 @@ public abstract class BaseService<M, K> implements IBaseService<M, K> {
|
||||
if (dataObject == null || relationParam == null) {
|
||||
return;
|
||||
}
|
||||
// 集成本地一对一和字段级别的数据关联。
|
||||
boolean buildOneToOne = relationParam.isBuildOneToOne() || relationParam.isBuildOneToOneWithDict();
|
||||
if (buildOneToOne) {
|
||||
this.buildOneToOneForData(dataObject, relationParam.isBuildOneToOneWithDict());
|
||||
}
|
||||
if (relationParam.isBuildDict()) {
|
||||
// 构建常量字典关联关系
|
||||
this.buildConstDictForData(dataObject);
|
||||
// 构建本地数据字典关联关系。
|
||||
this.buildDictForData(dataObject, buildOneToOne);
|
||||
}
|
||||
// 组装本地聚合计算关联数据
|
||||
if (relationParam.isBuildRelationAggregation()) {
|
||||
// 开始处理多对多场景。
|
||||
buildManyToManyAggregationForData(dataObject, buildAggregationAdditionalWhereCriteria());
|
||||
// 构建一对多场景
|
||||
buildOneToManyAggregationForData(dataObject, buildAggregationAdditionalWhereCriteria());
|
||||
}
|
||||
if (relationParam.isBuildRelationManyToMany()) {
|
||||
this.buildRelationManyToMany(dataObject);
|
||||
boolean dataFilterValue = GlobalThreadLocal.setDataFilter(false);
|
||||
try {
|
||||
// 集成本地一对一和字段级别的数据关联。
|
||||
boolean buildOneToOne = relationParam.isBuildOneToOne() || relationParam.isBuildOneToOneWithDict();
|
||||
if (buildOneToOne) {
|
||||
this.buildOneToOneForData(dataObject, relationParam.isBuildOneToOneWithDict());
|
||||
}
|
||||
// 集成一对多关联
|
||||
if (relationParam.isBuildOneToMany()) {
|
||||
this.buildOneToManyForData(dataObject);
|
||||
}
|
||||
if (relationParam.isBuildDict()) {
|
||||
// 构建常量字典关联关系
|
||||
this.buildConstDictForData(dataObject);
|
||||
// 构建本地数据字典关联关系。
|
||||
this.buildDictForData(dataObject, buildOneToOne);
|
||||
}
|
||||
// 组装本地聚合计算关联数据
|
||||
if (relationParam.isBuildRelationAggregation()) {
|
||||
// 开始处理多对多场景。
|
||||
buildManyToManyAggregationForData(dataObject, buildAggregationAdditionalWhereCriteria());
|
||||
// 构建一对多场景
|
||||
buildOneToManyAggregationForData(dataObject, buildAggregationAdditionalWhereCriteria());
|
||||
}
|
||||
if (relationParam.isBuildRelationManyToMany()) {
|
||||
this.buildRelationManyToMany(dataObject);
|
||||
}
|
||||
} finally {
|
||||
GlobalThreadLocal.setDataFilter(dataFilterValue);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,18 +12,6 @@ import java.util.List;
|
||||
*/
|
||||
public interface IBaseDictService<M, K> extends IBaseService<M, K> {
|
||||
|
||||
/**
|
||||
* 是否在服务启动的时候加载。子类可以重载该方法,并在需要的时候手工调用loadCachedData加载数据。
|
||||
*
|
||||
* @return true表示启动即可加载数据,false需要手动调用loadCachedData进行加载。
|
||||
*/
|
||||
boolean loadOnStartup();
|
||||
|
||||
/**
|
||||
* 在系统启动时,加载全部数据到内存,缓存的key只能为映射表的主键。
|
||||
*/
|
||||
void loadCachedData();
|
||||
|
||||
/**
|
||||
* 重新加载数据库中所有当前表数据到系统内存。
|
||||
*
|
||||
@@ -77,6 +65,13 @@ public interface IBaseDictService<M, K> extends IBaseService<M, K> {
|
||||
*/
|
||||
void removeDictionaryCache(K id);
|
||||
|
||||
/**
|
||||
* 根据字典对象将数据从缓存中删除。
|
||||
*
|
||||
* @param data 字典数据。
|
||||
*/
|
||||
void removeDictionaryCacheByModel(M data);
|
||||
|
||||
/**
|
||||
* 获取缓存中的数据数量。
|
||||
*
|
||||
|
||||
@@ -199,6 +199,24 @@ public interface IBaseService<M, K> {
|
||||
*/
|
||||
void buildRelationForDataList(List<M> resultList, MyRelationParam relationParam);
|
||||
|
||||
/**
|
||||
* 该函数主要用于对查询结果的批量导出。不同于支持分页的列表查询,批量导出没有分页机制,
|
||||
* 因此在导出数据量较大的情况下,很容易给数据库的内存、CPU和IO带来较大的压力。而通过
|
||||
* 我们的分批处理,可以极大的规避该问题的出现几率。调整batchSize的大小,也可以有效的
|
||||
* 改善运行效率。
|
||||
* 我们目前的处理机制是,先从主表取出所有符合条件的主表数据,这样可以避免分批处理时,
|
||||
* 后面几批数据,因为skip过多而带来的效率问题。因为是单表过滤,不会给数据库带来过大的压力。
|
||||
* 之后再在主表结果集数据上进行分批级联处理。
|
||||
* 集成所有与主表实体对象相关的关联数据列表。包括一对一、字典、一对多和多对多聚合运算等。
|
||||
* 也可以根据实际需求,单独调用该函数所包含的各个数据集成函数。
|
||||
* NOTE: 该方法内执行的SQL将禁用数据权限过滤。
|
||||
*
|
||||
* @param resultList 主表实体对象列表。数据集成将直接作用于该对象列表。
|
||||
* @param relationParam 实体对象数据组装的参数构建器。
|
||||
* @param batchSize 每批集成的记录数量。小于等于时将不做分批处理。
|
||||
*/
|
||||
void buildRelationForDataList(List<M> resultList, MyRelationParam relationParam, int batchSize);
|
||||
|
||||
/**
|
||||
* 集成所有与主表实体对象相关的关联数据对象。包括一对一、字典、一对多和多对多聚合运算等。
|
||||
* 也可以根据实际需求,单独调用该函数所包含的各个数据集成函数。
|
||||
|
||||
@@ -60,6 +60,45 @@ public class MapTreeDictionaryCache<K, V> extends MapDictionaryCache<K, V> {
|
||||
this.parentIdGetter = parentIdGetter;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 重新加载,先清空原有数据,在执行putAll的操作。
|
||||
*
|
||||
* @param dataList 待缓存的数据列表。
|
||||
* @param force true则强制刷新,如果false,当缓存中存在数据时不刷新。
|
||||
*/
|
||||
@Override
|
||||
public void reload(List<V> dataList, boolean force) {
|
||||
if (!force && this.getCount() > 0) {
|
||||
return;
|
||||
}
|
||||
String exceptionMessage;
|
||||
try {
|
||||
if (lock.readLock().tryLock(TIMEOUT, TimeUnit.MILLISECONDS)) {
|
||||
try {
|
||||
dataMap.clear();
|
||||
allTreeMap.clear();
|
||||
dataList.forEach(data -> {
|
||||
K id = idGetter.apply(data);
|
||||
dataMap.put(id, data);
|
||||
K parentId = parentIdGetter.apply(data);
|
||||
allTreeMap.put(parentId, data);
|
||||
});
|
||||
} finally {
|
||||
lock.readLock().unlock();
|
||||
}
|
||||
} else {
|
||||
throw new TimeoutException();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
exceptionMessage = String.format(
|
||||
"LOCK Operation of [MapDictionaryCache::getInList] encountered EXCEPTION [%s] for DICT.",
|
||||
e.getClass().getSimpleName());
|
||||
log.warn(exceptionMessage);
|
||||
throw new MapCacheAccessException(exceptionMessage, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取该父主键的子数据列表。
|
||||
*
|
||||
@@ -103,8 +142,9 @@ public class MapTreeDictionaryCache<K, V> extends MapDictionaryCache<K, V> {
|
||||
try {
|
||||
if (lock.readLock().tryLock(TIMEOUT, TimeUnit.MILLISECONDS)) {
|
||||
try {
|
||||
super.putAll(dataList);
|
||||
dataList.forEach(data -> {
|
||||
K id = idGetter.apply(data);
|
||||
dataMap.put(id, data);
|
||||
K parentId = parentIdGetter.apply(data);
|
||||
allTreeMap.remove(parentId, data);
|
||||
allTreeMap.put(parentId, data);
|
||||
@@ -136,7 +176,7 @@ public class MapTreeDictionaryCache<K, V> extends MapDictionaryCache<K, V> {
|
||||
try {
|
||||
if (lock.readLock().tryLock(TIMEOUT, TimeUnit.MILLISECONDS)) {
|
||||
try {
|
||||
super.put(id, data);
|
||||
dataMap.put(id, data);
|
||||
K parentId = parentIdGetter.apply(data);
|
||||
allTreeMap.remove(parentId, data);
|
||||
allTreeMap.put(parentId, data);
|
||||
@@ -168,7 +208,7 @@ public class MapTreeDictionaryCache<K, V> extends MapDictionaryCache<K, V> {
|
||||
try {
|
||||
if (lock.readLock().tryLock(TIMEOUT, TimeUnit.MILLISECONDS)) {
|
||||
try {
|
||||
v = super.invalidate(id);
|
||||
v = dataMap.remove(id);
|
||||
if (v != null) {
|
||||
K parentId = parentIdGetter.apply(v);
|
||||
allTreeMap.remove(parentId, v);
|
||||
@@ -233,7 +273,7 @@ public class MapTreeDictionaryCache<K, V> extends MapDictionaryCache<K, V> {
|
||||
try {
|
||||
if (lock.readLock().tryLock(TIMEOUT, TimeUnit.MILLISECONDS)) {
|
||||
try {
|
||||
super.invalidateAll();
|
||||
dataMap.clear();
|
||||
allTreeMap.clear();
|
||||
} finally {
|
||||
lock.readLock().unlock();
|
||||
|
||||
@@ -12,6 +12,10 @@ public final class ApplicationConstant {
|
||||
* 为字典表数据缓存时,缓存名称的固定后缀。
|
||||
*/
|
||||
public static final String DICT_CACHE_NAME_SUFFIX = "-DICT";
|
||||
/**
|
||||
* 为树形字典表数据缓存时,缓存名称的固定后缀。
|
||||
*/
|
||||
public static final String TREE_DICT_CACHE_NAME_SUFFIX = "-TREE-DICT";
|
||||
/**
|
||||
* 图片文件上传的父目录。
|
||||
*/
|
||||
@@ -44,6 +48,12 @@ public final class ApplicationConstant {
|
||||
* 请求头跟踪id名。
|
||||
*/
|
||||
public static final String HTTP_HEADER_TRACE_ID = "traceId";
|
||||
/**
|
||||
* 操作日志的数据源类型。仅当前服务为多数据源时使用。
|
||||
* 在common-log模块中,SysOperationLogServiceImpl的MyDataSource注解一定要使用该参数。
|
||||
* 在多数据源的业务服务中,DataSourceType的常量一定要包含该值,多数据源的配置中,也一定要有与该值匹配的数据源Bean。
|
||||
*/
|
||||
public static final int OPERATION_LOG_DATASOURCE_TYPE = 1000;
|
||||
/**
|
||||
* 重要说明:该值为项目生成后的缺省密钥,仅为使用户可以快速上手并跑通流程。
|
||||
* 在实际的应用中,一定要为不同的项目或服务,自行生成公钥和私钥,并将 PRIVATE_KEY 的引用改为服务的配置项。
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
package com.orange.demo.common.core.listener;
|
||||
|
||||
import com.orange.demo.common.core.base.service.BaseDictService;
|
||||
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 应用程序启动后的事件监听对象。主要负责加载Model之间的字典关联和一对一关联所对应的Service结构关系。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-09-24
|
||||
*/
|
||||
@Component
|
||||
public class LoadCachedDataListener implements ApplicationListener<ApplicationReadyEvent> {
|
||||
|
||||
@SuppressWarnings("all")
|
||||
@Override
|
||||
public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
|
||||
Map<String, BaseDictService> serviceMap =
|
||||
applicationReadyEvent.getApplicationContext().getBeansOfType(BaseDictService.class);
|
||||
for (Map.Entry<String, BaseDictService> e : serviceMap.entrySet()) {
|
||||
e.getValue().loadCachedData();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -70,7 +70,7 @@ public class LocalUpDownloader extends BaseUpDownloader {
|
||||
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) {
|
||||
int i = bis.read(buff);
|
||||
while (i != -1) {
|
||||
os.write(buff, 0, buff.length);
|
||||
os.write(buff, 0, i);
|
||||
os.flush();
|
||||
i = bis.read(buff);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.orange.demo.common.core.util;
|
||||
|
||||
/**
|
||||
* 基于自定义解析规则的多数据源解析接口。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-09-24
|
||||
*/
|
||||
public interface DataSourceResolver {
|
||||
|
||||
/**
|
||||
* 动态解析方法。实现类可以根据当前的请求,或者上下文环境进行动态解析。
|
||||
*
|
||||
* @param arg 可选的入参。MyDataSourceResolver注解中的arg参数。
|
||||
* @return 返回用于多数据源切换的类型值。DataSourceResolveAspect 切面方法会根据该返回值和配置信息,进行多数据源切换。
|
||||
*/
|
||||
int resolve(String arg);
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package com.orange.demo.common.redis.cache;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import com.orange.demo.common.core.constant.ApplicationConstant;
|
||||
import com.orange.demo.common.core.exception.RedisCacheAccessException;
|
||||
import com.google.common.collect.LinkedListMultimap;
|
||||
import com.google.common.collect.Multimap;
|
||||
@@ -79,7 +80,8 @@ public class RedisTreeDictionaryCache<K, V> extends RedisDictionaryCache<K, V> {
|
||||
Function<V, K> idGetter,
|
||||
Function<V, K> parentIdGetter) {
|
||||
super(redissonClient, dictionaryName, valueClazz, idGetter);
|
||||
this.allTreeMap = redissonClient.getListMultimap(dictionaryName + "-TREE-DICT");
|
||||
this.allTreeMap = redissonClient.getListMultimap(
|
||||
dictionaryName + ApplicationConstant.TREE_DICT_CACHE_NAME_SUFFIX);
|
||||
this.parentIdGetter = parentIdGetter;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user