mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 01:28:24 +08:00
feat: relation queries add ignore config
This commit is contained in:
parent
c13bf3b108
commit
d3655e3b92
@ -33,6 +33,7 @@ import static com.mybatisflex.core.query.QueryMethods.column;
|
||||
abstract class AbstractRelation<SelfEntity> {
|
||||
|
||||
protected String name;
|
||||
protected String simpleName;
|
||||
protected Class<SelfEntity> selfEntityClass;
|
||||
protected Field relationField;
|
||||
protected FieldWrapper relationFieldWrapper;
|
||||
@ -62,6 +63,7 @@ abstract class AbstractRelation<SelfEntity> {
|
||||
String extraCondition
|
||||
) {
|
||||
this.name = entityClass.getSimpleName()+"."+relationField.getName();
|
||||
this.simpleName = relationField.getName();
|
||||
this.selfEntityClass = entityClass;
|
||||
this.relationField = relationField;
|
||||
this.relationFieldWrapper = FieldWrapper.of(entityClass, relationField.getName());
|
||||
@ -139,8 +141,8 @@ abstract class AbstractRelation<SelfEntity> {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
public String getSimpleName() {
|
||||
return simpleName;
|
||||
}
|
||||
|
||||
public Class<SelfEntity> getSelfEntityClass() {
|
||||
|
||||
@ -56,6 +56,12 @@ public class RelationManager {
|
||||
private static ThreadLocal<Map<String, Object>> extraConditionParams = new ThreadLocal<>();
|
||||
|
||||
|
||||
/**
|
||||
* 查询时,可忽略某些已经添加 Relation 注解的属性
|
||||
*/
|
||||
private static ThreadLocal<Set<String>> ignoreRelations = new ThreadLocal<>();
|
||||
|
||||
|
||||
private static List<AbstractRelation> getRelations(Class<?> clazz) {
|
||||
return MapUtil.computeIfAbsent(classRelations, clazz, RelationManager::doGetRelations);
|
||||
}
|
||||
@ -112,6 +118,23 @@ public class RelationManager {
|
||||
return extraConditionParams.get();
|
||||
}
|
||||
|
||||
public static void setIgnoreRelations(Set<String> ignoreRelations) {
|
||||
RelationManager.ignoreRelations.set(ignoreRelations);
|
||||
}
|
||||
|
||||
public static void addIgnoreRelations(String... ignoreRelations) {
|
||||
Set<String> relations = RelationManager.ignoreRelations.get();
|
||||
if (relations == null) {
|
||||
relations = new HashSet<>();
|
||||
setIgnoreRelations(relations);
|
||||
}
|
||||
relations.addAll(Arrays.asList(ignoreRelations));
|
||||
}
|
||||
|
||||
public static Set<String> getIgnoreRelations() {
|
||||
return ignoreRelations.get();
|
||||
}
|
||||
|
||||
|
||||
static Object[] getExtraConditionParams(List<String> keys) {
|
||||
if (keys == null || keys.isEmpty()) {
|
||||
@ -132,12 +155,12 @@ public class RelationManager {
|
||||
|
||||
|
||||
public static <Entity> void queryRelations(BaseMapper<?> mapper, List<Entity> entities) {
|
||||
doQueryRelations(mapper, entities, 0, depthThreadLocal.get());
|
||||
doQueryRelations(mapper, entities, 0, depthThreadLocal.get(), ignoreRelations.get());
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
static <Entity> void doQueryRelations(BaseMapper<?> mapper, List<Entity> entities, int currentDepth, int maxDepth) {
|
||||
static <Entity> void doQueryRelations(BaseMapper<?> mapper, List<Entity> entities, int currentDepth, int maxDepth, Set<String> ignoreRelations) {
|
||||
if (CollectionUtil.isEmpty(entities)) {
|
||||
return;
|
||||
}
|
||||
@ -155,6 +178,12 @@ public class RelationManager {
|
||||
try {
|
||||
relations.forEach(relation -> {
|
||||
|
||||
//ignore
|
||||
if (ignoreRelations != null && (ignoreRelations.contains(relation.getSimpleName())
|
||||
|| ignoreRelations.contains(relation.getName()))) {
|
||||
return;
|
||||
}
|
||||
|
||||
Set<Object> targetValues;
|
||||
List<Row> mappingRows = null;
|
||||
|
||||
@ -205,7 +234,7 @@ public class RelationManager {
|
||||
QueryWrapper queryWrapper = relation.buildQueryWrapper(targetValues);
|
||||
List<?> targetObjectList = mapper.selectListByQueryAs(queryWrapper, relation.getMappingType());
|
||||
if (CollectionUtil.isNotEmpty(targetObjectList)) {
|
||||
doQueryRelations(mapper, targetObjectList, currentDepth + 1, maxDepth);
|
||||
doQueryRelations(mapper, targetObjectList, currentDepth + 1, maxDepth, ignoreRelations);
|
||||
relation.join(entities, targetObjectList, mappingRows);
|
||||
}
|
||||
} finally {
|
||||
|
||||
@ -103,6 +103,16 @@ public class RelationsTester {
|
||||
System.out.println( JSON.toJSONString(menus));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMenuIgnoreParent() {
|
||||
QueryWrapper qw = QueryWrapper.create();
|
||||
qw.where(MENU.PARENT_ID.eq(0));
|
||||
|
||||
RelationManager.addIgnoreRelations("parent");
|
||||
List<Menu> menus = menuMapper.selectListWithRelationsByQuery(qw);
|
||||
System.out.println( JSON.toJSONString(menus));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPaginate() {
|
||||
Page<Account> accountPage = accountMapper.paginateWithRelations(1, 2, QueryWrapper.create());
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user