mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
feat: add selectColumns config for @Relation annotations
This commit is contained in:
parent
198db373c5
commit
0203cb76bd
@ -99,6 +99,11 @@ public @interface RelationManyToMany {
|
|||||||
*/
|
*/
|
||||||
String extraCondition() default "";
|
String extraCondition() default "";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询(加载)指定的列
|
||||||
|
*/
|
||||||
|
String[] selectColumns() default {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询排序。
|
* 查询排序。
|
||||||
*
|
*
|
||||||
|
|||||||
@ -95,4 +95,9 @@ public @interface RelationManyToOne {
|
|||||||
*/
|
*/
|
||||||
String dataSource() default "";
|
String dataSource() default "";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询(加载)指定的列
|
||||||
|
*/
|
||||||
|
String[] selectColumns() default {};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -99,6 +99,11 @@ public @interface RelationOneToMany {
|
|||||||
*/
|
*/
|
||||||
String extraCondition() default "";
|
String extraCondition() default "";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询(加载)指定的列
|
||||||
|
*/
|
||||||
|
String[] selectColumns() default {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询排序。
|
* 查询排序。
|
||||||
*
|
*
|
||||||
@ -120,4 +125,5 @@ public @interface RelationOneToMany {
|
|||||||
*/
|
*/
|
||||||
String dataSource() default "";
|
String dataSource() default "";
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -95,4 +95,9 @@ public @interface RelationOneToOne {
|
|||||||
*/
|
*/
|
||||||
String dataSource() default "";
|
String dataSource() default "";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询(加载)指定的列
|
||||||
|
*/
|
||||||
|
String[] selectColumns() default {};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -16,11 +16,13 @@
|
|||||||
package com.mybatisflex.core.relation;
|
package com.mybatisflex.core.relation;
|
||||||
|
|
||||||
import com.mybatisflex.core.exception.FlexExceptions;
|
import com.mybatisflex.core.exception.FlexExceptions;
|
||||||
|
import com.mybatisflex.core.query.QueryColumn;
|
||||||
import com.mybatisflex.core.query.QueryWrapper;
|
import com.mybatisflex.core.query.QueryWrapper;
|
||||||
import com.mybatisflex.core.row.Row;
|
import com.mybatisflex.core.row.Row;
|
||||||
import com.mybatisflex.core.table.IdInfo;
|
import com.mybatisflex.core.table.IdInfo;
|
||||||
import com.mybatisflex.core.table.TableInfo;
|
import com.mybatisflex.core.table.TableInfo;
|
||||||
import com.mybatisflex.core.table.TableInfoFactory;
|
import com.mybatisflex.core.table.TableInfoFactory;
|
||||||
|
import com.mybatisflex.core.util.ArrayUtil;
|
||||||
import com.mybatisflex.core.util.ClassUtil;
|
import com.mybatisflex.core.util.ClassUtil;
|
||||||
import com.mybatisflex.core.util.FieldWrapper;
|
import com.mybatisflex.core.util.FieldWrapper;
|
||||||
import com.mybatisflex.core.util.StringUtil;
|
import com.mybatisflex.core.util.StringUtil;
|
||||||
@ -57,10 +59,13 @@ abstract class AbstractRelation<SelfEntity> {
|
|||||||
protected String extraConditionSql;
|
protected String extraConditionSql;
|
||||||
protected List<String> extraConditionParamKeys;
|
protected List<String> extraConditionParamKeys;
|
||||||
|
|
||||||
|
protected QueryColumn conditionColumn;
|
||||||
|
protected String[] selectColumns;
|
||||||
|
|
||||||
public AbstractRelation(String selfField, String targetSchema, String targetTable, String targetField,
|
public AbstractRelation(String selfField, String targetSchema, String targetTable, String targetField,
|
||||||
String joinTable, String joinSelfColumn, String joinTargetColumn,
|
String joinTable, String joinSelfColumn, String joinTargetColumn,
|
||||||
String dataSource, Class<SelfEntity> entityClass, Field relationField,
|
String dataSource, Class<SelfEntity> entityClass, Field relationField,
|
||||||
String extraCondition
|
String extraCondition, String[] selectColumns
|
||||||
) {
|
) {
|
||||||
this.name = entityClass.getSimpleName() + "." + relationField.getName();
|
this.name = entityClass.getSimpleName() + "." + relationField.getName();
|
||||||
this.simpleName = relationField.getName();
|
this.simpleName = relationField.getName();
|
||||||
@ -87,6 +92,17 @@ abstract class AbstractRelation<SelfEntity> {
|
|||||||
|
|
||||||
this.targetTableInfo = TableInfoFactory.ofEntityClass(targetEntityClass);
|
this.targetTableInfo = TableInfoFactory.ofEntityClass(targetEntityClass);
|
||||||
|
|
||||||
|
this.conditionColumn = column(targetTableInfo.getColumnByProperty(this.targetField.getName()));
|
||||||
|
|
||||||
|
if (ArrayUtil.isNotEmpty(selectColumns)) {
|
||||||
|
if (ArrayUtil.contains(selectColumns, conditionColumn.getName())) {
|
||||||
|
this.selectColumns = selectColumns;
|
||||||
|
} else {
|
||||||
|
//需要追加 conditionColumn,因为进行内存 join 的时候,需要用到这个内容进行对比
|
||||||
|
this.selectColumns = ArrayUtil.concat(selectColumns, new String[]{conditionColumn.getName()});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
initExtraCondition(extraCondition);
|
initExtraCondition(extraCondition);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -323,14 +339,18 @@ abstract class AbstractRelation<SelfEntity> {
|
|||||||
* @return QueryWrapper
|
* @return QueryWrapper
|
||||||
*/
|
*/
|
||||||
public QueryWrapper buildQueryWrapper(Set<Object> targetValues) {
|
public QueryWrapper buildQueryWrapper(Set<Object> targetValues) {
|
||||||
QueryWrapper queryWrapper = QueryWrapper.create()
|
QueryWrapper queryWrapper = QueryWrapper.create();
|
||||||
.select()
|
|
||||||
.from(getTargetTableWithSchema());
|
if (ArrayUtil.isNotEmpty(selectColumns)) {
|
||||||
|
queryWrapper.select(selectColumns);
|
||||||
|
}
|
||||||
|
|
||||||
|
queryWrapper.from(getTargetTableWithSchema());
|
||||||
|
|
||||||
if (targetValues.size() > 1) {
|
if (targetValues.size() > 1) {
|
||||||
queryWrapper.where(column(targetTableInfo.getColumnByProperty(targetField.getName())).in(targetValues));
|
queryWrapper.where(conditionColumn.in(targetValues));
|
||||||
} else {
|
} else {
|
||||||
queryWrapper.where(column(targetTableInfo.getColumnByProperty(targetField.getName())).eq(targetValues.iterator().next()));
|
queryWrapper.where(conditionColumn.eq(targetValues.iterator().next()));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (StringUtil.isNotBlank(extraConditionSql)) {
|
if (StringUtil.isNotBlank(extraConditionSql)) {
|
||||||
@ -344,7 +364,7 @@ abstract class AbstractRelation<SelfEntity> {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 方便子类最近自定义的条件
|
* 方便子类追加自定义的条件
|
||||||
*
|
*
|
||||||
* @param queryWrapper 查询条件
|
* @param queryWrapper 查询条件
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -30,7 +30,8 @@ class ManyToMany<SelfEntity> extends ToManyRelation<SelfEntity> {
|
|||||||
, annotation.joinSelfColumn()
|
, annotation.joinSelfColumn()
|
||||||
, annotation.joinTargetColumn()
|
, annotation.joinTargetColumn()
|
||||||
, annotation.dataSource(), entityClass, relationField
|
, annotation.dataSource(), entityClass, relationField
|
||||||
, annotation.extraCondition());
|
, annotation.extraCondition()
|
||||||
|
, annotation.selectColumns());
|
||||||
|
|
||||||
this.orderBy = annotation.orderBy();
|
this.orderBy = annotation.orderBy();
|
||||||
this.setMapKeyField(annotation.mapKeyField());
|
this.setMapKeyField(annotation.mapKeyField());
|
||||||
|
|||||||
@ -32,7 +32,8 @@ class ManyToOne<SelfEntity> extends ToOneRelation<SelfEntity> {
|
|||||||
, annotation.joinTargetColumn()
|
, annotation.joinTargetColumn()
|
||||||
, annotation.dataSource()
|
, annotation.dataSource()
|
||||||
, entityClass
|
, entityClass
|
||||||
, relationField);
|
, relationField
|
||||||
|
, annotation.selectColumns());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -31,7 +31,8 @@ class OneToMany<SelfEntity> extends ToManyRelation<SelfEntity> {
|
|||||||
, annotation.joinSelfColumn()
|
, annotation.joinSelfColumn()
|
||||||
, annotation.joinTargetColumn()
|
, annotation.joinTargetColumn()
|
||||||
, annotation.dataSource(), entityClass, relationField
|
, annotation.dataSource(), entityClass, relationField
|
||||||
, annotation.extraCondition());
|
, annotation.extraCondition()
|
||||||
|
, annotation.selectColumns());
|
||||||
|
|
||||||
this.orderBy = annotation.orderBy();
|
this.orderBy = annotation.orderBy();
|
||||||
this.limit = annotation.limit();
|
this.limit = annotation.limit();
|
||||||
@ -40,6 +41,4 @@ class OneToMany<SelfEntity> extends ToManyRelation<SelfEntity> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -32,7 +32,8 @@ class OneToOne<SelfEntity> extends ToOneRelation<SelfEntity> {
|
|||||||
, annotation.joinTargetColumn()
|
, annotation.joinTargetColumn()
|
||||||
, annotation.dataSource()
|
, annotation.dataSource()
|
||||||
, entityClass
|
, entityClass
|
||||||
, relationField);
|
, relationField
|
||||||
|
, annotation.selectColumns());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,6 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
package com.mybatisflex.core.relation;
|
package com.mybatisflex.core.relation;
|
||||||
|
|
||||||
|
import com.mybatisflex.core.exception.FlexExceptions;
|
||||||
import com.mybatisflex.core.query.QueryWrapper;
|
import com.mybatisflex.core.query.QueryWrapper;
|
||||||
import com.mybatisflex.core.row.Row;
|
import com.mybatisflex.core.row.Row;
|
||||||
import com.mybatisflex.core.util.*;
|
import com.mybatisflex.core.util.*;
|
||||||
@ -33,11 +34,11 @@ class ToManyRelation<SelfEntity> extends AbstractRelation<SelfEntity> {
|
|||||||
public ToManyRelation(String selfField, String targetSchema, String targetTable, String targetField,
|
public ToManyRelation(String selfField, String targetSchema, String targetTable, String targetField,
|
||||||
String joinTable, String joinSelfColumn, String joinTargetColumn,
|
String joinTable, String joinSelfColumn, String joinTargetColumn,
|
||||||
String dataSource, Class<SelfEntity> selfEntityClass, Field relationField,
|
String dataSource, Class<SelfEntity> selfEntityClass, Field relationField,
|
||||||
String extraCondition) {
|
String extraCondition, String[] selectColumns) {
|
||||||
super(selfField, targetSchema, targetTable, targetField,
|
super(selfField, targetSchema, targetTable, targetField,
|
||||||
joinTable, joinSelfColumn, joinTargetColumn,
|
joinTable, joinSelfColumn, joinTargetColumn,
|
||||||
dataSource, selfEntityClass, relationField,
|
dataSource, selfEntityClass, relationField,
|
||||||
extraCondition
|
extraCondition, selectColumns
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -113,6 +114,10 @@ class ToManyRelation<SelfEntity> extends AbstractRelation<SelfEntity> {
|
|||||||
this.mapKeyField = mapKeyField;
|
this.mapKeyField = mapKeyField;
|
||||||
if (StringUtil.isNotBlank(mapKeyField)) {
|
if (StringUtil.isNotBlank(mapKeyField)) {
|
||||||
this.mapKeyFieldWrapper = FieldWrapper.of(targetEntityClass, mapKeyField);
|
this.mapKeyFieldWrapper = FieldWrapper.of(targetEntityClass, mapKeyField);
|
||||||
|
} else {
|
||||||
|
if (Map.class.isAssignableFrom(relationFieldWrapper.getFieldType())) {
|
||||||
|
throw FlexExceptions.wrap("Please config mapKeyField for map field: " + relationFieldWrapper.getField());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -25,11 +25,11 @@ class ToOneRelation<SelfEntity> extends AbstractRelation<SelfEntity> {
|
|||||||
|
|
||||||
public ToOneRelation(String selfField, String targetSchema, String targetTable, String targetField,
|
public ToOneRelation(String selfField, String targetSchema, String targetTable, String targetField,
|
||||||
String joinTable, String joinSelfColumn, String joinTargetColumn,
|
String joinTable, String joinSelfColumn, String joinTargetColumn,
|
||||||
String dataSource, Class<SelfEntity> selfEntityClass, Field relationField) {
|
String dataSource, Class<SelfEntity> selfEntityClass, Field relationField, String[] selectColumns) {
|
||||||
super(selfField, targetSchema, targetTable, targetField,
|
super(selfField, targetSchema, targetTable, targetField,
|
||||||
joinTable, joinSelfColumn, joinTargetColumn,
|
joinTable, joinSelfColumn, joinTargetColumn,
|
||||||
dataSource, selfEntityClass, relationField,
|
dataSource, selfEntityClass, relationField,
|
||||||
null
|
null, selectColumns
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -26,6 +26,7 @@ public class FieldWrapper {
|
|||||||
|
|
||||||
public static Map<Class<?>, Map<String, FieldWrapper>> cache = new ConcurrentHashMap<>();
|
public static Map<Class<?>, Map<String, FieldWrapper>> cache = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
private Field field;
|
||||||
private Class<?> fieldType;
|
private Class<?> fieldType;
|
||||||
private Class<?> mappingType;
|
private Class<?> mappingType;
|
||||||
private Class<?> keyType;
|
private Class<?> keyType;
|
||||||
@ -64,6 +65,7 @@ public class FieldWrapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fieldWrapper = new FieldWrapper();
|
fieldWrapper = new FieldWrapper();
|
||||||
|
fieldWrapper.field = findField;
|
||||||
fieldWrapper.fieldType = findField.getType();
|
fieldWrapper.fieldType = findField.getType();
|
||||||
initMappingTypeAndKeyType(clazz, findField, fieldWrapper);
|
initMappingTypeAndKeyType(clazz, findField, fieldWrapper);
|
||||||
|
|
||||||
@ -132,4 +134,8 @@ public class FieldWrapper {
|
|||||||
public Class<?> getKeyType() {
|
public Class<?> getKeyType() {
|
||||||
return keyType;
|
return keyType;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Field getField() {
|
||||||
|
return field;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user