mirror of
https://gitee.com/orangeform/orange-admin.git
synced 2026-01-17 18:46:36 +08:00
commit:同步2.0版本
This commit is contained in:
@@ -96,9 +96,9 @@
|
||||
<version>${druid.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>tk.mybatis</groupId>
|
||||
<artifactId>mapper-spring-boot-starter</artifactId>
|
||||
<version>${mybatis-mapper.version}</version>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
<version>${mybatisplus.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.pagehelper</groupId>
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
package com.orange.demo.common.core.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 主要用于标记逻辑删除字段。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-09-24
|
||||
*/
|
||||
@Target({ElementType.FIELD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface DeletedFlagColumn {
|
||||
|
||||
}
|
||||
@@ -10,6 +10,8 @@ import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 字典缓存同步的AOP。该AOP的优先级必须比事务切面的优先级高,因此会在事务外执行该切面的代码。
|
||||
*
|
||||
@@ -39,23 +41,23 @@ public class DictCacheSyncAspect {
|
||||
Object arg = joinPoint.getArgs()[0];
|
||||
if ("saveNew".equals(methodName)) {
|
||||
Object data = joinPoint.proceed();
|
||||
BaseDictService<Object, Object> service =
|
||||
(BaseDictService<Object, Object>) joinPoint.getTarget();
|
||||
BaseDictService<Object, Serializable> service =
|
||||
(BaseDictService<Object, Serializable>) joinPoint.getTarget();
|
||||
// 这里参数必须使用saveNew方法的返回对象,因为里面包含实际主键值。
|
||||
service.putDictionaryCache(data);
|
||||
return data;
|
||||
} else if ("update".equals(methodName)) {
|
||||
Object data = joinPoint.proceed();
|
||||
BaseDictService<Object, Object> service =
|
||||
(BaseDictService<Object, Object>) joinPoint.getTarget();
|
||||
BaseDictService<Object, Serializable> service =
|
||||
(BaseDictService<Object, Serializable>) joinPoint.getTarget();
|
||||
// update的方法返回的是boolean,因此这里的参数需要使用第一个参数即可。
|
||||
service.putDictionaryCache(arg);
|
||||
return data;
|
||||
} else {
|
||||
// remove
|
||||
BaseDictService<Object, Object> service =
|
||||
(BaseDictService<Object, Object>) joinPoint.getTarget();
|
||||
service.removeDictionaryCache(arg);
|
||||
BaseDictService<Object, Serializable> service =
|
||||
(BaseDictService<Object, Serializable>) joinPoint.getTarget();
|
||||
service.removeDictionaryCache((Serializable) arg);
|
||||
return joinPoint.proceed();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
package com.orange.demo.common.core.base.dao;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
import tk.mybatis.mapper.additional.insert.InsertListMapper;
|
||||
import tk.mybatis.mapper.annotation.RegisterMapper;
|
||||
import tk.mybatis.mapper.common.Mapper;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -16,8 +14,7 @@ import java.util.Map;
|
||||
* @author Jerry
|
||||
* @date 2020-09-24
|
||||
*/
|
||||
@RegisterMapper
|
||||
public interface BaseDaoMapper<M> extends Mapper<M>, InsertListMapper<M> {
|
||||
public interface BaseDaoMapper<M> extends BaseMapper<M> {
|
||||
|
||||
/**
|
||||
* 根据指定的表名、显示字段列表、过滤条件字符串和分组字段,返回聚合计算后的查询结果。
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.orange.demo.common.core.base.service;
|
||||
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.orange.demo.common.core.constant.GlobalDeletedFlag;
|
||||
import com.orange.demo.common.core.exception.MyRuntimeException;
|
||||
import com.orange.demo.common.core.cache.DictionaryCache;
|
||||
@@ -8,8 +9,8 @@ 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;
|
||||
import tk.mybatis.mapper.entity.Example;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
@@ -22,7 +23,7 @@ import java.util.*;
|
||||
* @date 2020-09-24
|
||||
*/
|
||||
@Slf4j
|
||||
public abstract class BaseDictService<M, K> extends BaseService<M, K> implements IBaseDictService<M, K> {
|
||||
public abstract class BaseDictService<M, K extends Serializable> extends BaseService<M, K> implements IBaseDictService<M, K> {
|
||||
|
||||
/**
|
||||
* 缓存池对象。
|
||||
@@ -89,15 +90,7 @@ public abstract class BaseDictService<M, K> extends BaseService<M, K> implements
|
||||
if (tenantIdField != null) {
|
||||
ReflectUtil.setFieldValue(data, tenantIdField, TokenData.takeFromRequest().getTenantId());
|
||||
}
|
||||
if (deletedFlagFieldName != null) {
|
||||
try {
|
||||
setDeletedFlagMethod.invoke(data, GlobalDeletedFlag.NORMAL);
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to call reflection [setDeletedFlagMethod] in BaseDictService.update.", e);
|
||||
throw new MyRuntimeException(e);
|
||||
}
|
||||
}
|
||||
return mapper().updateByPrimaryKey(data) == 1;
|
||||
return mapper().updateById(data) == 1;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -109,7 +102,7 @@ public abstract class BaseDictService<M, K> extends BaseService<M, K> implements
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public boolean remove(K id) {
|
||||
return this.removeById(id);
|
||||
return mapper().deleteById(id) == 1;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -118,15 +111,16 @@ public abstract class BaseDictService<M, K> extends BaseService<M, K> implements
|
||||
* @param id 主键Id。
|
||||
* @return 主键关联的数据,不存在返回null。
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public M getById(K id) {
|
||||
M data = dictionaryCache.get(id);
|
||||
public M getById(Serializable id) {
|
||||
M data = dictionaryCache.get((K) id);
|
||||
if (data != null) {
|
||||
return data;
|
||||
}
|
||||
data = super.getById(id);
|
||||
if (data != null) {
|
||||
this.dictionaryCache.put(id, data);
|
||||
this.dictionaryCache.put((K) id, data);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
@@ -220,8 +214,10 @@ public abstract class BaseDictService<M, K> extends BaseService<M, K> implements
|
||||
List<M> dataList = this.getInList((Set<K>) inFilterValues);
|
||||
return dataList.size() == inFilterValues.size();
|
||||
}
|
||||
Example e = this.makeDefaultInListExample(inFilterField, inFilterValues, null);
|
||||
return mapper().selectCountByExample(e) == inFilterValues.size();
|
||||
String columnName = this.safeMapToColumnName(inFilterField);
|
||||
QueryWrapper<M> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.in(columnName, inFilterValues);
|
||||
return mapper().selectCount(queryWrapper) == inFilterValues.size();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,9 +1,15 @@
|
||||
package com.orange.demo.common.core.base.service;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.orange.demo.common.core.annotation.*;
|
||||
import com.orange.demo.common.core.base.dao.BaseDaoMapper;
|
||||
import com.orange.demo.common.core.constant.AggregationType;
|
||||
import com.orange.demo.common.core.constant.GlobalDeletedFlag;
|
||||
import com.orange.demo.common.core.exception.InvalidDataFieldException;
|
||||
import com.orange.demo.common.core.exception.MyRuntimeException;
|
||||
import com.orange.demo.common.core.object.*;
|
||||
import com.orange.demo.common.core.util.AopTargetUtil;
|
||||
@@ -14,13 +20,9 @@ import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.commons.collections4.MapUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import tk.mybatis.mapper.entity.Example;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.Transient;
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
@@ -41,7 +43,7 @@ import static java.util.stream.Collectors.*;
|
||||
* @date 2020-09-24
|
||||
*/
|
||||
@Slf4j
|
||||
public abstract class BaseService<M, K> implements IBaseService<M, K> {
|
||||
public abstract class BaseService<M, K extends Serializable> extends ServiceImpl<BaseDaoMapper<M>, M> implements IBaseService<M, K> {
|
||||
/**
|
||||
* 当前Service关联的主Model实体对象的Class。
|
||||
*/
|
||||
@@ -135,6 +137,11 @@ public abstract class BaseService<M, K> implements IBaseService<M, K> {
|
||||
private static final String AGGREGATED_VALUE = "aggregatedValue";
|
||||
private static final String AND_OP = " AND ";
|
||||
|
||||
@Override
|
||||
public BaseDaoMapper<M> getBaseMapper() {
|
||||
return mapper();
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造函数,在实例化的时候,一次性完成所有有关主Model对象信息的加载。
|
||||
*/
|
||||
@@ -142,7 +149,7 @@ public abstract class BaseService<M, K> implements IBaseService<M, K> {
|
||||
public BaseService() {
|
||||
modelClass = (Class<M>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
|
||||
idFieldClass = (Class<K>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[1];
|
||||
this.tableName = modelClass.getAnnotation(Table.class).name();
|
||||
this.tableName = modelClass.getAnnotation(TableName.class).value();
|
||||
Field[] fields = ReflectUtil.getFields(modelClass);
|
||||
for (Field field : fields) {
|
||||
initializeField(field);
|
||||
@@ -150,10 +157,10 @@ public abstract class BaseService<M, K> implements IBaseService<M, K> {
|
||||
}
|
||||
|
||||
private void initializeField(Field field) {
|
||||
if (idFieldName == null && null != field.getAnnotation(Id.class)) {
|
||||
if (idFieldName == null && null != field.getAnnotation(TableId.class)) {
|
||||
idFieldName = field.getName();
|
||||
Column c = field.getAnnotation(Column.class);
|
||||
idColumnName = c == null ? idFieldName : c.name();
|
||||
TableId c = field.getAnnotation(TableId.class);
|
||||
idColumnName = c == null ? idFieldName : c.value();
|
||||
setIdFieldMethod = ReflectUtil.getMethod(
|
||||
modelClass, "set" + StringUtils.capitalize(idFieldName), idFieldClass);
|
||||
getIdFieldMethod = ReflectUtil.getMethod(
|
||||
@@ -161,21 +168,18 @@ public abstract class BaseService<M, K> implements IBaseService<M, K> {
|
||||
}
|
||||
if (updateTimeFieldName == null && null != field.getAnnotation(JobUpdateTimeColumn.class)) {
|
||||
updateTimeFieldName = field.getName();
|
||||
Column c = field.getAnnotation(Column.class);
|
||||
updateTimeColumnName = c == null ? updateTimeFieldName : c.name();
|
||||
updateTimeColumnName = this.safeMapToColumnName(updateTimeFieldName);
|
||||
}
|
||||
if (deletedFlagFieldName == null && null != field.getAnnotation(DeletedFlagColumn.class)) {
|
||||
if (deletedFlagFieldName == null && null != field.getAnnotation(TableLogic.class)) {
|
||||
deletedFlagFieldName = field.getName();
|
||||
Column c = field.getAnnotation(Column.class);
|
||||
deletedFlagColumnName = c == null ? deletedFlagFieldName : c.name();
|
||||
deletedFlagColumnName = this.safeMapToColumnName(deletedFlagFieldName);
|
||||
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();
|
||||
tenantIdColumnName = this.safeMapToColumnName(tenantIdFieldName);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -186,31 +190,6 @@ public abstract class BaseService<M, K> implements IBaseService<M, K> {
|
||||
*/
|
||||
protected abstract BaseDaoMapper<M> mapper();
|
||||
|
||||
/**
|
||||
* 基于主键Id删除数据。如果包含逻辑删除字段,则进行逻辑删除。
|
||||
*
|
||||
* @param id 主键Id值。
|
||||
* @return true删除成功,false数据不存在。
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public boolean removeById(K id) {
|
||||
if (this.deletedFlagFieldName == null) {
|
||||
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);
|
||||
return mapper().updateByExampleSelective(data, e) == 1;
|
||||
} catch (Exception ex) {
|
||||
log.error("Failed to call reflection method in BaseService.removeById.", ex);
|
||||
throw new MyRuntimeException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据过滤条件删除数据。
|
||||
*
|
||||
@@ -220,25 +199,7 @@ public abstract class BaseService<M, K> implements IBaseService<M, K> {
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public Integer removeBy(M filter) {
|
||||
if (deletedFlagFieldName == null) {
|
||||
return mapper().delete(filter);
|
||||
}
|
||||
Example e = new Example(modelClass);
|
||||
Example.Criteria c = e.createCriteria();
|
||||
Field[] fields = ReflectUtil.getFields(modelClass);
|
||||
for (Field field : fields) {
|
||||
if (field.getAnnotation(Transient.class) == null) {
|
||||
this.assembleCriteriaByFilter(filter, field, c);
|
||||
}
|
||||
}
|
||||
try {
|
||||
M deletedObject = modelClass.newInstance();
|
||||
this.setDeletedFlagMethod.invoke(deletedObject, GlobalDeletedFlag.DELETED);
|
||||
return mapper().updateByExampleSelective(deletedObject, e);
|
||||
} catch (Exception ex) {
|
||||
log.error("Failed to call reflection method in BaseService.removeBy.", ex);
|
||||
throw new MyRuntimeException(ex);
|
||||
}
|
||||
return mapper().delete(new QueryWrapper<>(filter));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -255,9 +216,8 @@ public abstract class BaseService<M, K> implements IBaseService<M, K> {
|
||||
if (fieldName.equals(this.idFieldName)) {
|
||||
return this.existId((K) fieldValue);
|
||||
}
|
||||
Example e = new Example(modelClass);
|
||||
e.createCriteria().andEqualTo(fieldName, fieldValue);
|
||||
return mapper().selectCountByExample(e) == 1;
|
||||
String columnName = MyModelUtil.mapToColumnName(fieldName, modelClass);
|
||||
return mapper().selectCount(new QueryWrapper<M>().eq(columnName, fieldValue)) == 1;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -271,24 +231,6 @@ public abstract class BaseService<M, K> implements IBaseService<M, K> {
|
||||
return getById(id) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取主键Id关联的数据。
|
||||
*
|
||||
* @param id 主键Id。
|
||||
* @return 主键关联的数据,不存在返回null。
|
||||
*/
|
||||
@Override
|
||||
public M getById(K id) {
|
||||
if (deletedFlagFieldName == null) {
|
||||
return mapper().selectByPrimaryKey(id);
|
||||
}
|
||||
Example e = new Example(modelClass);
|
||||
e.createCriteria()
|
||||
.andEqualTo(this.idFieldName, id)
|
||||
.andEqualTo(deletedFlagFieldName, GlobalDeletedFlag.NORMAL);
|
||||
return mapper().selectOneByExample(e);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回符合 filterField = filterValue 条件的一条数据。
|
||||
*
|
||||
@@ -302,12 +244,9 @@ public abstract class BaseService<M, K> implements IBaseService<M, K> {
|
||||
if (filterField.equals(idFieldName)) {
|
||||
return this.getById((K) filterValue);
|
||||
}
|
||||
Example e = new Example(modelClass);
|
||||
Example.Criteria c = e.createCriteria().andEqualTo(filterField, filterValue);
|
||||
if (deletedFlagFieldName != null) {
|
||||
c.andEqualTo(deletedFlagFieldName, GlobalDeletedFlag.NORMAL);
|
||||
}
|
||||
return mapper().selectOneByExample(e);
|
||||
String columnName = this.safeMapToColumnName(filterField);
|
||||
QueryWrapper<M> queryWrapper = new QueryWrapper<M>().eq(columnName, filterValue);
|
||||
return mapper().selectOne(queryWrapper);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -331,12 +270,7 @@ public abstract class BaseService<M, K> implements IBaseService<M, K> {
|
||||
*/
|
||||
@Override
|
||||
public List<M> getAllList() {
|
||||
if (deletedFlagFieldName == null) {
|
||||
return mapper().selectAll();
|
||||
}
|
||||
Example e = new Example(modelClass);
|
||||
e.createCriteria().andEqualTo(deletedFlagFieldName, GlobalDeletedFlag.NORMAL);
|
||||
return mapper().selectByExample(e);
|
||||
return mapper().selectList(Wrappers.emptyWrapper());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -347,14 +281,11 @@ public abstract class BaseService<M, K> implements IBaseService<M, K> {
|
||||
*/
|
||||
@Override
|
||||
public List<M> getAllListByOrder(String... orderByProperties) {
|
||||
Example e = new Example(modelClass);
|
||||
for (String orderByProperty : orderByProperties) {
|
||||
e.orderBy(orderByProperty);
|
||||
String[] columns = new String[orderByProperties.length];
|
||||
for (int i = 0; i < orderByProperties.length; i++) {
|
||||
columns[i] = this.safeMapToColumnName(orderByProperties[i]);
|
||||
}
|
||||
if (deletedFlagFieldName != null) {
|
||||
e.and().andEqualTo(deletedFlagFieldName, GlobalDeletedFlag.NORMAL);
|
||||
}
|
||||
return mapper().selectByExample(e);
|
||||
return mapper().selectList(new QueryWrapper<M>().orderByAsc(columns));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -383,8 +314,8 @@ public abstract class BaseService<M, K> implements IBaseService<M, K> {
|
||||
if (CollectionUtils.isEmpty(inFilterValues)) {
|
||||
return true;
|
||||
}
|
||||
Example e = this.makeDefaultInListExample(inFilterField, inFilterValues, null);
|
||||
return mapper().selectCountByExample(e) == inFilterValues.size();
|
||||
String column = this.safeMapToColumnName(inFilterField);
|
||||
return mapper().selectCount(new QueryWrapper<M>().in(column, inFilterValues)) == inFilterValues.size();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -423,8 +354,58 @@ public abstract class BaseService<M, K> implements IBaseService<M, K> {
|
||||
if (CollectionUtils.isEmpty(inFilterValues)) {
|
||||
return new LinkedList<>();
|
||||
}
|
||||
Example e = this.makeDefaultInListExample(inFilterField, inFilterValues, orderBy);
|
||||
return mapper().selectByExample(e);
|
||||
String column = this.safeMapToColumnName(inFilterField);
|
||||
QueryWrapper<M> queryWrapper = new QueryWrapper<M>().in(column, inFilterValues);
|
||||
if (StringUtils.isNotBlank(orderBy)) {
|
||||
queryWrapper.last(orderBy);
|
||||
}
|
||||
return mapper().selectList(queryWrapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回符合主键 in (idValues) 条件的所有数据。同时返回关联数据。
|
||||
*
|
||||
* @param idValues 主键值集合。
|
||||
* @param relationParam 实体对象数据组装的参数构建器。
|
||||
* @return 检索后的数据列表。
|
||||
*/
|
||||
@Override
|
||||
public List<M> getInListWithRelation(Set<K> idValues, MyRelationParam relationParam) {
|
||||
List<M> resultList = this.getInList(idValues);
|
||||
this.buildRelationForDataList(resultList, relationParam);
|
||||
return resultList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回符合 inFilterField in (inFilterValues) 条件的所有数据。同时返回关联数据。
|
||||
*
|
||||
* @param inFilterField 参与(In-list)过滤的Java字段。
|
||||
* @param inFilterValues 参与(In-list)过滤的Java字段值集合。
|
||||
* @param relationParam 实体对象数据组装的参数构建器。
|
||||
* @return 检索后的数据列表。
|
||||
*/
|
||||
@Override
|
||||
public <T> List<M> getInListWithRelation(String inFilterField, Set<T> inFilterValues, MyRelationParam relationParam) {
|
||||
List<M> resultList = this.getInList(inFilterField, inFilterValues);
|
||||
this.buildRelationForDataList(resultList, relationParam);
|
||||
return resultList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回符合 inFilterField in (inFilterValues) 条件的所有数据,并根据orderBy字段排序。同时返回关联数据。
|
||||
*
|
||||
* @param inFilterField 参与(In-list)过滤的Java字段。
|
||||
* @param inFilterValues 参与(In-list)过滤的Java字段值集合。
|
||||
* @param orderBy 排序字段。
|
||||
* @param relationParam 实体对象数据组装的参数构建器。
|
||||
* @return 检索后的数据列表。
|
||||
*/
|
||||
@Override
|
||||
public <T> List<M> getInListWithRelation(
|
||||
String inFilterField, Set<T> inFilterValues, String orderBy, MyRelationParam relationParam) {
|
||||
List<M> resultList = this.getInList(inFilterField, inFilterValues, orderBy);
|
||||
this.buildRelationForDataList(resultList, relationParam);
|
||||
return resultList;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -435,16 +416,7 @@ public abstract class BaseService<M, K> implements IBaseService<M, K> {
|
||||
*/
|
||||
@Override
|
||||
public int getCountByFilter(M filter) {
|
||||
if (deletedFlagFieldName == null) {
|
||||
return mapper().selectCount(filter);
|
||||
}
|
||||
try {
|
||||
setDeletedFlagMethod.invoke(filter, GlobalDeletedFlag.NORMAL);
|
||||
return mapper().selectCount(filter);
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to call reflection [setDeletedFlagMethod] in BaseService.getCountByFilter.", e);
|
||||
throw new MyRuntimeException(e);
|
||||
}
|
||||
return mapper().selectCount(new QueryWrapper<>(filter));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -466,42 +438,7 @@ public abstract class BaseService<M, K> implements IBaseService<M, K> {
|
||||
*/
|
||||
@Override
|
||||
public List<M> getListByFilter(M filter) {
|
||||
if (filter == null) {
|
||||
return this.getAllList();
|
||||
}
|
||||
if (deletedFlagFieldName == null) {
|
||||
return mapper().select(filter);
|
||||
}
|
||||
try {
|
||||
setDeletedFlagMethod.invoke(filter, GlobalDeletedFlag.NORMAL);
|
||||
return mapper().select(filter);
|
||||
} catch (Exception ex) {
|
||||
log.error("Failed to call reflection code of BaseService.getListByFilter.", ex);
|
||||
throw new MyRuntimeException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private void assembleCriteriaByFilter(M filter, Field field, Example.Criteria c) {
|
||||
int modifiers = field.getModifiers();
|
||||
// transient类型的字段不能作为查询条件
|
||||
int transientMask = 128;
|
||||
if ((modifiers & transientMask) != 0 || Modifier.isStatic(modifiers)) {
|
||||
return;
|
||||
}
|
||||
if (field.getName().equals(deletedFlagFieldName)) {
|
||||
c.andEqualTo(deletedFlagFieldName, GlobalDeletedFlag.NORMAL);
|
||||
} else {
|
||||
ReflectUtil.setAccessible(field);
|
||||
try {
|
||||
Object o = field.get(filter);
|
||||
if (o != null) {
|
||||
c.andEqualTo(field.getName(), field.get(filter));
|
||||
}
|
||||
} catch (IllegalAccessException ex) {
|
||||
log.error("Failed to call reflection code of BaseService.getListByFilter.", ex);
|
||||
throw new MyRuntimeException(ex);
|
||||
}
|
||||
}
|
||||
return mapper().selectList(new QueryWrapper<>(filter));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -513,17 +450,14 @@ public abstract class BaseService<M, K> implements IBaseService<M, K> {
|
||||
*/
|
||||
@Override
|
||||
public List<M> getListByParentId(String parentIdFieldName, K parentId) {
|
||||
Example e = new Example(modelClass);
|
||||
Example.Criteria c = e.createCriteria();
|
||||
QueryWrapper<M> queryWrapper = new QueryWrapper<>();
|
||||
String parentIdColumn = this.safeMapToColumnName(parentIdFieldName);
|
||||
if (parentId != null) {
|
||||
c.andEqualTo(parentIdFieldName, parentId);
|
||||
queryWrapper.eq(parentIdColumn, parentId);
|
||||
} else {
|
||||
c.andIsNull(parentIdFieldName);
|
||||
queryWrapper.isNull(parentIdColumn);
|
||||
}
|
||||
if (deletedFlagFieldName != null) {
|
||||
c.andEqualTo(deletedFlagFieldName, GlobalDeletedFlag.NORMAL);
|
||||
}
|
||||
return mapper().selectByExample(e);
|
||||
return mapper().selectList(queryWrapper);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -553,32 +487,21 @@ public abstract class BaseService<M, K> implements IBaseService<M, K> {
|
||||
*/
|
||||
@Override
|
||||
public List<M> getListByCondition(List<String> selectList, M filter, String whereClause, String orderBy) {
|
||||
Example e = new Example(modelClass);
|
||||
Example.Criteria c = null;
|
||||
QueryWrapper<M> queryWrapper = new QueryWrapper<>(filter);
|
||||
if (CollectionUtils.isNotEmpty(selectList)) {
|
||||
String[] selectFields = new String[selectList.size()];
|
||||
selectList.toArray(selectFields);
|
||||
e.selectProperties(selectFields);
|
||||
}
|
||||
if (StringUtils.isNotBlank(orderBy)) {
|
||||
e.setOrderByClause(orderBy);
|
||||
}
|
||||
if (filter != null) {
|
||||
c = e.createCriteria();
|
||||
Field[] fields = ReflectUtil.getFields(modelClass);
|
||||
for (Field field : fields) {
|
||||
if (field.getAnnotation(Transient.class) == null) {
|
||||
this.assembleCriteriaByFilter(filter, field, c);
|
||||
}
|
||||
String[] columns = new String[selectList.size()];
|
||||
for (int i = 0; i < selectList.size(); i++) {
|
||||
columns[i] = this.safeMapToColumnName(selectList.get(i));
|
||||
}
|
||||
queryWrapper.select(columns);
|
||||
}
|
||||
if (StringUtils.isNotBlank(whereClause)) {
|
||||
if (c == null) {
|
||||
c = e.createCriteria();
|
||||
}
|
||||
c.andCondition(whereClause);
|
||||
queryWrapper.apply(whereClause);
|
||||
}
|
||||
return mapper().selectByExample(e);
|
||||
if (StringUtils.isNotBlank(orderBy)) {
|
||||
queryWrapper.last(" ORDER BY " + orderBy);
|
||||
}
|
||||
return mapper().selectList(queryWrapper);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -785,9 +708,10 @@ public abstract class BaseService<M, K> implements IBaseService<M, K> {
|
||||
continue;
|
||||
}
|
||||
Object masterIdValue = ReflectUtil.getFieldValue(dataObject, relationStruct.masterIdField);
|
||||
Example e = new Example(relationStruct.relationManyToMany.relationModelClass());
|
||||
e.createCriteria().andEqualTo(relationStruct.masterIdField.getName(), masterIdValue);
|
||||
List<?> manyToManyList = relationStruct.manyToManyMapper.selectByExample(e);
|
||||
String masterIdColumn = this.safeMapToColumnName(relationStruct.masterIdField.getName());
|
||||
Map<String, Object> filterMap = new HashMap<>(1);
|
||||
filterMap.put(masterIdColumn, masterIdValue);
|
||||
List<?> manyToManyList = relationStruct.manyToManyMapper.selectByMap(filterMap);
|
||||
ReflectUtil.setFieldValue(dataObject, relationStruct.relationField, manyToManyList);
|
||||
}
|
||||
}
|
||||
@@ -936,7 +860,7 @@ public abstract class BaseService<M, K> implements IBaseService<M, K> {
|
||||
.collect(toSet());
|
||||
// 从主表集合中,抽取主表关联字段的集合,再以in list形式去从表中查询。
|
||||
if (CollectionUtils.isNotEmpty(masterIdSet)) {
|
||||
BaseService<Object, Object> relationService = relationStruct.service;
|
||||
BaseService<Object, Serializable> relationService = relationStruct.service;
|
||||
List<Object> relationList =
|
||||
relationService.getInList(relationStruct.relationOneToOne.slaveIdField(), masterIdSet);
|
||||
MyModelUtil.makeOneToOneRelation(
|
||||
@@ -945,8 +869,8 @@ public abstract class BaseService<M, K> implements IBaseService<M, K> {
|
||||
if (withDict && relationStruct.relationOneToOne.loadSlaveDict()
|
||||
&& CollectionUtils.isNotEmpty(relationList)) {
|
||||
@SuppressWarnings("unchecked")
|
||||
BaseService<Object, Object> proxyTarget =
|
||||
(BaseService<Object, Object>) AopTargetUtil.getTarget(relationService);
|
||||
BaseService<Object, Serializable> proxyTarget =
|
||||
(BaseService<Object, Serializable>) AopTargetUtil.getTarget(relationService);
|
||||
// 关联本地字典。
|
||||
proxyTarget.buildDictForDataList(relationList, false, ignoreFields);
|
||||
// 关联常量字典
|
||||
@@ -973,14 +897,14 @@ public abstract class BaseService<M, K> implements IBaseService<M, K> {
|
||||
}
|
||||
Object id = ReflectUtil.getFieldValue(dataObject, relationStruct.masterIdField);
|
||||
if (id != null) {
|
||||
BaseService<Object, Object> relationService = relationStruct.service;
|
||||
BaseService<Object, Serializable> relationService = relationStruct.service;
|
||||
Object relationObject = relationService.getOne(relationStruct.relationOneToOne.slaveIdField(), id);
|
||||
ReflectUtil.setFieldValue(dataObject, relationStruct.relationField, relationObject);
|
||||
// 仅仅当需要加载从表字典关联时,才去加载。
|
||||
if (withDict && relationStruct.relationOneToOne.loadSlaveDict() && relationObject != null) {
|
||||
@SuppressWarnings("unchecked")
|
||||
BaseService<Object, Object> proxyTarget =
|
||||
(BaseService<Object, Object>) AopTargetUtil.getTarget(relationService);
|
||||
BaseService<Object, Serializable> proxyTarget =
|
||||
(BaseService<Object, Serializable>) AopTargetUtil.getTarget(relationService);
|
||||
// 关联本地字典
|
||||
proxyTarget.buildDictForData(relationObject, false, ignoreFields);
|
||||
// 关联常量字典
|
||||
@@ -1010,7 +934,7 @@ public abstract class BaseService<M, K> implements IBaseService<M, K> {
|
||||
.collect(toSet());
|
||||
// 从主表集合中,抽取主表关联字段的集合,再以in list形式去从表中查询。
|
||||
if (CollectionUtils.isNotEmpty(masterIdSet)) {
|
||||
BaseService<Object, Object> relationService = relationStruct.service;
|
||||
BaseService<Object, Serializable> relationService = relationStruct.service;
|
||||
List<Object> relationList =
|
||||
relationService.getInList(relationStruct.relationOneToMany.slaveIdField(), masterIdSet);
|
||||
MyModelUtil.makeOneToManyRelation(
|
||||
@@ -1035,7 +959,7 @@ public abstract class BaseService<M, K> implements IBaseService<M, K> {
|
||||
}
|
||||
Object id = ReflectUtil.getFieldValue(dataObject, relationStruct.masterIdField);
|
||||
if (id != null) {
|
||||
BaseService<Object, Object> relationService = relationStruct.service;
|
||||
BaseService<Object, Serializable> relationService = relationStruct.service;
|
||||
Set<Object> masterIdSet = new HashSet<>(1);
|
||||
masterIdSet.add(id);
|
||||
List<Object> relationObject = relationService.getInList(
|
||||
@@ -1338,32 +1262,97 @@ public abstract class BaseService<M, K> implements IBaseService<M, K> {
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过(In-list)条件和orderBy条件,构建Example对象,以供后续的查询操作使用。
|
||||
* 因为Mybatis Plus中QueryWrapper的条件方法都要求传入数据表字段名,因此提供该函数将
|
||||
* Java实体对象的字段名转换为数据表字段名,如果不存在会抛出异常。
|
||||
* 另外在MyModelUtil.mapToColumnName有一级缓存,对于查询过的对象字段都会放到缓存中,
|
||||
* 下次映射转换的时候,会直接从缓存获取。
|
||||
*
|
||||
* @param inFilterField 参与(In-list)过滤的Java字段。
|
||||
* @param inFilterValues 参与(In-list)过滤的Java字段值集合。
|
||||
* @param orderBy 排序字段。
|
||||
* @param <T> in 属性字段的类型。
|
||||
* @return 构建后的Example对象。
|
||||
* @param fieldName Java实体对象的字段名。
|
||||
* @return 对应的数据表字段名。
|
||||
*/
|
||||
protected <T> Example makeDefaultInListExample(String inFilterField, Collection<T> inFilterValues, String orderBy) {
|
||||
Set<T> inFilterValueSet;
|
||||
Example e = new Example(modelClass);
|
||||
if (StringUtils.isNotBlank(orderBy)) {
|
||||
e.setOrderByClause(orderBy);
|
||||
protected String safeMapToColumnName(String fieldName) {
|
||||
String columnName = MyModelUtil.mapToColumnName(fieldName, modelClass);
|
||||
if (columnName == null) {
|
||||
throw new InvalidDataFieldException(modelClass.getSimpleName(), fieldName);
|
||||
}
|
||||
if (inFilterValues instanceof Set) {
|
||||
inFilterValueSet = (Set<T>) inFilterValues;
|
||||
} else {
|
||||
inFilterValueSet = new HashSet<>(inFilterValues.size());
|
||||
inFilterValueSet.addAll(inFilterValues);
|
||||
return columnName;
|
||||
}
|
||||
|
||||
/**
|
||||
* 因为Mybatis Plus在update的时候,不能将实体对象中值为null的字段,更新为null,
|
||||
* 而且忽略更新,在全部更新场景下,这个是非常重要的,所以我们写了这个函数绕开这一问题。
|
||||
* 该函数会遍历实体对象中,所有不包含@Transient注解,没有transient修饰符的字段,如果
|
||||
* 当前对象的该字段值为null,则会调用UpdateWrapper的set方法,将该字段赋值为null。
|
||||
* 相比于其他重载方法,该方法会将参数中的主键id,设置到UpdateWrapper的过滤条件中。
|
||||
*
|
||||
* @param o 实体对象。
|
||||
* @param id 实体对象的主键值。
|
||||
* @return 创建后的UpdateWrapper。
|
||||
*/
|
||||
protected UpdateWrapper<M> createUpdateQueryForNullValue(M o, K id) {
|
||||
UpdateWrapper<M> uw = createUpdateQueryForNullValue(o, modelClass);
|
||||
try {
|
||||
M filter = modelClass.newInstance();
|
||||
this.setIdFieldMethod.invoke(filter, id);
|
||||
uw.setEntity(filter);
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to call reflection code of BaseService.createUpdateQueryForNullValue.", e);
|
||||
throw new MyRuntimeException(e);
|
||||
}
|
||||
Example.Criteria c = e.createCriteria();
|
||||
c.andIn(inFilterField, inFilterValueSet);
|
||||
if (deletedFlagFieldName != null) {
|
||||
c.andEqualTo(deletedFlagFieldName, GlobalDeletedFlag.NORMAL);
|
||||
return uw;
|
||||
}
|
||||
|
||||
/**
|
||||
* 因为Mybatis Plus在update的时候,不能将实体对象中值为null的字段,更新为null,
|
||||
* 而且忽略更新,在全部更新场景下,这个是非常重要的,所以我们写了这个函数绕开这一问题。
|
||||
* 该函数会遍历实体对象中,所有不包含@Transient注解,没有transient修饰符的字段,如果
|
||||
* 当前对象的该字段值为null,则会调用UpdateWrapper的set方法,将该字段赋值为null。
|
||||
*
|
||||
* @param o 实体对象。
|
||||
* @return 创建后的UpdateWrapper。
|
||||
*/
|
||||
protected UpdateWrapper<M> createUpdateQueryForNullValue(M o) {
|
||||
return createUpdateQueryForNullValue(o, modelClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* 因为Mybatis Plus在update的时候,不能将实体对象中值为null的字段,更新为null,
|
||||
* 而且忽略更新,在全部更新场景下,这个是非常重要的,所以我们写了这个函数绕开这一问题。
|
||||
* 该函数会遍历实体对象中,所有不包含@Transient注解,没有transient修饰符的字段,如果
|
||||
* 当前对象的该字段值为null,则会调用UpdateWrapper的set方法,将该字段赋值为null。
|
||||
*
|
||||
* @param o 实体对象。
|
||||
* @param clazz 实体对象的class。
|
||||
* @return 创建后的UpdateWrapper。
|
||||
*/
|
||||
public static <T> UpdateWrapper<T> createUpdateQueryForNullValue(T o, Class<T> clazz) {
|
||||
UpdateWrapper<T> uw = new UpdateWrapper<>();
|
||||
Field[] fields = ReflectUtil.getFields(clazz);
|
||||
List<String> nullColumnList = new LinkedList<>();
|
||||
for (Field field : fields) {
|
||||
TableField tableField = field.getAnnotation(TableField.class);
|
||||
if (tableField == null || tableField.exist()) {
|
||||
int modifiers = field.getModifiers();
|
||||
// transient类型的字段不能作为查询条件,静态字段和逻辑删除都不考虑。
|
||||
int transientMask = 128;
|
||||
if ((modifiers & transientMask) == 1
|
||||
|| Modifier.isStatic(modifiers)
|
||||
|| field.getAnnotation(TableLogic.class) != null) {
|
||||
continue;
|
||||
}
|
||||
// 仅当实体对象参数中,当前字段值为null的时候,才会赋值给UpdateWrapper。
|
||||
// 以便在后续的更新中,可以将这些null字段的值设置到数据库表对应的字段中。
|
||||
if (ReflectUtil.getFieldValue(o, field) == null) {
|
||||
nullColumnList.add(MyModelUtil.safeMapToColumnName(field.getName(), clazz));
|
||||
}
|
||||
}
|
||||
}
|
||||
return e;
|
||||
if (CollectionUtils.isNotEmpty(nullColumnList)) {
|
||||
for (String nullColumn : nullColumnList) {
|
||||
uw.set(nullColumn, null);
|
||||
}
|
||||
}
|
||||
return uw;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -1378,7 +1367,7 @@ public abstract class BaseService<M, K> implements IBaseService<M, K> {
|
||||
relationStruct.service = ApplicationContextHolder.getBean(
|
||||
StringUtils.uncapitalize(relationOneToOne.slaveServiceName()));
|
||||
} else {
|
||||
relationStruct.service = (BaseService<Object, Object>)
|
||||
relationStruct.service = (BaseService<Object, Serializable>)
|
||||
ApplicationContextHolder.getBean(relationOneToOne.slaveServiceClass());
|
||||
}
|
||||
relationOneToOneStructList.add(relationStruct);
|
||||
@@ -1394,7 +1383,7 @@ public abstract class BaseService<M, K> implements IBaseService<M, K> {
|
||||
relationStruct.service = ApplicationContextHolder.getBean(
|
||||
StringUtils.uncapitalize(relationOneToMany.slaveServiceName()));
|
||||
} else {
|
||||
relationStruct.service = (BaseService<Object, Object>)
|
||||
relationStruct.service = (BaseService<Object, Serializable>)
|
||||
ApplicationContextHolder.getBean(relationOneToMany.slaveServiceClass());
|
||||
}
|
||||
relationOneToManyStructList.add(relationStruct);
|
||||
@@ -1424,7 +1413,7 @@ public abstract class BaseService<M, K> implements IBaseService<M, K> {
|
||||
relationStruct.service = ApplicationContextHolder.getBean(
|
||||
StringUtils.uncapitalize(relationOneToManyAggregation.slaveServiceName()));
|
||||
} else {
|
||||
relationStruct.service = (BaseService<Object, Object>)
|
||||
relationStruct.service = (BaseService<Object, Serializable>)
|
||||
ApplicationContextHolder.getBean(relationOneToManyAggregation.slaveServiceClass());
|
||||
}
|
||||
relationOneToManyAggrStructList.add(relationStruct);
|
||||
@@ -1440,7 +1429,7 @@ public abstract class BaseService<M, K> implements IBaseService<M, K> {
|
||||
relationStruct.service = ApplicationContextHolder.getBean(
|
||||
StringUtils.uncapitalize(relationManyToManyAggregation.slaveServiceName()));
|
||||
} else {
|
||||
relationStruct.service = (BaseService<Object, Object>)
|
||||
relationStruct.service = (BaseService<Object, Serializable>)
|
||||
ApplicationContextHolder.getBean(relationManyToManyAggregation.slaveServiceClass());
|
||||
}
|
||||
relationManyToManyAggrStructList.add(relationStruct);
|
||||
@@ -1473,7 +1462,7 @@ public abstract class BaseService<M, K> implements IBaseService<M, K> {
|
||||
relationStruct.service = ApplicationContextHolder.getBean(
|
||||
StringUtils.uncapitalize(relationDict.slaveServiceName()));
|
||||
} else {
|
||||
relationStruct.service = (BaseService<Object, Object>)
|
||||
relationStruct.service = (BaseService<Object, Serializable>)
|
||||
ApplicationContextHolder.getBean(relationDict.slaveServiceClass());
|
||||
}
|
||||
relationDictStructList.add(relationStruct);
|
||||
@@ -1670,7 +1659,7 @@ public abstract class BaseService<M, K> implements IBaseService<M, K> {
|
||||
private Field relationField;
|
||||
private Field masterIdField;
|
||||
private Field equalOneToOneRelationField;
|
||||
private BaseService<Object, Object> service;
|
||||
private BaseService<Object, Serializable> service;
|
||||
private BaseDaoMapper<Object> manyToManyMapper;
|
||||
private Map<Object, String> dictMap;
|
||||
private RelationDict relationDict;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.orange.demo.common.core.base.service;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -10,7 +11,7 @@ import java.util.List;
|
||||
* @author Jerry
|
||||
* @date 2020-09-24
|
||||
*/
|
||||
public interface IBaseDictService<M, K> extends IBaseService<M, K> {
|
||||
public interface IBaseDictService<M, K extends Serializable> extends IBaseService<M, K> {
|
||||
|
||||
/**
|
||||
* 重新加载数据库中所有当前表数据到系统内存。
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package com.orange.demo.common.core.base.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.orange.demo.common.core.object.MyRelationParam;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.*;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
@@ -14,15 +16,7 @@ import java.util.function.Supplier;
|
||||
* @author Jerry
|
||||
* @date 2020-09-24
|
||||
*/
|
||||
public interface IBaseService<M, K> {
|
||||
|
||||
/**
|
||||
* 基于主键Id删除数据。如果包含逻辑删除字段,则进行逻辑删除。
|
||||
*
|
||||
* @param id 主键Id值。
|
||||
* @return true删除成功,false数据不存在。
|
||||
*/
|
||||
boolean removeById(K id);
|
||||
public interface IBaseService<M, K extends Serializable> extends IService<M>{
|
||||
|
||||
/**
|
||||
* 根据过滤条件删除数据。
|
||||
@@ -50,14 +44,6 @@ public interface IBaseService<M, K> {
|
||||
*/
|
||||
boolean existId(K id);
|
||||
|
||||
/**
|
||||
* 获取主键Id关联的数据。
|
||||
*
|
||||
* @param id 主键Id。
|
||||
* @return 主键关联的数据,不存在返回null。
|
||||
*/
|
||||
M getById(K id);
|
||||
|
||||
/**
|
||||
* 返回符合 filterField = filterValue 条件的一条数据。
|
||||
*
|
||||
@@ -135,6 +121,37 @@ public interface IBaseService<M, K> {
|
||||
*/
|
||||
<T> List<M> getInList(String inFilterField, Set<T> inFilterValues, String orderBy);
|
||||
|
||||
/**
|
||||
* 返回符合主键 in (idValues) 条件的所有数据。同时返回关联数据。
|
||||
*
|
||||
* @param idValues 主键值集合。
|
||||
* @param relationParam 实体对象数据组装的参数构建器。
|
||||
* @return 检索后的数据列表。
|
||||
*/
|
||||
List<M> getInListWithRelation(Set<K> idValues, MyRelationParam relationParam);
|
||||
|
||||
/**
|
||||
* 返回符合 inFilterField in (inFilterValues) 条件的所有数据。同时返回关联数据。
|
||||
*
|
||||
* @param inFilterField 参与(In-list)过滤的Java字段。
|
||||
* @param inFilterValues 参与(In-list)过滤的Java字段值集合。
|
||||
* @param relationParam 实体对象数据组装的参数构建器。
|
||||
* @return 检索后的数据列表。
|
||||
*/
|
||||
<T> List<M> getInListWithRelation(String inFilterField, Set<T> inFilterValues, MyRelationParam relationParam);
|
||||
|
||||
/**
|
||||
* 返回符合 inFilterField in (inFilterValues) 条件的所有数据,并根据orderBy字段排序。同时返回关联数据。
|
||||
*
|
||||
* @param inFilterField 参与(In-list)过滤的Java字段。
|
||||
* @param inFilterValues 参与(In-list)过滤的Java字段值集合。
|
||||
* @param orderBy 排序字段。
|
||||
* @param relationParam 实体对象数据组装的参数构建器。
|
||||
* @return 检索后的数据列表。
|
||||
*/
|
||||
<T> List<M> getInListWithRelation(
|
||||
String inFilterField, Set<T> inFilterValues, String orderBy, MyRelationParam relationParam);
|
||||
|
||||
/**
|
||||
* 用参数对象作为过滤条件,获取数据数量。
|
||||
*
|
||||
|
||||
@@ -5,10 +5,13 @@ import cn.hutool.core.util.ReflectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.crypto.digest.DigestUtil;
|
||||
import com.orange.demo.common.core.constant.AppDeviceType;
|
||||
import com.orange.demo.common.core.validator.AddGroup;
|
||||
import com.orange.demo.common.core.validator.UpdateGroup;
|
||||
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Validation;
|
||||
import javax.validation.Validator;
|
||||
import javax.validation.groups.Default;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -109,11 +112,75 @@ public class MyCommonUtil {
|
||||
* @return 没有错误返回null,否则返回具体的错误信息。
|
||||
*/
|
||||
public static <T> String getModelValidationError(T model, Class<?>...groups) {
|
||||
Set<ConstraintViolation<T>> constraintViolations = VALIDATOR.validate(model, groups);
|
||||
if (!constraintViolations.isEmpty()) {
|
||||
Iterator<ConstraintViolation<T>> it = constraintViolations.iterator();
|
||||
ConstraintViolation<T> constraint = it.next();
|
||||
return constraint.getMessage();
|
||||
if (model != null) {
|
||||
Set<ConstraintViolation<T>> constraintViolations = VALIDATOR.validate(model, groups);
|
||||
if (!constraintViolations.isEmpty()) {
|
||||
Iterator<ConstraintViolation<T>> it = constraintViolations.iterator();
|
||||
ConstraintViolation<T> constraint = it.next();
|
||||
return constraint.getMessage();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断模型对象是否通过校验,没有通过返回具体的校验错误信息。
|
||||
*
|
||||
* @param model 带校验的model。
|
||||
* @param forUpdate 是否为更新。
|
||||
* @return 没有错误返回null,否则返回具体的错误信息。
|
||||
*/
|
||||
public static <T> String getModelValidationError(T model, boolean forUpdate) {
|
||||
if (model != null) {
|
||||
Set<ConstraintViolation<T>> constraintViolations;
|
||||
if (forUpdate) {
|
||||
constraintViolations = VALIDATOR.validate(model, Default.class, UpdateGroup.class);
|
||||
} else {
|
||||
constraintViolations = VALIDATOR.validate(model, Default.class, AddGroup.class);
|
||||
}
|
||||
if (!constraintViolations.isEmpty()) {
|
||||
Iterator<ConstraintViolation<T>> it = constraintViolations.iterator();
|
||||
ConstraintViolation<T> constraint = it.next();
|
||||
return constraint.getMessage();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断模型对象是否通过校验,没有通过返回具体的校验错误信息。
|
||||
*
|
||||
* @param modelList 带校验的model列表。
|
||||
* @param groups Validate绑定的校验组。
|
||||
* @return 没有错误返回null,否则返回具体的错误信息。
|
||||
*/
|
||||
public static <T> String getModelValidationError(List<T> modelList, Class<?>... groups) {
|
||||
if (CollUtil.isNotEmpty(modelList)) {
|
||||
for (T model : modelList) {
|
||||
String errorMessage = getModelValidationError(model, groups);
|
||||
if (StrUtil.isNotBlank(errorMessage)) {
|
||||
return errorMessage;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断模型对象是否通过校验,没有通过返回具体的校验错误信息。
|
||||
*
|
||||
* @param modelList 带校验的model列表。
|
||||
* @param forUpdate 是否为更新。
|
||||
* @return 没有错误返回null,否则返回具体的错误信息。
|
||||
*/
|
||||
public static <T> String getModelValidationError(List<T> modelList, boolean forUpdate) {
|
||||
if (CollUtil.isNotEmpty(modelList)) {
|
||||
for (T model : modelList) {
|
||||
String errorMessage = getModelValidationError(model, forUpdate);
|
||||
if (StrUtil.isNotBlank(errorMessage)) {
|
||||
return errorMessage;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -2,22 +2,19 @@ package com.orange.demo.common.core.util;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
import com.orange.demo.common.core.exception.InvalidDataFieldException;
|
||||
import com.orange.demo.common.core.annotation.*;
|
||||
import com.orange.demo.common.core.exception.MyRuntimeException;
|
||||
import com.orange.demo.common.core.object.TokenData;
|
||||
import com.orange.demo.common.core.object.Tuple2;
|
||||
import com.orange.demo.common.core.upload.UploadStoreInfo;
|
||||
import com.google.common.base.CaseFormat;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.commons.collections4.MapUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import tk.mybatis.mapper.entity.Example;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.Transient;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
@@ -96,6 +93,9 @@ public class MyModelUtil {
|
||||
* @return copy后的目标类型对象集合。
|
||||
*/
|
||||
public static <S, T> List<T> copyCollectionTo(Collection<S> sourceCollection, Class<T> targetClazz) {
|
||||
if (sourceCollection == null) {
|
||||
return null;
|
||||
}
|
||||
List<T> targetList = new LinkedList<>();
|
||||
if (CollectionUtils.isNotEmpty(sourceCollection)) {
|
||||
for (S source : sourceCollection) {
|
||||
@@ -207,8 +207,20 @@ public class MyModelUtil {
|
||||
if (field == null) {
|
||||
return null;
|
||||
}
|
||||
Column c = field.getAnnotation(Column.class);
|
||||
String columnName = c == null ? fieldName : c.name();
|
||||
TableField c = field.getAnnotation(TableField.class);
|
||||
String columnName = null;
|
||||
if (c == null) {
|
||||
TableId id = field.getAnnotation(TableId.class);
|
||||
if (id != null) {
|
||||
columnName = id.value();
|
||||
}
|
||||
}
|
||||
if (columnName == null) {
|
||||
columnName = c == null ? CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, fieldName) : c.value();
|
||||
if (StringUtils.isBlank(columnName)) {
|
||||
columnName = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, fieldName);
|
||||
}
|
||||
}
|
||||
// 这里缺省情况下都是按照整型去处理,因为他覆盖太多的类型了。
|
||||
// 如Integer/Long/Double/BigDecimal,可根据实际情况完善和扩充。
|
||||
String typeName = field.getType().getSimpleName();
|
||||
@@ -231,8 +243,8 @@ public class MyModelUtil {
|
||||
* @return Model对象对应的数据表名称。
|
||||
*/
|
||||
public static String mapToTableName(Class<?> modelClazz) {
|
||||
Table t = modelClazz.getAnnotation(Table.class);
|
||||
return t == null ? null : t.name();
|
||||
TableName t = modelClazz.getAnnotation(TableName.class);
|
||||
return t == null ? null : t.value();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -592,43 +604,6 @@ public class MyModelUtil {
|
||||
return isMap ? BeanUtil.beanToMap(model) : model;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换过滤对象到与其等效的Example对象。
|
||||
*
|
||||
* @param filterModel 过滤对象。
|
||||
* @param modelClass 过滤对象的Class对象。
|
||||
* @param <T> 过滤对象类型。
|
||||
* @return 转换后的Example对象。
|
||||
*/
|
||||
public static <T> Example convertFilterModelToExample(T filterModel, Class<T> modelClass) {
|
||||
if (filterModel == null) {
|
||||
return null;
|
||||
}
|
||||
Example e = new Example(modelClass);
|
||||
Example.Criteria c = e.createCriteria();
|
||||
Field[] fields = ReflectUtil.getFields(modelClass);
|
||||
for (Field field : fields) {
|
||||
if (field.getAnnotation(Transient.class) == null) {
|
||||
int modifiers = field.getModifiers();
|
||||
// transient类型的字段不能作为查询条件
|
||||
if ((modifiers & 128) != 0 || Modifier.isStatic(modifiers)) {
|
||||
continue;
|
||||
}
|
||||
ReflectUtil.setAccessible(field);
|
||||
try {
|
||||
Object o = field.get(filterModel);
|
||||
if (o != null) {
|
||||
c.andEqualTo(field.getName(), field.get(filterModel));
|
||||
}
|
||||
} catch (IllegalAccessException ex) {
|
||||
log.error("Failed to call reflection code.", ex);
|
||||
throw new MyRuntimeException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
return e;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取上传字段的存储信息。
|
||||
*
|
||||
|
||||
@@ -9,31 +9,21 @@
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>common-swagger</artifactId>
|
||||
<artifactId>common-datafilter</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<name>common-swagger</name>
|
||||
<name>common-datafilter</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.github.xiaoymin</groupId>
|
||||
<artifactId>knife4j-spring-boot-starter</artifactId>
|
||||
<version>${knife4j.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.springframework.plugin</groupId>
|
||||
<artifactId>spring-plugin-core</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>org.springframework.plugin</groupId>
|
||||
<artifactId>spring-plugin-metadata</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.orange.demo</groupId>
|
||||
<artifactId>common-core</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.orange.demo</groupId>
|
||||
<artifactId>common-redis</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.orange.demo.common.datafilter.aop;
|
||||
|
||||
import com.orange.demo.common.core.object.GlobalThreadLocal;
|
||||
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;
|
||||
|
||||
/**
|
||||
* 禁用Mybatis拦截器数据过滤的AOP处理类。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-09-24
|
||||
*/
|
||||
@Aspect
|
||||
@Component
|
||||
@Order(1)
|
||||
@Slf4j
|
||||
public class DisableDataFilterAspect {
|
||||
|
||||
/**
|
||||
* 所有标记了DisableDataFilter注解的方法。
|
||||
*/
|
||||
@Pointcut("@annotation(com.orange.demo.common.core.annotation.DisableDataFilter)")
|
||||
public void disableDataFilterPointCut() {
|
||||
// 空注释,避免sonar警告
|
||||
}
|
||||
|
||||
@Around("disableDataFilterPointCut()")
|
||||
public Object around(ProceedingJoinPoint point) throws Throwable {
|
||||
boolean dataFilterEnabled = GlobalThreadLocal.setDataFilter(false);
|
||||
try {
|
||||
return point.proceed();
|
||||
} finally {
|
||||
GlobalThreadLocal.setDataFilter(dataFilterEnabled);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.orange.demo.common.datafilter.config;
|
||||
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
|
||||
/**
|
||||
* common-datafilter模块的自动配置引导类。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-09-24
|
||||
*/
|
||||
@EnableConfigurationProperties({DataFilterProperties.class})
|
||||
public class DataFilterAutoConfig {
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.orange.demo.common.datafilter.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* common-datafilter模块的配置类。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-09-24
|
||||
*/
|
||||
@Data
|
||||
@ConfigurationProperties(prefix = "datafilter")
|
||||
public class DataFilterProperties {
|
||||
|
||||
/**
|
||||
* 是否启用租户过滤。
|
||||
*/
|
||||
@Value("${datafilter.tenant.enabled:false}")
|
||||
private Boolean enabledTenantFilter;
|
||||
|
||||
/**
|
||||
* 是否启动数据权限过滤。
|
||||
*/
|
||||
@Value("${datafilter.dataperm.enabled:false}")
|
||||
private Boolean enabledDataPermFilter;
|
||||
|
||||
/**
|
||||
* 部门关联表的表名前缀,如zz_。该值主要用在MybatisDataFilterInterceptor拦截器中,
|
||||
* 用于拼接数据权限过滤的SQL语句。
|
||||
*/
|
||||
@Value("${datafilter.dataperm.deptRelationTablePrefix:}")
|
||||
private String deptRelationTablePrefix;
|
||||
|
||||
/**
|
||||
* 该值为true的时候,在进行数据权限过滤时,会加上表名,如:zz_sys_user.dept_id = xxx。
|
||||
* 为false时,过滤条件不加表名,只是使用字段名,如:dept_id = xxx。该值目前主要适用于
|
||||
* Oracle分页SQL使用了子查询的场景。此场景下,由于子查询使用了别名,再在数据权限过滤条件中
|
||||
* 加上原有表名时,SQL语法会报错。
|
||||
*/
|
||||
@Value("${datafilter.dataperm.addTableNamePrefix:true}")
|
||||
private Boolean addTableNamePrefix;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.orange.demo.common.datafilter.config;
|
||||
|
||||
import com.orange.demo.common.datafilter.interceptor.DataFilterInterceptor;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
/**
|
||||
* 添加数据过滤相关的拦截器。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-09-24
|
||||
*/
|
||||
@Configuration
|
||||
public class DataFilterWebMvcConfigurer implements WebMvcConfigurer {
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(new DataFilterInterceptor()).addPathPatterns("/**");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.orange.demo.common.datafilter.constant;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 数据权限规则类型常量类。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-09-24
|
||||
*/
|
||||
public final class DataPermRuleType {
|
||||
|
||||
/**
|
||||
* 查看全部。
|
||||
*/
|
||||
public static final int TYPE_ALL = 0;
|
||||
|
||||
/**
|
||||
* 仅查看当前用户
|
||||
*/
|
||||
public static final int TYPE_USER_ONLY = 1;
|
||||
|
||||
/**
|
||||
* 仅查看当前部门
|
||||
*/
|
||||
public static final int TYPE_DEPT_ONLY = 2;
|
||||
|
||||
/**
|
||||
* 所在部门及子部门
|
||||
*/
|
||||
public static final int TYPE_DEPT_AND_CHILD_DEPT = 3;
|
||||
|
||||
/**
|
||||
* 多部门及子部门
|
||||
*/
|
||||
public static final int TYPE_MULTI_DEPT_AND_CHILD_DEPT = 4;
|
||||
|
||||
/**
|
||||
* 自定义部门列表
|
||||
*/
|
||||
public static final int TYPE_CUSTOM_DEPT_LIST = 5;
|
||||
|
||||
private static final Map<Object, String> DICT_MAP = new HashMap<>(6);
|
||||
static {
|
||||
DICT_MAP.put(0, "查看全部");
|
||||
DICT_MAP.put(1, "仅查看当前用户");
|
||||
DICT_MAP.put(2, "仅查看所在部门");
|
||||
DICT_MAP.put(3, "所在部门及子部门");
|
||||
DICT_MAP.put(4, "多部门及子部门");
|
||||
DICT_MAP.put(5, "自定义部门列表");
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断参数是否为当前常量字典的合法取值范围。
|
||||
*
|
||||
* @param value 待验证的参数值。
|
||||
* @return 合法返回true,否则false。
|
||||
*/
|
||||
public static boolean isValid(Integer value) {
|
||||
return value != null && DICT_MAP.containsKey(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 私有构造函数,明确标识该常量类的作用。
|
||||
*/
|
||||
private DataPermRuleType() {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.orange.demo.common.datafilter.interceptor;
|
||||
|
||||
import com.orange.demo.common.core.object.GlobalThreadLocal;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* 主要用于初始化,通过Mybatis拦截器插件进行数据过滤的标记。
|
||||
* 在调用controller接口处理方法之前,必须强制将数据过滤标记设置为缺省值。
|
||||
* 这样可以避免使用当前线程在处理上一个请求时,未能正常清理的数据过滤标记值。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-09-24
|
||||
*/
|
||||
@Slf4j
|
||||
public class DataFilterInterceptor implements HandlerInterceptor {
|
||||
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
|
||||
throws Exception {
|
||||
// 每次进入Controller接口之前,均主动打开数据权限验证。
|
||||
// 可以避免该Servlet线程在处理之前的请求时异常退出,从而导致该状态数据没有被正常清除。
|
||||
GlobalThreadLocal.setDataFilter(true);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
|
||||
ModelAndView modelAndView) throws Exception {
|
||||
// 这里需要加注释,否则sonar不happy。
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
|
||||
throws Exception {
|
||||
GlobalThreadLocal.clearDataFilter();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,469 @@
|
||||
package com.orange.demo.common.datafilter.interceptor;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.orange.demo.common.core.base.dao.BaseDaoMapper;
|
||||
import com.orange.demo.common.core.annotation.*;
|
||||
import com.orange.demo.common.core.exception.NoDataPermException;
|
||||
import com.orange.demo.common.core.object.GlobalThreadLocal;
|
||||
import com.orange.demo.common.core.object.TokenData;
|
||||
import com.orange.demo.common.core.util.ApplicationContextHolder;
|
||||
import com.orange.demo.common.core.util.ContextUtil;
|
||||
import com.orange.demo.common.core.util.MyModelUtil;
|
||||
import com.orange.demo.common.core.util.RedisKeyUtil;
|
||||
import com.orange.demo.common.datafilter.config.DataFilterProperties;
|
||||
import com.orange.demo.common.datafilter.constant.DataPermRuleType;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.sf.jsqlparser.JSQLParserException;
|
||||
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
|
||||
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
|
||||
import net.sf.jsqlparser.statement.Statement;
|
||||
import net.sf.jsqlparser.statement.delete.Delete;
|
||||
import net.sf.jsqlparser.statement.select.FromItem;
|
||||
import net.sf.jsqlparser.statement.select.PlainSelect;
|
||||
import net.sf.jsqlparser.statement.select.Select;
|
||||
import net.sf.jsqlparser.statement.select.SubSelect;
|
||||
import net.sf.jsqlparser.statement.update.Update;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.commons.collections4.MapUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.ibatis.executor.statement.RoutingStatementHandler;
|
||||
import org.apache.ibatis.executor.statement.StatementHandler;
|
||||
import org.apache.ibatis.mapping.BoundSql;
|
||||
import org.apache.ibatis.mapping.MappedStatement;
|
||||
import org.apache.ibatis.mapping.SqlCommandType;
|
||||
import org.apache.ibatis.plugin.*;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.sql.Connection;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Mybatis拦截器。目前用于数据权限的统一拦截和注入处理。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-09-24
|
||||
*/
|
||||
@Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})})
|
||||
@Slf4j
|
||||
@Component
|
||||
public class MybatisDataFilterInterceptor implements Interceptor {
|
||||
|
||||
@Autowired
|
||||
private RedissonClient redissonClient;
|
||||
@Autowired
|
||||
private DataFilterProperties properties;
|
||||
|
||||
/**
|
||||
* 对象缓存。由于Set是排序后的,因此在查找排除方法名称时效率更高。
|
||||
* 在应用服务启动的监听器中(LoadDataPermMapperListener),会调用当前对象的(loadMappersWithDataPerm)方法,加载缓存。
|
||||
*/
|
||||
private final Map<String, ModelDataPermInfo> cachedDataPermMap = new HashMap<>();
|
||||
/**
|
||||
* 租户租户对象缓存。
|
||||
*/
|
||||
private final Map<String, ModelTenantInfo> cachedTenantMap = new HashMap<>();
|
||||
|
||||
/**
|
||||
* 预先加载与数据过滤相关的数据到缓存,该函数会在(LoadDataFilterInfoListener)监听器中调用。
|
||||
*/
|
||||
@SuppressWarnings("all")
|
||||
public void loadInfoWithDataFilter() {
|
||||
Map<String, BaseDaoMapper> mapperMap =
|
||||
ApplicationContextHolder.getApplicationContext().getBeansOfType(BaseDaoMapper.class);
|
||||
for (BaseDaoMapper<?> mapperProxy : mapperMap.values()) {
|
||||
// 优先处理jdk的代理
|
||||
Object proxy = ReflectUtil.getFieldValue(mapperProxy, "h");
|
||||
// 如果不是jdk的代理,再看看cjlib的代理。
|
||||
if (proxy == null) {
|
||||
proxy = ReflectUtil.getFieldValue(mapperProxy, "CGLIB$CALLBACK_0");
|
||||
}
|
||||
Class<?> mapperClass = (Class<?>) ReflectUtil.getFieldValue(proxy, "mapperInterface");
|
||||
if (properties.getEnabledTenantFilter()) {
|
||||
loadTenantFilterData(mapperClass);
|
||||
}
|
||||
if (properties.getEnabledDataPermFilter()) {
|
||||
EnableDataPerm rule = mapperClass.getAnnotation(EnableDataPerm.class);
|
||||
if (rule != null) {
|
||||
loadDataPermFilterRules(mapperClass, rule);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void loadTenantFilterData(Class<?> mapperClass) {
|
||||
Class<?> modelClass = (Class<?>) ((ParameterizedType)
|
||||
mapperClass.getGenericInterfaces()[0]).getActualTypeArguments()[0];
|
||||
Field[] fields = ReflectUtil.getFields(modelClass);
|
||||
for (Field field : fields) {
|
||||
if (field.getAnnotation(TenantFilterColumn.class) != null) {
|
||||
ModelTenantInfo tenantInfo = new ModelTenantInfo();
|
||||
tenantInfo.setModelName(modelClass.getSimpleName());
|
||||
tenantInfo.setTableName(modelClass.getAnnotation(TableName.class).value());
|
||||
tenantInfo.setFieldName(field.getName());
|
||||
tenantInfo.setColumnName(MyModelUtil.mapToColumnName(field, modelClass));
|
||||
// 判断当前dao中是否包括不需要自动注入租户Id过滤的方法。
|
||||
DisableTenantFilter disableTenantFilter = mapperClass.getAnnotation(DisableTenantFilter.class);
|
||||
if (disableTenantFilter != null) {
|
||||
// 这里开始获取当前Mapper已经声明的的SqlId中,有哪些是需要排除在外的。
|
||||
// 排除在外的将不进行数据过滤。
|
||||
Set<String> excludeMethodNameSet = new HashSet<>();
|
||||
for (String excludeName : disableTenantFilter.includeMethodName()) {
|
||||
excludeMethodNameSet.add(excludeName);
|
||||
// 这里是给pagehelper中,分页查询先获取数据总量的查询。
|
||||
excludeMethodNameSet.add(excludeName + "_COUNT");
|
||||
}
|
||||
tenantInfo.setExcludeMethodNameSet(excludeMethodNameSet);
|
||||
}
|
||||
cachedTenantMap.put(mapperClass.getName(), tenantInfo);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void loadDataPermFilterRules(Class<?> mapperClass, EnableDataPerm rule) {
|
||||
String sysDataPermMapperName = "SysDataPermMapper";
|
||||
// 由于给数据权限Mapper添加@EnableDataPerm,将会导致无限递归,因此这里检测到之后,
|
||||
// 会在系统启动加载监听器的时候,及时抛出异常。
|
||||
if (StringUtils.equals(sysDataPermMapperName, mapperClass.getSimpleName())) {
|
||||
throw new IllegalStateException("Add @EnableDataPerm annotation to SysDataPermMapper is ILLEGAL!");
|
||||
}
|
||||
// 这里开始获取当前Mapper已经声明的的SqlId中,有哪些是需要排除在外的。
|
||||
// 排除在外的将不进行数据过滤。
|
||||
Set<String> excludeMethodNameSet = null;
|
||||
String[] excludes = rule.excluseMethodName();
|
||||
if (excludes.length > 0) {
|
||||
excludeMethodNameSet = new HashSet<>();
|
||||
for (String excludeName : excludes) {
|
||||
excludeMethodNameSet.add(excludeName);
|
||||
// 这里是给pagehelper中,分页查询先获取数据总量的查询。
|
||||
excludeMethodNameSet.add(excludeName + "_COUNT");
|
||||
}
|
||||
}
|
||||
// 获取Mapper关联的主表信息,包括表名,user过滤字段名和dept过滤字段名。
|
||||
Class<?> modelClazz = (Class<?>)
|
||||
((ParameterizedType) mapperClass.getGenericInterfaces()[0]).getActualTypeArguments()[0];
|
||||
Field[] fields = ReflectUtil.getFields(modelClazz);
|
||||
Field userFilterField = null;
|
||||
Field deptFilterField = null;
|
||||
for (Field field : fields) {
|
||||
if (null != field.getAnnotation(UserFilterColumn.class)) {
|
||||
userFilterField = field;
|
||||
}
|
||||
if (null != field.getAnnotation(DeptFilterColumn.class)) {
|
||||
deptFilterField = field;
|
||||
}
|
||||
if (userFilterField != null && deptFilterField != null) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
// 通过注解解析与Mapper关联的Model,并获取与数据权限关联的信息,并将结果缓存。
|
||||
ModelDataPermInfo info = new ModelDataPermInfo();
|
||||
info.setMainTableName(MyModelUtil.mapToTableName(modelClazz));
|
||||
info.setExcludeMethodNameSet(excludeMethodNameSet);
|
||||
if (userFilterField != null) {
|
||||
info.setUserFilterColumn(MyModelUtil.mapToColumnName(userFilterField, modelClazz));
|
||||
}
|
||||
if (deptFilterField != null) {
|
||||
info.setDeptFilterColumn(MyModelUtil.mapToColumnName(deptFilterField, modelClazz));
|
||||
}
|
||||
cachedDataPermMap.put(mapperClass.getName(), info);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object intercept(Invocation invocation) throws Throwable {
|
||||
// 判断当前线程本地存储中,业务操作是否禁用了数据权限过滤,如果禁用,则不进行后续的数据过滤处理了。
|
||||
if (!GlobalThreadLocal.enabledDataFilter()) {
|
||||
return invocation.proceed();
|
||||
}
|
||||
// 只有在HttpServletRequest场景下,该拦截器才起作用,对于系统级别的预加载数据不会应用数据权限。
|
||||
if (!ContextUtil.hasRequestContext()) {
|
||||
return invocation.proceed();
|
||||
}
|
||||
// 没有登录的用户,不会参与租户过滤,如果需要过滤的,自己在代码中手动实现
|
||||
// 通常对于无需登录的白名单url,也无需过滤了。
|
||||
// 另外就是登录接口中,获取菜单列表的接口,由于尚未登录,没有TokenData,所以这个接口我们手动加入了该条件。
|
||||
if (TokenData.takeFromRequest() == null) {
|
||||
return invocation.proceed();
|
||||
}
|
||||
RoutingStatementHandler handler = (RoutingStatementHandler) invocation.getTarget();
|
||||
StatementHandler delegate =
|
||||
(StatementHandler) ReflectUtil.getFieldValue(handler, "delegate");
|
||||
// 通过反射获取delegate父类BaseStatementHandler的mappedStatement属性
|
||||
MappedStatement mappedStatement =
|
||||
(MappedStatement) ReflectUtil.getFieldValue(delegate, "mappedStatement");
|
||||
SqlCommandType commandType = mappedStatement.getSqlCommandType();
|
||||
// 对于INSERT语句,我们不进行任何数据过滤。
|
||||
if (commandType == SqlCommandType.INSERT) {
|
||||
return invocation.proceed();
|
||||
}
|
||||
String sqlId = mappedStatement.getId();
|
||||
int pos = StringUtils.lastIndexOf(sqlId, ".");
|
||||
String className = StringUtils.substring(sqlId, 0, pos);
|
||||
String methodName = StringUtils.substring(sqlId, pos + 1);
|
||||
// 先进行租户过滤条件的处理,再将解析并处理后的SQL Statement交给下一步的数据权限过滤去处理。
|
||||
// 这样做的目的主要是为了减少一次SQL解析的过程,因为这是高频操作,所以要尽量去优化。
|
||||
Statement statement = null;
|
||||
if (properties.getEnabledTenantFilter()) {
|
||||
statement = this.processTenantFilter(className, methodName, delegate.getBoundSql(), commandType);
|
||||
}
|
||||
// 处理数据权限过滤。
|
||||
if (properties.getEnabledDataPermFilter()) {
|
||||
this.processDataPermFilter(className, methodName, delegate.getBoundSql(), commandType, statement, sqlId);
|
||||
}
|
||||
return invocation.proceed();
|
||||
}
|
||||
|
||||
private Statement processTenantFilter(
|
||||
String className, String methodName, BoundSql boundSql, SqlCommandType commandType) throws JSQLParserException {
|
||||
ModelTenantInfo info = cachedTenantMap.get(className);
|
||||
if (info == null || CollUtil.contains(info.getExcludeMethodNameSet(), methodName)) {
|
||||
return null;
|
||||
}
|
||||
String sql = boundSql.getSql();
|
||||
Statement statement = CCJSqlParserUtil.parse(sql);
|
||||
StringBuilder filterBuilder = new StringBuilder(64);
|
||||
filterBuilder.append(info.tableName).append(".")
|
||||
.append(info.columnName)
|
||||
.append("=")
|
||||
.append(TokenData.takeFromRequest().getTenantId());
|
||||
String dataFilter = filterBuilder.toString();
|
||||
if (commandType == SqlCommandType.UPDATE) {
|
||||
Update update = (Update) statement;
|
||||
this.buildWhereClause(update, dataFilter);
|
||||
} else if (commandType == SqlCommandType.DELETE) {
|
||||
Delete delete = (Delete) statement;
|
||||
this.buildWhereClause(delete, dataFilter);
|
||||
} else {
|
||||
Select select = (Select) statement;
|
||||
PlainSelect selectBody = (PlainSelect) select.getSelectBody();
|
||||
FromItem fromItem = selectBody.getFromItem();
|
||||
if (fromItem != null) {
|
||||
PlainSelect subSelect = null;
|
||||
if (fromItem instanceof SubSelect) {
|
||||
subSelect = (PlainSelect) ((SubSelect) fromItem).getSelectBody();
|
||||
}
|
||||
if (subSelect != null) {
|
||||
buildWhereClause(subSelect, dataFilter);
|
||||
} else {
|
||||
buildWhereClause(selectBody, dataFilter);
|
||||
}
|
||||
}
|
||||
}
|
||||
ReflectUtil.setFieldValue(boundSql, "sql", statement.toString());
|
||||
return statement;
|
||||
}
|
||||
|
||||
private void processDataPermFilter(
|
||||
String className, String methodName, BoundSql boundSql, SqlCommandType commandType, Statement statement, String sqlId)
|
||||
throws JSQLParserException {
|
||||
// 判断当前线程本地存储中,业务操作是否禁用了数据权限过滤,如果禁用,则不进行后续的数据过滤处理了。
|
||||
// 数据过滤权限中,INSERT不过滤。如果是管理员则不参与数据权限的数据过滤,显示全部数据。
|
||||
TokenData tokenData = TokenData.takeFromRequest();
|
||||
if (Boolean.TRUE.equals(tokenData.getIsAdmin())) {
|
||||
return;
|
||||
}
|
||||
ModelDataPermInfo info = cachedDataPermMap.get(className);
|
||||
// 再次查找当前方法是否为排除方法,如果不是,就参与数据权限注入过滤。
|
||||
if (info == null || CollUtil.contains(info.getExcludeMethodNameSet(), methodName)) {
|
||||
return;
|
||||
}
|
||||
String dataPermSessionKey = RedisKeyUtil.makeSessionDataPermIdKey(tokenData.getSessionId());
|
||||
String dataPermData = redissonClient.getBucket(dataPermSessionKey).get().toString();
|
||||
if (StringUtils.isBlank(dataPermData)) {
|
||||
throw new NoDataPermException("No Related DataPerm found for SQL_ID [ " + sqlId + " ].");
|
||||
}
|
||||
Map<Integer, String> dataPermMap = new HashMap<>(8);
|
||||
for (Map.Entry<String, Object> entry : JSON.parseObject(dataPermData).entrySet()) {
|
||||
dataPermMap.put(Integer.valueOf(entry.getKey()), entry.getValue().toString());
|
||||
}
|
||||
if (MapUtils.isEmpty(dataPermMap)) {
|
||||
throw new NoDataPermException("No Related DataPerm found for SQL_ID [ " + sqlId + " ].");
|
||||
}
|
||||
if (dataPermMap.containsKey(DataPermRuleType.TYPE_ALL)) {
|
||||
return;
|
||||
}
|
||||
this.processDataPerm(info, dataPermMap, boundSql, commandType, statement);
|
||||
}
|
||||
|
||||
private void processDataPerm(
|
||||
ModelDataPermInfo info,
|
||||
Map<Integer, String> dataPermMap,
|
||||
BoundSql boundSql,
|
||||
SqlCommandType commandType,
|
||||
Statement statement) throws JSQLParserException {
|
||||
List<String> criteriaList = new LinkedList<>();
|
||||
for (Map.Entry<Integer, String> entry : dataPermMap.entrySet()) {
|
||||
String filterClause = processDataPermRule(info, entry.getKey(), entry.getValue());
|
||||
if (StringUtils.isNotBlank(filterClause)) {
|
||||
criteriaList.add(filterClause);
|
||||
}
|
||||
}
|
||||
if (CollectionUtils.isEmpty(criteriaList)) {
|
||||
return;
|
||||
}
|
||||
StringBuilder filterBuilder = new StringBuilder(128);
|
||||
filterBuilder.append("(");
|
||||
filterBuilder.append(StringUtils.join(criteriaList, " OR "));
|
||||
filterBuilder.append(")");
|
||||
String dataFilter = filterBuilder.toString();
|
||||
if (statement == null) {
|
||||
String sql = boundSql.getSql();
|
||||
statement = CCJSqlParserUtil.parse(sql);
|
||||
}
|
||||
if (commandType == SqlCommandType.UPDATE) {
|
||||
Update update = (Update) statement;
|
||||
this.buildWhereClause(update, dataFilter);
|
||||
} else if (commandType == SqlCommandType.DELETE) {
|
||||
Delete delete = (Delete) statement;
|
||||
this.buildWhereClause(delete, dataFilter);
|
||||
} else {
|
||||
Select select = (Select) statement;
|
||||
PlainSelect selectBody = (PlainSelect) select.getSelectBody();
|
||||
FromItem fromItem = selectBody.getFromItem();
|
||||
PlainSelect subSelect = null;
|
||||
if (fromItem != null) {
|
||||
if (fromItem instanceof SubSelect) {
|
||||
subSelect = (PlainSelect) ((SubSelect) fromItem).getSelectBody();
|
||||
}
|
||||
if (subSelect != null) {
|
||||
buildWhereClause(subSelect, dataFilter);
|
||||
} else {
|
||||
buildWhereClause(selectBody, dataFilter);
|
||||
}
|
||||
}
|
||||
}
|
||||
ReflectUtil.setFieldValue(boundSql, "sql", statement.toString());
|
||||
}
|
||||
|
||||
private String processDataPermRule(ModelDataPermInfo info, Integer ruleType, String deptIds) {
|
||||
TokenData tokenData = TokenData.takeFromRequest();
|
||||
StringBuilder filter = new StringBuilder(128);
|
||||
if (ruleType == DataPermRuleType.TYPE_USER_ONLY) {
|
||||
if (StringUtils.isNotBlank(info.getUserFilterColumn())) {
|
||||
if (properties.getAddTableNamePrefix()) {
|
||||
filter.append(info.getMainTableName()).append(".");
|
||||
}
|
||||
filter.append(info.getUserFilterColumn())
|
||||
.append(" = ")
|
||||
.append(tokenData.getUserId());
|
||||
}
|
||||
} else {
|
||||
if (StringUtils.isNotBlank(info.getDeptFilterColumn())) {
|
||||
if (ruleType == DataPermRuleType.TYPE_DEPT_ONLY) {
|
||||
if (properties.getAddTableNamePrefix()) {
|
||||
filter.append(info.getMainTableName()).append(".");
|
||||
}
|
||||
filter.append(info.getDeptFilterColumn())
|
||||
.append(" = ")
|
||||
.append(tokenData.getDeptId());
|
||||
} else if (ruleType == DataPermRuleType.TYPE_DEPT_AND_CHILD_DEPT) {
|
||||
filter.append(" EXISTS ")
|
||||
.append("(SELECT 1 FROM ")
|
||||
.append(properties.getDeptRelationTablePrefix())
|
||||
.append("sys_dept_relation WHERE ")
|
||||
.append(properties.getDeptRelationTablePrefix())
|
||||
.append("sys_dept_relation.parent_dept_id = ")
|
||||
.append(tokenData.getDeptId())
|
||||
.append(" AND ");
|
||||
if (properties.getAddTableNamePrefix()) {
|
||||
filter.append(info.getMainTableName()).append(".");
|
||||
}
|
||||
filter.append(info.getDeptFilterColumn())
|
||||
.append(" = ")
|
||||
.append(properties.getDeptRelationTablePrefix())
|
||||
.append("sys_dept_relation.dept_id) ");
|
||||
} else if (ruleType == DataPermRuleType.TYPE_MULTI_DEPT_AND_CHILD_DEPT) {
|
||||
filter.append(" EXISTS ")
|
||||
.append("(SELECT 1 FROM ")
|
||||
.append(properties.getDeptRelationTablePrefix())
|
||||
.append("sys_dept_relation WHERE ")
|
||||
.append(properties.getDeptRelationTablePrefix())
|
||||
.append("sys_dept_relation.parent_dept_id IN (")
|
||||
.append(deptIds)
|
||||
.append(") AND ");
|
||||
if (properties.getAddTableNamePrefix()) {
|
||||
filter.append(info.getMainTableName()).append(".");
|
||||
}
|
||||
filter.append(info.getDeptFilterColumn())
|
||||
.append(" = ")
|
||||
.append(properties.getDeptRelationTablePrefix())
|
||||
.append("sys_dept_relation.dept_id) ");
|
||||
} else if (ruleType == DataPermRuleType.TYPE_CUSTOM_DEPT_LIST) {
|
||||
if (properties.getAddTableNamePrefix()) {
|
||||
filter.append(info.getMainTableName()).append(".");
|
||||
}
|
||||
filter.append(info.getDeptFilterColumn())
|
||||
.append(" IN (")
|
||||
.append(deptIds)
|
||||
.append(") ");
|
||||
}
|
||||
}
|
||||
}
|
||||
return filter.toString();
|
||||
}
|
||||
|
||||
private void buildWhereClause(Update update, String dataFilter) throws JSQLParserException {
|
||||
if (update.getWhere() == null) {
|
||||
update.setWhere(CCJSqlParserUtil.parseCondExpression(dataFilter));
|
||||
} else {
|
||||
AndExpression and = new AndExpression(
|
||||
CCJSqlParserUtil.parseCondExpression(dataFilter), update.getWhere());
|
||||
update.setWhere(and);
|
||||
}
|
||||
}
|
||||
|
||||
private void buildWhereClause(Delete delete, String dataFilter) throws JSQLParserException {
|
||||
if (delete.getWhere() == null) {
|
||||
delete.setWhere(CCJSqlParserUtil.parseCondExpression(dataFilter));
|
||||
} else {
|
||||
AndExpression and = new AndExpression(
|
||||
CCJSqlParserUtil.parseCondExpression(dataFilter), delete.getWhere());
|
||||
delete.setWhere(and);
|
||||
}
|
||||
}
|
||||
|
||||
private void buildWhereClause(PlainSelect select, String dataFilter) throws JSQLParserException {
|
||||
if (select.getWhere() == null) {
|
||||
select.setWhere(CCJSqlParserUtil.parseCondExpression(dataFilter));
|
||||
} else {
|
||||
AndExpression and = new AndExpression(
|
||||
CCJSqlParserUtil.parseCondExpression(dataFilter), select.getWhere());
|
||||
select.setWhere(and);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object plugin(Object target) {
|
||||
return Plugin.wrap(target, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setProperties(Properties properties) {
|
||||
// 这里需要空注解,否则sonar会不happy。
|
||||
}
|
||||
|
||||
@Data
|
||||
private static final class ModelDataPermInfo {
|
||||
private Set<String> excludeMethodNameSet;
|
||||
private String userFilterColumn;
|
||||
private String deptFilterColumn;
|
||||
private String mainTableName;
|
||||
}
|
||||
|
||||
@Data
|
||||
private static final class ModelTenantInfo {
|
||||
private Set<String> excludeMethodNameSet;
|
||||
private String modelName;
|
||||
private String tableName;
|
||||
private String fieldName;
|
||||
private String columnName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.orange.demo.common.datafilter.listener;
|
||||
|
||||
import com.orange.demo.common.datafilter.interceptor.MybatisDataFilterInterceptor;
|
||||
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 应用服务启动监听器。
|
||||
* 目前主要功能是调用MybatisDataFilterInterceptor中的loadInfoWithDataFilter方法,
|
||||
* 将标记有过滤注解的数据加载到缓存,以提升系统运行时效率。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-09-24
|
||||
*/
|
||||
@Component
|
||||
public class LoadDataFilterInfoListener implements ApplicationListener<ApplicationReadyEvent> {
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
|
||||
MybatisDataFilterInterceptor interceptor =
|
||||
applicationReadyEvent.getApplicationContext().getBean(MybatisDataFilterInterceptor.class);
|
||||
interceptor.loadInfoWithDataFilter();
|
||||
}
|
||||
}
|
||||
@@ -1,2 +1,2 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.orange.demo.common.swagger.config.SwaggerAutoConfiguration
|
||||
com.orange.demo.common.datafilter.config.DataFilterAutoConfig
|
||||
@@ -0,0 +1,43 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>common</artifactId>
|
||||
<groupId>com.orange.demo</groupId>
|
||||
<version>1.0.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>common-log</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<name>common-log</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.orange.demo</groupId>
|
||||
<artifactId>common-sequence</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<includes>
|
||||
<include>**/*.*</include>
|
||||
</includes>
|
||||
<filtering>false</filtering>
|
||||
</resource>
|
||||
<resource>
|
||||
<directory>src/main/java</directory>
|
||||
<includes>
|
||||
<include>**/*.xml</include>
|
||||
</includes>
|
||||
<filtering>false</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.orange.demo.common.log.annotation;
|
||||
|
||||
import com.orange.demo.common.log.model.constant.SysOperationLogType;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 操作日志记录注解。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-09-24
|
||||
*/
|
||||
@Target({ElementType.METHOD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface OperationLog {
|
||||
|
||||
/**
|
||||
* 描述。
|
||||
*/
|
||||
String description() default "";
|
||||
|
||||
/**
|
||||
* 操作类型。
|
||||
*/
|
||||
int type() default SysOperationLogType.OTHER;
|
||||
|
||||
/**
|
||||
* 是否保存应答结果。
|
||||
* 对于类似导出和文件下载之类的接口,该参与应该设置为false。
|
||||
*/
|
||||
boolean saveResponse() default true;
|
||||
}
|
||||
@@ -0,0 +1,251 @@
|
||||
package com.orange.demo.common.log.aop;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.orange.demo.common.core.constant.ApplicationConstant;
|
||||
import com.orange.demo.common.core.object.ResponseResult;
|
||||
import com.orange.demo.common.core.object.TokenData;
|
||||
import com.orange.demo.common.core.util.ContextUtil;
|
||||
import com.orange.demo.common.core.util.IpUtil;
|
||||
import com.orange.demo.common.core.util.MyCommonUtil;
|
||||
import com.orange.demo.common.log.annotation.OperationLog;
|
||||
import com.orange.demo.common.log.config.OperationLogProperties;
|
||||
import com.orange.demo.common.log.model.SysOperationLog;
|
||||
import com.orange.demo.common.log.model.constant.SysOperationLogType;
|
||||
import com.orange.demo.common.log.service.SysOperationLogService;
|
||||
import com.orange.demo.common.sequence.wrapper.IdGeneratorWrapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.Signature;
|
||||
import org.aspectj.lang.annotation.*;
|
||||
import org.aspectj.lang.reflect.MethodSignature;
|
||||
import org.slf4j.MDC;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 操作日志记录处理AOP对象。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-09-24
|
||||
*/
|
||||
@Aspect
|
||||
@Component
|
||||
@Order(1)
|
||||
@Slf4j
|
||||
public class OperationLogAspect {
|
||||
|
||||
@Value("${spring.application.name}")
|
||||
private String serviceName;
|
||||
@Autowired
|
||||
private SysOperationLogService operationLogService;
|
||||
@Autowired
|
||||
private OperationLogProperties properties;
|
||||
@Autowired
|
||||
private IdGeneratorWrapper idGenerator;
|
||||
|
||||
/**
|
||||
* 错误信息、请求参数和应答结果字符串的最大长度。
|
||||
*/
|
||||
private final static int MAX_LENGTH = 2000;
|
||||
|
||||
/**
|
||||
* 所有controller方法。
|
||||
*/
|
||||
@Pointcut("execution(public * com.orange.demo..controller..*(..))")
|
||||
public void operationLogPointCut() {
|
||||
// 空注释,避免sonar警告
|
||||
}
|
||||
|
||||
@Around("operationLogPointCut()")
|
||||
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||
// 计时。
|
||||
long start = System.currentTimeMillis();
|
||||
HttpServletRequest request = ContextUtil.getHttpRequest();
|
||||
HttpServletResponse response = ContextUtil.getHttpResponse();
|
||||
String traceId = this.getTraceId(request);
|
||||
request.setAttribute(ApplicationConstant.HTTP_HEADER_TRACE_ID, traceId);
|
||||
// 将流水号通过应答头返回给前端,便于问题精确定位。
|
||||
response.setHeader(ApplicationConstant.HTTP_HEADER_TRACE_ID, traceId);
|
||||
MDC.put(ApplicationConstant.HTTP_HEADER_TRACE_ID, traceId);
|
||||
TokenData tokenData = TokenData.takeFromRequest();
|
||||
// 为log4j2日志设定变量,使日志可以输出更多有价值的信息。
|
||||
if (tokenData != null) {
|
||||
MDC.put("sessionId", tokenData.getSessionId());
|
||||
MDC.put("userId", tokenData.getUserId().toString());
|
||||
}
|
||||
String[] parameterNames = this.getParameterNames(joinPoint);
|
||||
Object[] args = joinPoint.getArgs();
|
||||
JSONObject jsonArgs = new JSONObject();
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
Object arg = args[i];
|
||||
if (this.isNormalArgs(arg)) {
|
||||
String parameterName = parameterNames[i];
|
||||
jsonArgs.put(parameterName, arg);
|
||||
}
|
||||
}
|
||||
String params = jsonArgs.toJSONString();
|
||||
SysOperationLog operationLog = null;
|
||||
OperationLog operationLogAnnotation = null;
|
||||
boolean saveOperationLog = properties.isEnabled();
|
||||
if (saveOperationLog) {
|
||||
operationLogAnnotation = getOperationLogAnnotation(joinPoint);
|
||||
saveOperationLog = (operationLogAnnotation != null);
|
||||
}
|
||||
if (saveOperationLog) {
|
||||
operationLog = this.buildSysOperationLog(operationLogAnnotation, joinPoint, params, traceId, tokenData);
|
||||
}
|
||||
Object result;
|
||||
log.info("开始请求,url={}, reqData={}", request.getRequestURI(), params);
|
||||
try {
|
||||
// 调用原来的方法
|
||||
result = joinPoint.proceed();
|
||||
String respData = result == null ? "null" : JSON.toJSONString(result);
|
||||
Long elapse = System.currentTimeMillis() - start;
|
||||
if (saveOperationLog) {
|
||||
this.operationLogPostProcess(operationLogAnnotation, respData, operationLog, result);
|
||||
}
|
||||
log.info("请求完成, url={},elapse={}ms, respData={}", request.getRequestURI(), elapse, respData);
|
||||
} catch (Exception e) {
|
||||
if (saveOperationLog) {
|
||||
operationLog.setSuccess(false);
|
||||
operationLog.setErrorMsg(StringUtils.substring(e.getMessage(), 0, MAX_LENGTH));
|
||||
}
|
||||
log.error("请求报错,url={}, reqData={}, error={}", request.getRequestURI(), params, e.getMessage());
|
||||
throw e;
|
||||
} finally {
|
||||
if (saveOperationLog) {
|
||||
operationLog.setElapse(System.currentTimeMillis() - start);
|
||||
operationLogService.saveNewAsync(operationLog);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private SysOperationLog buildSysOperationLog(
|
||||
OperationLog operationLogAnnotation,
|
||||
ProceedingJoinPoint joinPoint,
|
||||
String params,
|
||||
String traceId,
|
||||
TokenData tokenData) {
|
||||
HttpServletRequest request = ContextUtil.getHttpRequest();
|
||||
SysOperationLog operationLog = new SysOperationLog();
|
||||
operationLog.setLogId(idGenerator.nextLongId());
|
||||
operationLog.setTraceId(traceId);
|
||||
operationLog.setDescription(operationLogAnnotation.description());
|
||||
operationLog.setOperationType(operationLogAnnotation.type());
|
||||
operationLog.setServiceName(this.serviceName);
|
||||
operationLog.setApiClass(joinPoint.getTarget().getClass().getName());
|
||||
operationLog.setApiMethod(operationLog.getApiClass() + "." + joinPoint.getSignature().getName());
|
||||
operationLog.setRequestMethod(request.getMethod());
|
||||
operationLog.setRequestUrl(request.getRequestURI());
|
||||
if (tokenData != null) {
|
||||
operationLog.setRequestIp(tokenData.getLoginIp());
|
||||
} else {
|
||||
operationLog.setRequestIp(IpUtil.getRemoteIpAddress(request));
|
||||
}
|
||||
operationLog.setOperationTime(new Date());
|
||||
if (params != null) {
|
||||
if (params.length() <= MAX_LENGTH) {
|
||||
operationLog.setRequestArguments(params);
|
||||
} else {
|
||||
operationLog.setRequestArguments(StringUtils.substring(params, 0, MAX_LENGTH));
|
||||
}
|
||||
}
|
||||
if (tokenData != null) {
|
||||
// 对于非多租户系统,该值为空可以忽略。
|
||||
operationLog.setTenantId(tokenData.getTenantId());
|
||||
operationLog.setSessionId(tokenData.getSessionId());
|
||||
operationLog.setOperatorId(tokenData.getUserId());
|
||||
operationLog.setOperatorName(tokenData.getLoginName());
|
||||
}
|
||||
return operationLog;
|
||||
}
|
||||
|
||||
private void operationLogPostProcess(
|
||||
OperationLog operationLogAnnotation, String respData, SysOperationLog operationLog, Object result) {
|
||||
if (operationLogAnnotation.saveResponse()) {
|
||||
if (respData.length() <= MAX_LENGTH) {
|
||||
operationLog.setResponseResult(respData);
|
||||
} else {
|
||||
operationLog.setResponseResult(StringUtils.substring(respData, 0, MAX_LENGTH));
|
||||
}
|
||||
}
|
||||
// 处理大部分返回ResponseResult的接口。
|
||||
if (!(result instanceof ResponseResult)) {
|
||||
if (ContextUtil.hasRequestContext()) {
|
||||
operationLog.setSuccess(ContextUtil.getHttpResponse().getStatus() == HttpServletResponse.SC_OK);
|
||||
}
|
||||
return;
|
||||
}
|
||||
ResponseResult<?> responseResult = (ResponseResult<?>) result;
|
||||
operationLog.setSuccess(responseResult.isSuccess());
|
||||
if (!responseResult.isSuccess()) {
|
||||
operationLog.setErrorMsg(responseResult.getErrorMessage());
|
||||
}
|
||||
if (operationLog.getOperationType().equals(SysOperationLogType.LOGIN)) {
|
||||
// 对于登录操作,由于在调用登录方法之前,没有可用的TokenData。
|
||||
// 因此如果登录成功,可再次通过TokenData.takeFromRequest()获取TokenData。
|
||||
if (operationLog.getSuccess()) {
|
||||
// 这里为了保证LoginController.doLogin方法,一定将TokenData存入Request.Attribute之中,
|
||||
// 我们将不做空值判断,一旦出错,开发者可在调试时立刻发现异常,并根据这里的注释进行修复。
|
||||
TokenData tokenData = TokenData.takeFromRequest();
|
||||
// 对于非多租户系统,为了保证代码一致性,仍可保留对tenantId的赋值代码。
|
||||
operationLog.setTenantId(tokenData.getTenantId());
|
||||
operationLog.setSessionId(tokenData.getSessionId());
|
||||
operationLog.setOperatorId(tokenData.getUserId());
|
||||
operationLog.setOperatorName(tokenData.getLoginName());
|
||||
} else {
|
||||
HttpServletRequest request = ContextUtil.getHttpRequest();
|
||||
// 登录操作需要特殊处理,无论是登录成功还是失败,都要记录operator_name字段。
|
||||
operationLog.setOperatorName(request.getParameter("loginName"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String[] getParameterNames(ProceedingJoinPoint joinPoint) {
|
||||
Signature signature = joinPoint.getSignature();
|
||||
MethodSignature methodSignature = (MethodSignature) signature;
|
||||
return methodSignature.getParameterNames();
|
||||
}
|
||||
|
||||
private OperationLog getOperationLogAnnotation(JoinPoint joinPoint) throws Exception {
|
||||
Signature signature = joinPoint.getSignature();
|
||||
MethodSignature methodSignature = (MethodSignature) signature;
|
||||
Method method = methodSignature.getMethod();
|
||||
return method.getAnnotation(OperationLog.class);
|
||||
}
|
||||
|
||||
private String getTraceId(HttpServletRequest request) {
|
||||
// 获取请求流水号。
|
||||
// 对于微服务系统,为了保证traceId在全调用链的唯一性,因此在网关的过滤器中创建了该值。
|
||||
String traceId = request.getHeader(ApplicationConstant.HTTP_HEADER_TRACE_ID);
|
||||
if (StringUtils.isBlank(traceId)) {
|
||||
traceId = MyCommonUtil.generateUuid();
|
||||
}
|
||||
return traceId;
|
||||
}
|
||||
|
||||
private boolean isNormalArgs(Object o) {
|
||||
if (o instanceof List) {
|
||||
List<?> list = (List<?>) o;
|
||||
if (CollUtil.isNotEmpty(list)) {
|
||||
return !(list.get(0) instanceof MultipartFile);
|
||||
}
|
||||
}
|
||||
return !(o instanceof HttpServletRequest)
|
||||
&& !(o instanceof HttpServletResponse)
|
||||
&& !(o instanceof MultipartFile);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.orange.demo.common.log.config;
|
||||
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
|
||||
/**
|
||||
* common-log模块的自动配置引导类。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-09-24
|
||||
*/
|
||||
@EnableConfigurationProperties({OperationLogProperties.class})
|
||||
public class CommonLogAutoConfig {
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.orange.demo.common.log.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* 操作日志的配置类。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-09-24
|
||||
*/
|
||||
@Data
|
||||
@ConfigurationProperties(prefix = "common-log.operation-log")
|
||||
public class OperationLogProperties {
|
||||
|
||||
/**
|
||||
* 是否采集操作日志。
|
||||
*/
|
||||
private boolean enabled = true;
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.orange.demo.common.log.dao;
|
||||
|
||||
import com.orange.demo.common.core.base.dao.BaseDaoMapper;
|
||||
import com.orange.demo.common.log.model.SysOperationLog;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 系统操作日志对应的数据访问对象。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-09-24
|
||||
*/
|
||||
public interface SysOperationLogMapper extends BaseDaoMapper<SysOperationLog> {
|
||||
|
||||
/**
|
||||
* 批量插入。
|
||||
*
|
||||
* @param operationLogList 操作日志列表。
|
||||
*/
|
||||
void insertList(List<SysOperationLog> operationLogList);
|
||||
|
||||
/**
|
||||
* 根据过滤条件和排序规则,查询操作日志。
|
||||
*
|
||||
* @param sysOperationLogFilter 操作日志的过滤对象。
|
||||
* @param orderBy 排序规则。
|
||||
* @return 查询列表。
|
||||
*/
|
||||
List<SysOperationLog> getSysOperationLogList(
|
||||
@Param("sysOperationLogFilter") SysOperationLog sysOperationLogFilter,
|
||||
@Param("orderBy") String orderBy);
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.orange.demo.common.log.dao.SysOperationLogMapper">
|
||||
<resultMap id="BaseResultMap" type="com.orange.demo.common.log.model.SysOperationLog">
|
||||
<id column="log_id" jdbcType="BIGINT" property="logId"/>
|
||||
<result column="description" jdbcType="VARCHAR" property="description"/>
|
||||
<result column="operation_type" jdbcType="INTEGER" property="operationType"/>
|
||||
<result column="service_name" jdbcType="VARCHAR" property="serviceName"/>
|
||||
<result column="api_class" jdbcType="VARCHAR" property="apiClass"/>
|
||||
<result column="api_method" jdbcType="VARCHAR" property="apiMethod"/>
|
||||
<result column="session_id" jdbcType="VARCHAR" property="sessionId"/>
|
||||
<result column="trace_id" jdbcType="VARCHAR" property="traceId"/>
|
||||
<result column="elapse" jdbcType="BIGINT" property="elapse"/>
|
||||
<result column="request_method" jdbcType="VARCHAR" property="requestMethod"/>
|
||||
<result column="request_url" jdbcType="VARCHAR" property="requestUrl"/>
|
||||
<result column="request_arguments" jdbcType="VARCHAR" property="requestArguments"/>
|
||||
<result column="response_result" jdbcType="VARCHAR" property="responseResult"/>
|
||||
<result column="request_ip" jdbcType="VARCHAR" property="requestIp"/>
|
||||
<result column="success" jdbcType="BIT" property="success"/>
|
||||
<result column="error_msg" jdbcType="VARCHAR" property="errorMsg"/>
|
||||
<result column="tenant_id" jdbcType="BIGINT" property="tenantId"/>
|
||||
<result column="operator_id" jdbcType="BIGINT" property="operatorId"/>
|
||||
<result column="operator_name" jdbcType="VARCHAR" property="operatorName"/>
|
||||
<result column="operation_time" jdbcType="TIMESTAMP" property="operationTime"/>
|
||||
</resultMap>
|
||||
|
||||
<!-- 这里仅包含调用接口输入的主表过滤条件 -->
|
||||
<sql id="filterRef">
|
||||
<if test="sysOperationLogFilter != null">
|
||||
<if test="sysOperationLogFilter.operationType != null">
|
||||
AND zz_sys_operation_log.operation_type = #{sysOperationLogFilter.operationType}
|
||||
</if>
|
||||
<if test="sysOperationLogFilter.requestUrl != null and sysOperationLogFilter.requestUrl != ''">
|
||||
<bind name = "safeRequestUrl" value = "'%' + sysOperationLogFilter.requestUrl + '%'" />
|
||||
AND zz_sys_operation_log.request_url LIKE #{safeRequestUrl}
|
||||
</if>
|
||||
<if test="sysOperationLogFilter.traceId != null and sysOperationLogFilter.traceId != ''">
|
||||
AND zz_sys_operation_log.trace_id = #{sysOperationLogFilter.traceId}
|
||||
</if>
|
||||
<if test="sysOperationLogFilter.success != null">
|
||||
AND zz_sys_operation_log.success = #{sysOperationLogFilter.success}
|
||||
</if>
|
||||
<if test="sysOperationLogFilter.operatorName != null and sysOperationLogFilter.operatorName != ''">
|
||||
<bind name = "safeOperatorName" value = "'%' + sysOperationLogFilter.operatorName + '%'" />
|
||||
AND zz_sys_operation_log.operator_name LIKE #{safeOperatorName}
|
||||
</if>
|
||||
<if test="sysOperationLogFilter.elapseMin != null and sysOperationLogFilter.elapseMin != ''">
|
||||
AND zz_sys_operation_log.elapse >= #{sysOperationLogFilter.elapseMin}
|
||||
</if>
|
||||
<if test="sysOperationLogFilter.elapseMax != null and sysOperationLogFilter.elapseMax != ''">
|
||||
AND zz_sys_operation_log.elapse <= #{sysOperationLogFilter.elapseMax}
|
||||
</if>
|
||||
<if test="sysOperationLogFilter.operationTimeStart != null and sysOperationLogFilter.operationTimeStart != ''">
|
||||
AND zz_sys_operation_log.operation_time >= #{sysOperationLogFilter.operationTimeStart}
|
||||
</if>
|
||||
<if test="sysOperationLogFilter.operationTimeEnd != null and sysOperationLogFilter.operationTimeEnd != ''">
|
||||
AND zz_sys_operation_log.operation_time <= #{sysOperationLogFilter.operationTimeEnd}
|
||||
</if>
|
||||
</if>
|
||||
</sql>
|
||||
|
||||
<insert id="insertList">
|
||||
INSERT INTO zz_sys_operation_log VALUES
|
||||
<foreach collection="list" index="index" item="item" separator=",">
|
||||
(
|
||||
#{item.logId},
|
||||
#{item.description},
|
||||
#{item.operationType},
|
||||
#{item.serviceName},
|
||||
#{item.apiClass},
|
||||
#{item.apiMethod},
|
||||
#{item.sessionId},
|
||||
#{item.traceId},
|
||||
#{item.elapse},
|
||||
#{item.requestMethod},
|
||||
#{item.requestUrl},
|
||||
#{item.requestArguments},
|
||||
#{item.responseResult},
|
||||
#{item.requestIp},
|
||||
#{item.success},
|
||||
#{item.errorMsg},
|
||||
#{item.tenantId},
|
||||
#{item.operatorId},
|
||||
#{item.operatorName},
|
||||
#{item.operationTime}
|
||||
)
|
||||
</foreach>
|
||||
</insert>
|
||||
|
||||
<select id="getSysOperationLogList" resultMap="BaseResultMap" parameterType="com.orange.demo.common.log.model.SysOperationLog">
|
||||
SELECT * FROM zz_sys_operation_log
|
||||
<where>
|
||||
<include refid="filterRef"/>
|
||||
</where>
|
||||
<if test="orderBy != null and orderBy != ''">
|
||||
ORDER BY ${orderBy}
|
||||
</if>
|
||||
</select>
|
||||
</mapper>
|
||||
@@ -0,0 +1,170 @@
|
||||
package com.orange.demo.common.log.model;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
import com.orange.demo.common.core.annotation.TenantFilterColumn;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 操作日志记录表
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-09-24
|
||||
*/
|
||||
@Data
|
||||
@TableName("zz_sys_operation_log")
|
||||
public class SysOperationLog {
|
||||
|
||||
/**
|
||||
* 主键Id。
|
||||
*/
|
||||
@TableId(value = "log_id")
|
||||
private Long logId;
|
||||
|
||||
/**
|
||||
* 日志描述。
|
||||
*/
|
||||
@TableField(value = "description")
|
||||
private String description;
|
||||
|
||||
/**
|
||||
* 操作类型。
|
||||
* 常量值定义可参考SysOperationLogType对象。
|
||||
*/
|
||||
@TableField(value = "operation_type")
|
||||
private Integer operationType;
|
||||
|
||||
/**
|
||||
* 接口所在服务名称。
|
||||
* 通常为spring.application.name配置项的值。
|
||||
*/
|
||||
@TableField(value = "service_name")
|
||||
private String serviceName;
|
||||
|
||||
/**
|
||||
* 调用的controller全类名。
|
||||
* 之所以为独立字段,是为了便于查询和统计接口的调用频度。
|
||||
*/
|
||||
@TableField(value = "api_class")
|
||||
private String apiClass;
|
||||
|
||||
/**
|
||||
* 调用的controller中的方法。
|
||||
* 格式为:接口类名 + "." + 方法名。
|
||||
*/
|
||||
@TableField(value = "api_method")
|
||||
private String apiMethod;
|
||||
|
||||
/**
|
||||
* 用户会话sessionId。
|
||||
* 主要是为了便于统计,以及跟踪查询定位问题。
|
||||
*/
|
||||
@TableField(value = "session_id")
|
||||
private String sessionId;
|
||||
|
||||
/**
|
||||
* 每次请求的Id。
|
||||
* 对于微服务之间的调用,在同一个请求的调用链中,该值是相同的。
|
||||
*/
|
||||
@TableField(value = "trace_id")
|
||||
private String traceId;
|
||||
|
||||
/**
|
||||
* 调用时长。
|
||||
*/
|
||||
@TableField(value = "elapse")
|
||||
private Long elapse;
|
||||
|
||||
/**
|
||||
* HTTP 请求方法,如GET。
|
||||
*/
|
||||
@TableField(value = "request_method")
|
||||
private String requestMethod;
|
||||
|
||||
/**
|
||||
* HTTP 请求地址。
|
||||
*/
|
||||
@TableField(value = "request_url")
|
||||
private String requestUrl;
|
||||
|
||||
/**
|
||||
* controller接口参数。
|
||||
*/
|
||||
@TableField(value = "request_arguments")
|
||||
private String requestArguments;
|
||||
|
||||
/**
|
||||
* controller应答结果。
|
||||
*/
|
||||
@TableField(value = "response_result")
|
||||
private String responseResult;
|
||||
|
||||
/**
|
||||
* 请求IP。
|
||||
*/
|
||||
@TableField(value = "request_ip")
|
||||
private String requestIp;
|
||||
|
||||
/**
|
||||
* 应答状态。
|
||||
*/
|
||||
@TableField(value = "success")
|
||||
private Boolean success;
|
||||
|
||||
/**
|
||||
* 错误信息。
|
||||
*/
|
||||
@TableField(value = "error_msg")
|
||||
private String errorMsg;
|
||||
|
||||
/**
|
||||
* 租户Id。
|
||||
* 仅用于多租户系统,是便于进行对租户的操作查询和统计分析。
|
||||
*/
|
||||
@TenantFilterColumn
|
||||
@TableField(value = "tenant_id")
|
||||
private Long tenantId;
|
||||
|
||||
/**
|
||||
* 操作员Id。
|
||||
*/
|
||||
@TableField(value = "operator_id")
|
||||
private Long operatorId;
|
||||
|
||||
/**
|
||||
* 操作员名称。
|
||||
*/
|
||||
@TableField(value = "operator_name")
|
||||
private String operatorName;
|
||||
|
||||
/**
|
||||
* 操作时间。
|
||||
*/
|
||||
@TableField(value = "operation_time")
|
||||
private Date operationTime;
|
||||
|
||||
/**
|
||||
* 调用时长最小值。
|
||||
*/
|
||||
@TableField(exist = false)
|
||||
private Long elapseMin;
|
||||
|
||||
/**
|
||||
* 调用时长最大值。
|
||||
*/
|
||||
@TableField(exist = false)
|
||||
private Long elapseMax;
|
||||
|
||||
/**
|
||||
* 操作开始时间。
|
||||
*/
|
||||
@TableField(exist = false)
|
||||
private String operationTimeStart;
|
||||
|
||||
/**
|
||||
* 操作结束时间。
|
||||
*/
|
||||
@TableField(exist = false)
|
||||
private String operationTimeEnd;
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
package com.orange.demo.common.log.model.constant;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 操作日志类型常量字典对象。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-09-24
|
||||
*/
|
||||
public final class SysOperationLogType {
|
||||
|
||||
/**
|
||||
* 其他。
|
||||
*/
|
||||
public static final int OTHER = -1;
|
||||
/**
|
||||
* 登录。
|
||||
*/
|
||||
public static final int LOGIN = 0;
|
||||
/**
|
||||
* 登出。
|
||||
*/
|
||||
public static final int LOGOUT = 5;
|
||||
/**
|
||||
* 新增。
|
||||
*/
|
||||
public static final int ADD = 10;
|
||||
/**
|
||||
* 修改。
|
||||
*/
|
||||
public static final int UPDATE = 15;
|
||||
/**
|
||||
* 删除。
|
||||
*/
|
||||
public static final int DELETE = 20;
|
||||
/**
|
||||
* 新增多对多关联。
|
||||
*/
|
||||
public static final int ADD_M2M = 25;
|
||||
/**
|
||||
* 移除多对多关联。
|
||||
*/
|
||||
public static final int DELETE_M2M = 30;
|
||||
/**
|
||||
* 查询。
|
||||
*/
|
||||
public static final int LIST = 35;
|
||||
/**
|
||||
* 分组查询。
|
||||
*/
|
||||
public static final int LIST_WITH_GROUP = 40;
|
||||
/**
|
||||
* 导出。
|
||||
*/
|
||||
public static final int EXPORT = 45;
|
||||
/**
|
||||
* 上传。
|
||||
*/
|
||||
public static final int UPLOAD = 50;
|
||||
/**
|
||||
* 下载。
|
||||
*/
|
||||
public static final int DOWNLOAD = 55;
|
||||
/**
|
||||
* 重置缓存。
|
||||
*/
|
||||
public static final int RELOAD_CACHE = 60;
|
||||
/**
|
||||
* 发布。
|
||||
*/
|
||||
public static final int PUBLISH = 65;
|
||||
/**
|
||||
* 取消发布。
|
||||
*/
|
||||
public static final int UNPUBLISH = 70;
|
||||
/**
|
||||
* 暂停。
|
||||
*/
|
||||
public static final int SUSPEND = 75;
|
||||
/**
|
||||
* 恢复。
|
||||
*/
|
||||
public static final int RESUME = 80;
|
||||
/**
|
||||
* 启动流程。
|
||||
*/
|
||||
public static final int START_PROCESS = 100;
|
||||
/**
|
||||
* 停止流程。
|
||||
*/
|
||||
public static final int STOP_PROCESS = 105;
|
||||
/**
|
||||
* 删除流程。
|
||||
*/
|
||||
public static final int DELETE_PROCESS = 110;
|
||||
/**
|
||||
* 取消流程。
|
||||
*/
|
||||
public static final int CANCEL_PROCESS = 115;
|
||||
/**
|
||||
* 提交任务。
|
||||
*/
|
||||
public static final int SUBMIT_TASK = 120;
|
||||
|
||||
private static final Map<Object, String> DICT_MAP = new HashMap<>(15);
|
||||
static {
|
||||
DICT_MAP.put(OTHER, "其他");
|
||||
DICT_MAP.put(LOGIN, "登录");
|
||||
DICT_MAP.put(LOGOUT, "登出");
|
||||
DICT_MAP.put(ADD, "新增");
|
||||
DICT_MAP.put(UPDATE, "修改");
|
||||
DICT_MAP.put(DELETE, "删除");
|
||||
DICT_MAP.put(ADD_M2M, "新增多对多关联");
|
||||
DICT_MAP.put(DELETE_M2M, "移除多对多关联");
|
||||
DICT_MAP.put(LIST, "查询");
|
||||
DICT_MAP.put(LIST_WITH_GROUP, "分组查询");
|
||||
DICT_MAP.put(EXPORT, "导出");
|
||||
DICT_MAP.put(UPLOAD, "上传");
|
||||
DICT_MAP.put(DOWNLOAD, "下载");
|
||||
DICT_MAP.put(RELOAD_CACHE, "重置缓存");
|
||||
DICT_MAP.put(PUBLISH, "发布");
|
||||
DICT_MAP.put(UNPUBLISH, "取消发布");
|
||||
DICT_MAP.put(SUSPEND, "暂停");
|
||||
DICT_MAP.put(RESUME, "恢复");
|
||||
DICT_MAP.put(START_PROCESS, "启动流程");
|
||||
DICT_MAP.put(STOP_PROCESS, "停止流程");
|
||||
DICT_MAP.put(DELETE_PROCESS, "删除流程");
|
||||
DICT_MAP.put(CANCEL_PROCESS, "取消流程");
|
||||
DICT_MAP.put(SUBMIT_TASK, "提交任务");
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断参数是否为当前常量字典的合法值。
|
||||
*
|
||||
* @param value 待验证的参数值。
|
||||
* @return 合法返回true,否则false。
|
||||
*/
|
||||
public static boolean isValid(Integer value) {
|
||||
return value != null && DICT_MAP.containsKey(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 私有构造函数,明确标识该常量类的作用。
|
||||
*/
|
||||
private SysOperationLogType() {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.orange.demo.common.log.service;
|
||||
|
||||
import com.orange.demo.common.core.base.service.IBaseService;
|
||||
import com.orange.demo.common.log.model.SysOperationLog;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 操作日志服务接口。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-09-24
|
||||
*/
|
||||
public interface SysOperationLogService extends IBaseService<SysOperationLog, Long> {
|
||||
|
||||
/**
|
||||
* 异步的插入一条新操作日志。
|
||||
*
|
||||
* @param operationLog 操作日志对象。
|
||||
*/
|
||||
void saveNewAsync(SysOperationLog operationLog);
|
||||
|
||||
/**
|
||||
* 插入一条新操作日志。
|
||||
*
|
||||
* @param operationLog 操作日志对象。
|
||||
*/
|
||||
void saveNew(SysOperationLog operationLog);
|
||||
|
||||
/**
|
||||
* 批量插入。
|
||||
*
|
||||
* @param sysOperationLogList 操作日志列表。
|
||||
*/
|
||||
void batchSave(List<SysOperationLog> sysOperationLogList);
|
||||
|
||||
/**
|
||||
* 根据过滤条件和排序规则,查询操作日志。
|
||||
*
|
||||
* @param filter 操作日志的过滤对象。
|
||||
* @param orderBy 排序规则。
|
||||
* @return 查询列表。
|
||||
*/
|
||||
List<SysOperationLog> getSysOperationLogList(SysOperationLog filter, String orderBy);
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package com.orange.demo.common.log.service.impl;
|
||||
|
||||
import com.orange.demo.common.core.annotation.MyDataSource;
|
||||
import com.orange.demo.common.core.base.dao.BaseDaoMapper;
|
||||
import com.orange.demo.common.core.base.service.BaseService;
|
||||
import com.orange.demo.common.core.constant.ApplicationConstant;
|
||||
import com.orange.demo.common.log.dao.SysOperationLogMapper;
|
||||
import com.orange.demo.common.log.model.SysOperationLog;
|
||||
import com.orange.demo.common.log.service.SysOperationLogService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 操作日志服务实现类。
|
||||
* 这里需要重点解释下MyDataSource注解。在单数据源服务中,由于没有DataSourceAspect的切面类,所以该注解不会
|
||||
* 有任何作用和影响。然而在多数据源情况下,由于每个服务都有自己的DataSourceType常量对象,表示不同的数据源。
|
||||
* 而common-log在公用模块中,不能去依赖业务服务,因此这里给出了一个固定值。我们在业务的DataSourceType中,也要
|
||||
* 使用该值ApplicationConstant.OPERATION_LOG_DATASOURCE_TYPE,去关联操作日志所需的数据源配置。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-09-24
|
||||
*/
|
||||
@MyDataSource(ApplicationConstant.OPERATION_LOG_DATASOURCE_TYPE)
|
||||
@Service
|
||||
public class SysOperationLogServiceImpl extends BaseService<SysOperationLog, Long> implements SysOperationLogService {
|
||||
|
||||
@Autowired
|
||||
private SysOperationLogMapper sysOperationLogMapper;
|
||||
|
||||
@Override
|
||||
protected BaseDaoMapper<SysOperationLog> mapper() {
|
||||
return sysOperationLogMapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* 异步插入一条新操作日志。通常用于在橙单中创建的单体工程服务。
|
||||
*
|
||||
* @param operationLog 操作日志对象。
|
||||
*/
|
||||
@Async
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public void saveNewAsync(SysOperationLog operationLog) {
|
||||
sysOperationLogMapper.insert(operationLog);
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入一条新操作日志。
|
||||
*
|
||||
* @param operationLog 操作日志对象。
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public void saveNew(SysOperationLog operationLog) {
|
||||
sysOperationLogMapper.insert(operationLog);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量插入。通常用于在橙单中创建的微服务工程服务。
|
||||
*
|
||||
* @param sysOperationLogList 操作日志列表。
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public void batchSave(List<SysOperationLog> sysOperationLogList) {
|
||||
sysOperationLogMapper.insertList(sysOperationLogList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据过滤条件和排序规则,查询操作日志。
|
||||
*
|
||||
* @param filter 操作日志的过滤对象。
|
||||
* @param orderBy 排序规则。
|
||||
* @return 查询列表。
|
||||
*/
|
||||
@Override
|
||||
public List<SysOperationLog> getSysOperationLogList(SysOperationLog filter, String orderBy) {
|
||||
return sysOperationLogMapper.getSysOperationLogList(filter, orderBy);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.orange.demo.common.log.config.CommonLogAutoConfig
|
||||
@@ -1,56 +0,0 @@
|
||||
package com.orange.demo.common.swagger.config;
|
||||
|
||||
import com.orange.demo.common.core.annotation.MyRequestBody;
|
||||
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import springfox.documentation.builders.ApiInfoBuilder;
|
||||
import springfox.documentation.builders.PathSelectors;
|
||||
import springfox.documentation.builders.RequestHandlerSelectors;
|
||||
import springfox.documentation.service.ApiInfo;
|
||||
import springfox.documentation.spi.DocumentationType;
|
||||
import springfox.documentation.spring.web.plugins.Docket;
|
||||
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
|
||||
|
||||
/**
|
||||
* 自动加载bean的配置对象。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-09-24
|
||||
*/
|
||||
@EnableSwagger2WebMvc
|
||||
@EnableKnife4j
|
||||
@EnableConfigurationProperties(SwaggerProperties.class)
|
||||
@ConditionalOnProperty(prefix = "swagger", name = "enabled")
|
||||
public class SwaggerAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public Docket upmsDocket(SwaggerProperties properties) {
|
||||
return new Docket(DocumentationType.SWAGGER_2)
|
||||
.groupName("1. 用户权限分组接口")
|
||||
.ignoredParameterTypes(MyRequestBody.class)
|
||||
.apiInfo(apiInfo(properties))
|
||||
.select()
|
||||
.apis(RequestHandlerSelectors.basePackage(properties.getBasePackage() + ".upms.controller"))
|
||||
.paths(PathSelectors.any()).build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Docket bizDocket(SwaggerProperties properties) {
|
||||
return new Docket(DocumentationType.SWAGGER_2)
|
||||
.groupName("2. 业务应用分组接口")
|
||||
.ignoredParameterTypes(MyRequestBody.class)
|
||||
.apiInfo(apiInfo(properties))
|
||||
.select()
|
||||
.apis(RequestHandlerSelectors.basePackage(properties.getBasePackage() + ".app.controller"))
|
||||
.paths(PathSelectors.any()).build();
|
||||
}
|
||||
|
||||
private ApiInfo apiInfo(SwaggerProperties properties) {
|
||||
return new ApiInfoBuilder()
|
||||
.title(properties.getTitle())
|
||||
.description(properties.getDescription())
|
||||
.version(properties.getVersion()).build();
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package com.orange.demo.common.swagger.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* 配置参数对象。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-09-24
|
||||
*/
|
||||
@Data
|
||||
@ConfigurationProperties("swagger")
|
||||
public class SwaggerProperties {
|
||||
|
||||
/**
|
||||
* 是否开启Swagger。
|
||||
*/
|
||||
private Boolean enabled;
|
||||
|
||||
/**
|
||||
* Swagger解析的基础包路径。
|
||||
**/
|
||||
private String basePackage = "";
|
||||
|
||||
/**
|
||||
* ApiInfo中的标题。
|
||||
**/
|
||||
private String title = "";
|
||||
|
||||
/**
|
||||
* ApiInfo中的描述信息。
|
||||
**/
|
||||
private String description = "";
|
||||
|
||||
/**
|
||||
* ApiInfo中的版本信息。
|
||||
**/
|
||||
private String version = "";
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
package com.orange.demo.common.swagger.plugin;
|
||||
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import com.orange.demo.common.core.annotation.MyRequestBody;
|
||||
import com.github.xiaoymin.knife4j.core.conf.Consts;
|
||||
import javassist.*;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import springfox.documentation.service.ResolvedMethodParameter;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 通过字节码方式动态创建接口参数封装对象。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-09-24
|
||||
*/
|
||||
@Slf4j
|
||||
public class ByteBuddyUtil {
|
||||
private static final ClassPool CLASS_POOL = ClassPool.getDefault();
|
||||
|
||||
public static Class<?> createDynamicModelClass(String name, List<ResolvedMethodParameter> parameters) {
|
||||
String clazzName = Consts.BASE_PACKAGE_PREFIX + name;
|
||||
try {
|
||||
CtClass tmp = CLASS_POOL.getCtClass(clazzName);
|
||||
if (tmp != null) {
|
||||
tmp.detach();
|
||||
}
|
||||
} catch (NotFoundException e) {
|
||||
// 需要吃掉这个异常。
|
||||
}
|
||||
CtClass ctClass = CLASS_POOL.makeClass(clazzName);
|
||||
try {
|
||||
int fieldCount = 0;
|
||||
for (ResolvedMethodParameter dynamicParameter : parameters) {
|
||||
// 因为在调用这个方法之前,这些参数都包含MyRequestBody注解。
|
||||
MyRequestBody myRequestBody =
|
||||
dynamicParameter.findAnnotation(MyRequestBody.class).orElse(null);
|
||||
Assert.notNull(myRequestBody);
|
||||
String fieldName = dynamicParameter.defaultName().isPresent()
|
||||
? dynamicParameter.defaultName().get() : "parameter";
|
||||
if (StringUtils.isNotBlank(myRequestBody.value())) {
|
||||
fieldName = myRequestBody.value();
|
||||
}
|
||||
ctClass.addField(createField(dynamicParameter, fieldName, ctClass));
|
||||
fieldCount++;
|
||||
}
|
||||
if (fieldCount > 0) {
|
||||
return ctClass.toClass();
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static CtField createField(ResolvedMethodParameter parameter, String parameterName, CtClass ctClass)
|
||||
throws NotFoundException, CannotCompileException {
|
||||
CtField field = new CtField(getFieldType(parameter.getParameterType().getErasedType()), parameterName, ctClass);
|
||||
field.setModifiers(Modifier.PUBLIC);
|
||||
return field;
|
||||
}
|
||||
|
||||
private static CtClass getFieldType(Class<?> propetyType) {
|
||||
CtClass fieldType = null;
|
||||
try {
|
||||
if (!propetyType.isAssignableFrom(Void.class)) {
|
||||
fieldType = CLASS_POOL.get(propetyType.getName());
|
||||
} else {
|
||||
fieldType = CLASS_POOL.get(String.class.getName());
|
||||
}
|
||||
} catch (NotFoundException e) {
|
||||
// 抛异常
|
||||
ClassClassPath path = new ClassClassPath(propetyType);
|
||||
CLASS_POOL.insertClassPath(path);
|
||||
try {
|
||||
fieldType = CLASS_POOL.get(propetyType.getName());
|
||||
} catch (NotFoundException e1) {
|
||||
log.error(e1.getMessage(), e1);
|
||||
}
|
||||
}
|
||||
return fieldType;
|
||||
}
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
package com.orange.demo.common.swagger.plugin;
|
||||
|
||||
import com.orange.demo.common.core.annotation.MyRequestBody;
|
||||
import com.fasterxml.classmate.TypeResolver;
|
||||
import com.google.common.base.CaseFormat;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
import springfox.documentation.service.ResolvedMethodParameter;
|
||||
import springfox.documentation.spi.DocumentationType;
|
||||
import springfox.documentation.spi.service.OperationModelsProviderPlugin;
|
||||
import springfox.documentation.spi.service.contexts.RequestMappingContext;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 生成参数包装类的插件。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-09-24
|
||||
*/
|
||||
@Component
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE + 200)
|
||||
@ConditionalOnProperty(prefix = "swagger", name = "enabled")
|
||||
public class DynamicBodyModelPlugin implements OperationModelsProviderPlugin {
|
||||
|
||||
private final TypeResolver typeResolver;
|
||||
|
||||
public DynamicBodyModelPlugin(TypeResolver typeResolver) {
|
||||
this.typeResolver = typeResolver;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply(RequestMappingContext context) {
|
||||
List<ResolvedMethodParameter> parameterTypes = context.getParameters();
|
||||
if (CollectionUtils.isEmpty(parameterTypes)) {
|
||||
return;
|
||||
}
|
||||
List<ResolvedMethodParameter> bodyParameter = parameterTypes.stream()
|
||||
.filter(p -> p.hasParameterAnnotation(MyRequestBody.class)).collect(Collectors.toList());
|
||||
if (CollectionUtils.isEmpty(bodyParameter)) {
|
||||
return;
|
||||
}
|
||||
String groupName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, context.getGroupName());
|
||||
String clazzName = groupName + StringUtils.capitalize(context.getName());
|
||||
Class<?> clazz = ByteBuddyUtil.createDynamicModelClass(clazzName, bodyParameter);
|
||||
if (clazz != null) {
|
||||
context.operationModelsBuilder().addInputParam(typeResolver.resolve(clazz));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supports(DocumentationType delimiter) {
|
||||
// 支持2.0版本
|
||||
return delimiter == DocumentationType.SWAGGER_2;
|
||||
}
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
package com.orange.demo.common.swagger.plugin;
|
||||
|
||||
import com.orange.demo.common.core.annotation.MyRequestBody;
|
||||
import com.google.common.base.CaseFormat;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
import springfox.documentation.builders.ParameterBuilder;
|
||||
import springfox.documentation.schema.ModelRef;
|
||||
import springfox.documentation.service.Parameter;
|
||||
import springfox.documentation.service.ResolvedMethodParameter;
|
||||
import springfox.documentation.spi.DocumentationType;
|
||||
import springfox.documentation.spi.service.OperationBuilderPlugin;
|
||||
import springfox.documentation.spi.service.contexts.OperationContext;
|
||||
import springfox.documentation.spi.service.contexts.ParameterContext;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 构建操作接口参数对象的插件。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-09-24
|
||||
*/
|
||||
@Component
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE + 102)
|
||||
@ConditionalOnProperty(prefix = "swagger", name = "enabled")
|
||||
public class DynamicBodyParameterBuilder implements OperationBuilderPlugin {
|
||||
|
||||
@Override
|
||||
public void apply(OperationContext context) {
|
||||
List<ResolvedMethodParameter> methodParameters = context.getParameters();
|
||||
List<Parameter> parameters = new ArrayList<>();
|
||||
if (CollectionUtils.isNotEmpty(methodParameters)) {
|
||||
List<ResolvedMethodParameter> bodyParameter = methodParameters.stream()
|
||||
.filter(p -> p.hasParameterAnnotation(MyRequestBody.class)).collect(Collectors.toList());
|
||||
if (CollectionUtils.isNotEmpty(bodyParameter)) {
|
||||
// 构造model
|
||||
String groupName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, context.getGroupName());
|
||||
String clazzName = groupName + StringUtils.capitalize(context.getName());
|
||||
ResolvedMethodParameter methodParameter = bodyParameter.get(0);
|
||||
ParameterContext parameterContext = new ParameterContext(methodParameter,
|
||||
new ParameterBuilder(),
|
||||
context.getDocumentationContext(),
|
||||
context.getGenericsNamingStrategy(),
|
||||
context);
|
||||
Parameter parameter = parameterContext.parameterBuilder()
|
||||
.parameterType("body").modelRef(new ModelRef(clazzName)).name(clazzName).build();
|
||||
parameters.add(parameter);
|
||||
}
|
||||
}
|
||||
context.operationBuilder().parameters(parameters);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supports(DocumentationType delimiter) {
|
||||
return delimiter == DocumentationType.SWAGGER_2;
|
||||
}
|
||||
}
|
||||
@@ -13,8 +13,9 @@
|
||||
|
||||
<modules>
|
||||
<module>common-core</module>
|
||||
<module>common-log</module>
|
||||
<module>common-datafilter</module>
|
||||
<module>common-redis</module>
|
||||
<module>common-sequence</module>
|
||||
<module>common-swagger</module>
|
||||
</modules>
|
||||
</project>
|
||||
|
||||
Reference in New Issue
Block a user