mirror of
https://gitee.com/best_handsome/mybatis-plus-join
synced 2025-07-11 00:02:22 +08:00
1.2.5版本临时分支
This commit is contained in:
parent
00311e5dda
commit
68c6f67b65
@ -10,10 +10,13 @@ import com.github.yulichang.toolkit.Constant;
|
|||||||
import com.github.yulichang.toolkit.ReflectionKit;
|
import com.github.yulichang.toolkit.ReflectionKit;
|
||||||
import com.github.yulichang.toolkit.support.SelectColumn;
|
import com.github.yulichang.toolkit.support.SelectColumn;
|
||||||
import com.github.yulichang.wrapper.MPJLambdaWrapper;
|
import com.github.yulichang.wrapper.MPJLambdaWrapper;
|
||||||
|
import com.github.yulichang.wrapper.resultmap.MybatisLabel;
|
||||||
|
import com.github.yulichang.wrapper.resultmap.Result;
|
||||||
import org.apache.ibatis.executor.Executor;
|
import org.apache.ibatis.executor.Executor;
|
||||||
import org.apache.ibatis.logging.Log;
|
import org.apache.ibatis.logging.Log;
|
||||||
import org.apache.ibatis.logging.LogFactory;
|
import org.apache.ibatis.logging.LogFactory;
|
||||||
import org.apache.ibatis.mapping.MappedStatement;
|
import org.apache.ibatis.mapping.MappedStatement;
|
||||||
|
import org.apache.ibatis.mapping.ResultFlag;
|
||||||
import org.apache.ibatis.mapping.ResultMap;
|
import org.apache.ibatis.mapping.ResultMap;
|
||||||
import org.apache.ibatis.mapping.ResultMapping;
|
import org.apache.ibatis.mapping.ResultMapping;
|
||||||
import org.apache.ibatis.plugin.Interceptor;
|
import org.apache.ibatis.plugin.Interceptor;
|
||||||
@ -30,6 +33,7 @@ import org.apache.ibatis.type.UnknownTypeHandler;
|
|||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 连表拦截器
|
* 连表拦截器
|
||||||
@ -53,11 +57,6 @@ public class MPJInterceptor implements Interceptor {
|
|||||||
*/
|
*/
|
||||||
private static final Map<String, Map<Configuration, MappedStatement>> MS_CACHE = new ConcurrentHashMap<>();
|
private static final Map<String, Map<Configuration, MappedStatement>> MS_CACHE = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
/**
|
|
||||||
* 打印 MPJ resultMap
|
|
||||||
*/
|
|
||||||
private static final boolean printResultMap = false;
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings({"Java8MapApi", "unchecked"})
|
@SuppressWarnings({"Java8MapApi", "unchecked"})
|
||||||
@ -137,10 +136,7 @@ public class MPJInterceptor implements Interceptor {
|
|||||||
if (ms.getKeyProperties() != null && ms.getKeyProperties().length != 0) {
|
if (ms.getKeyProperties() != null && ms.getKeyProperties().length != 0) {
|
||||||
builder.keyProperty(String.join(StringPool.COMMA, ms.getKeyProperties()));
|
builder.keyProperty(String.join(StringPool.COMMA, ms.getKeyProperties()));
|
||||||
}
|
}
|
||||||
List<ResultMap> resultMaps = new ArrayList<>();
|
List<ResultMap> resultMaps = buildResultMap(ms, resultType, ew);
|
||||||
ResultMap resultMap = buildResultMap(ms, resultType, ew);
|
|
||||||
resultMaps.add(resultMap);
|
|
||||||
printResultMap(resultMap);
|
|
||||||
builder.resultMaps(resultMaps);
|
builder.resultMaps(resultMaps);
|
||||||
return builder.build();
|
return builder.build();
|
||||||
}
|
}
|
||||||
@ -149,65 +145,94 @@ public class MPJInterceptor implements Interceptor {
|
|||||||
* 构建resultMap TODO 可以用lambda简化代码
|
* 构建resultMap TODO 可以用lambda简化代码
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||||
private ResultMap buildResultMap(MappedStatement ms, Class<?> resultType, Object obj) {
|
private List<ResultMap> buildResultMap(MappedStatement ms, Class<?> resultType, Object obj) {
|
||||||
|
List<ResultMap> result = new ArrayList<>();
|
||||||
TableInfo tableInfo = TableInfoHelper.getTableInfo(resultType);
|
TableInfo tableInfo = TableInfoHelper.getTableInfo(resultType);
|
||||||
String currentNamespace = ms.getResource().split(StringPool.SPACE)[0];
|
String id = ms.getId() + StringPool.DOT + Constants.MYBATIS_PLUS + StringPool.UNDERSCORE + resultType.getName();
|
||||||
String id = currentNamespace + StringPool.DOT + Constants.MYBATIS_PLUS + StringPool.UNDERSCORE + resultType.getSimpleName();
|
|
||||||
|
|
||||||
if (!(obj instanceof MPJLambdaWrapper) || Map.class.isAssignableFrom(resultType) ||
|
if (!(obj instanceof MPJLambdaWrapper) || Map.class.isAssignableFrom(resultType) ||
|
||||||
ReflectionKit.isPrimitiveOrWrapper(resultType) ||
|
ReflectionKit.isPrimitiveOrWrapper(resultType) ||
|
||||||
Collection.class.isAssignableFrom(resultType)) {
|
Collection.class.isAssignableFrom(resultType)) {
|
||||||
return getDefaultResultMap(tableInfo, ms, resultType, id);
|
result.add(getDefaultResultMap(tableInfo, ms, resultType, id));
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
MPJLambdaWrapper wrapper = (MPJLambdaWrapper) obj;
|
MPJLambdaWrapper wrapper = (MPJLambdaWrapper) obj;
|
||||||
Map<String, Field> fieldMap = ReflectionKit.getFieldMap(resultType);
|
Map<String, Field> fieldMap = ReflectionKit.getFieldMap(resultType);
|
||||||
|
List<SelectColumn> columnList = wrapper.getSelectColumns();
|
||||||
if (wrapper.isResultMap()) {
|
List<ResultMapping> resultMappings = new ArrayList<>(columnList.size());
|
||||||
//TODO
|
columnList.forEach(i -> {
|
||||||
return new ResultMap.Builder(ms.getConfiguration(), id, resultType, EMPTY_RESULT_MAPPING).build();
|
TableFieldInfo info = i.getTableFieldInfo();
|
||||||
} else {
|
if (StringUtils.isNotBlank(i.getAlias())) {
|
||||||
List<SelectColumn> columnList = wrapper.getSelectColumns();
|
//优先别名查询 selectFunc selectAs
|
||||||
List<ResultMapping> resultMappings = new ArrayList<>(columnList.size());
|
ResultMapping.Builder builder = new ResultMapping.Builder(ms.getConfiguration(), i.getAlias(),
|
||||||
columnList.forEach(i -> {
|
i.getAlias(), getAliasField(resultType, fieldMap, i.getAlias()));
|
||||||
TableFieldInfo info = i.getTableFieldInfo();
|
if (i.getFuncEnum() == null || StringUtils.isBlank(i.getFuncEnum().getSql())) {
|
||||||
if (StringUtils.isNotBlank(i.getAlias())) {
|
if (info != null && info.getTypeHandler() != null && info.getTypeHandler() != UnknownTypeHandler.class) {
|
||||||
//优先别名查询 selectFunc selectAs
|
Field f = fieldMap.get(i.getAlias());
|
||||||
ResultMapping.Builder builder = new ResultMapping.Builder(ms.getConfiguration(), i.getAlias(),
|
if (f.getType() == info.getField().getType()) {
|
||||||
i.getAlias(), getAliasField(resultType, fieldMap, i.getAlias()));
|
builder.typeHandler(getTypeHandler(ms, info));
|
||||||
if (i.getFuncEnum() == null || StringUtils.isBlank(i.getFuncEnum().getSql())) {
|
}
|
||||||
if (info != null && info.getTypeHandler() != null && info.getTypeHandler() != UnknownTypeHandler.class) {
|
}
|
||||||
Field f = fieldMap.get(i.getAlias());
|
}
|
||||||
if (f.getType() == info.getField().getType()) {
|
resultMappings.add(builder.build());
|
||||||
builder.typeHandler(getTypeHandler(ms, info));
|
} else if (info != null) {
|
||||||
}
|
// select selectAll selectAsClass
|
||||||
|
if (i.getFuncEnum() == null || StringUtils.isBlank(i.getFuncEnum().getSql())) {
|
||||||
|
ResultMapping.Builder builder = new ResultMapping.Builder(ms.getConfiguration(), info.getProperty(),
|
||||||
|
info.getColumn(), info.getPropertyType());
|
||||||
|
if (info.getTypeHandler() != null && info.getTypeHandler() != UnknownTypeHandler.class) {
|
||||||
|
Field f = fieldMap.get(info.getProperty());
|
||||||
|
if (f != null && f.getType() == info.getField().getType()) {
|
||||||
|
builder.typeHandler(getTypeHandler(ms, info));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
resultMappings.add(builder.build());
|
resultMappings.add(builder.build());
|
||||||
} else if (info != null) {
|
|
||||||
// select selectAll selectAsClass
|
|
||||||
if (i.getFuncEnum() == null || StringUtils.isBlank(i.getFuncEnum().getSql())) {
|
|
||||||
ResultMapping.Builder builder = new ResultMapping.Builder(ms.getConfiguration(), info.getProperty(),
|
|
||||||
info.getColumn(), info.getPropertyType());
|
|
||||||
if (info.getTypeHandler() != null && info.getTypeHandler() != UnknownTypeHandler.class) {
|
|
||||||
Field f = fieldMap.get(info.getProperty());
|
|
||||||
if (f != null && f.getType() == info.getField().getType()) {
|
|
||||||
builder.typeHandler(getTypeHandler(ms, info));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
resultMappings.add(builder.build());
|
|
||||||
} else {
|
|
||||||
resultMappings.add(new ResultMapping.Builder(ms.getConfiguration(), info.getProperty(), info.getColumn(), info.getPropertyType()).build());
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// 主键列
|
resultMappings.add(new ResultMapping.Builder(ms.getConfiguration(), info.getProperty(), info.getColumn(), info.getPropertyType()).build());
|
||||||
resultMappings.add(new ResultMapping.Builder(ms.getConfiguration(), i.getTagProperty(), i.getColumnName(),
|
|
||||||
getAliasField(resultType, fieldMap, i.getTagProperty())).build());
|
|
||||||
}
|
}
|
||||||
});
|
} else {
|
||||||
//移除result中不存在的标签
|
// 主键列
|
||||||
resultMappings.removeIf(i -> !fieldMap.containsKey(i.getProperty()));
|
resultMappings.add(new ResultMapping.Builder(ms.getConfiguration(), i.getTagProperty(), i.getColumnName(),
|
||||||
return new ResultMap.Builder(ms.getConfiguration(), id, resultType, resultMappings).build();
|
i.getKeyType()).build());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
//移除result中不存在的标签
|
||||||
|
Set<String> columnSet = resultMappings.stream().map(ResultMapping::getColumn).collect(Collectors.toSet());
|
||||||
|
resultMappings.removeIf(i -> !fieldMap.containsKey(i.getProperty()));
|
||||||
|
if (wrapper.isResultMap()) {
|
||||||
|
//先不考虑 去重 看一下能否实现
|
||||||
|
List<MybatisLabel<?, ?>> mybatisLabel = wrapper.getResultMapMybatisLabel();
|
||||||
|
for (MybatisLabel mpjColl : mybatisLabel) {
|
||||||
|
List<Result> list = mpjColl.getResultList();
|
||||||
|
if (CollectionUtils.isEmpty(list)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
List<ResultMapping> childMapping = new ArrayList<>(list.size());
|
||||||
|
for (Result r : list) {
|
||||||
|
String columnName = r.getColumn();
|
||||||
|
columnName = getColumn(columnSet, columnName);
|
||||||
|
columnList.add(SelectColumn.of(mpjColl.getEntityClass(), r.getColumn(), null,
|
||||||
|
Objects.equals(columnName, r.getColumn()) ? null : columnName, null, null, null));
|
||||||
|
ResultMapping.Builder builder = new ResultMapping.Builder(ms.getConfiguration(), r.getProperty(), columnName, r.getJavaType());
|
||||||
|
if (r.isId()) {
|
||||||
|
builder.flags(Collections.singletonList(ResultFlag.ID));
|
||||||
|
}
|
||||||
|
childMapping.add(builder.build());
|
||||||
|
}
|
||||||
|
String childId = id + StringPool.UNDERSCORE + mpjColl.getEntityClass().getName() + StringPool.UNDERSCORE +
|
||||||
|
mpjColl.getOfType().getName() + StringPool.UNDERSCORE + mpjColl.getLabelType() + StringPool.UNDERSCORE +
|
||||||
|
childMapping.stream().map(i -> (CollectionUtils.isEmpty(i.getFlags()) ? ResultFlag.CONSTRUCTOR :
|
||||||
|
i.getFlags().get(0)) + i.getProperty() + i.getColumn()).collect(Collectors.joining(StringPool.DASH));
|
||||||
|
resultMappings.add(new ResultMapping.Builder(ms.getConfiguration(), mpjColl.getProperty())
|
||||||
|
.javaType(mpjColl.getJavaType())
|
||||||
|
.nestedResultMapId(childId)
|
||||||
|
.build());
|
||||||
|
if (!ms.getConfiguration().getResultMapNames().contains(childId)) {
|
||||||
|
ms.getConfiguration().addResultMap(new ResultMap.Builder(ms.getConfiguration(), childId, mpjColl.getOfType(), childMapping).build());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
result.add(0, new ResultMap.Builder(ms.getConfiguration(), id, resultType, resultMappings).build());
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -222,6 +247,21 @@ public class MPJInterceptor implements Interceptor {
|
|||||||
return typeHandler;
|
return typeHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 列名去重 重复的添加 mpj 前缀 再重复走递归
|
||||||
|
*
|
||||||
|
* @param pool 查询列 集合
|
||||||
|
* @param columnName 列明
|
||||||
|
* @return 唯一列名
|
||||||
|
*/
|
||||||
|
private String getColumn(Set<String> pool, String columnName) {
|
||||||
|
if (!pool.contains(columnName)) {
|
||||||
|
pool.add(columnName);
|
||||||
|
return columnName;
|
||||||
|
}
|
||||||
|
columnName = "mpj_" + StringUtils.getTargetColumn(columnName);
|
||||||
|
return getColumn(pool, columnName);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取非lambda的resultMap
|
* 获取非lambda的resultMap
|
||||||
@ -255,19 +295,4 @@ public class MPJInterceptor implements Interceptor {
|
|||||||
Assert.notNull(field, "Result Class <%s> not find Field <%s>", resultType.getSimpleName(), alias);
|
Assert.notNull(field, "Result Class <%s> not find Field <%s>", resultType.getSimpleName(), alias);
|
||||||
return field.getType();
|
return field.getType();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 打印resultMap
|
|
||||||
* 先打开 mybatis plus 日志, 在设置 printResultMap = true 才会打印
|
|
||||||
*/
|
|
||||||
private void printResultMap(ResultMap resultMap) {
|
|
||||||
if (resultMap == null || !printResultMap)
|
|
||||||
return;
|
|
||||||
logger.debug("===================== MPJ resultMap =========================");
|
|
||||||
List<ResultMapping> mappings = resultMap.getResultMappings();
|
|
||||||
logger.debug(String.format(" <resultMap id=\"%s\" type=\"%s\">", resultMap.getId(), resultMap.getType().getName()));
|
|
||||||
mappings.forEach(i -> logger.debug(String.format(" <result property=\"%s\" column=\"%s\" javaType=\"%s\" typeHandler=\"%s\"/>", i.getProperty(), i.getColumn(), i.getJavaType(), i.getTypeHandler())));
|
|
||||||
logger.debug(" </resultMap>");
|
|
||||||
logger.debug("===================== end =========================");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -92,13 +92,13 @@ public final class LambdaUtils {
|
|||||||
if (info.havePK()) {
|
if (info.havePK()) {
|
||||||
map = CollectionUtils.newHashMapWithExpectedSize(info.getFieldList().size() + 1);
|
map = CollectionUtils.newHashMapWithExpectedSize(info.getFieldList().size() + 1);
|
||||||
map.put(formatKey(info.getKeyProperty()), new ColumnCache(info.getKeyColumn(), info.getKeySqlSelect(),
|
map.put(formatKey(info.getKeyProperty()), new ColumnCache(info.getKeyColumn(), info.getKeySqlSelect(),
|
||||||
null, info.getKeyProperty(), true));
|
null, info.getKeyProperty(), true, info.getKeyType()));
|
||||||
} else {
|
} else {
|
||||||
map = CollectionUtils.newHashMapWithExpectedSize(info.getFieldList().size());
|
map = CollectionUtils.newHashMapWithExpectedSize(info.getFieldList().size());
|
||||||
}
|
}
|
||||||
|
|
||||||
info.getFieldList().forEach(i ->
|
info.getFieldList().forEach(i ->
|
||||||
map.put(formatKey(i.getProperty()), new ColumnCache(i.getColumn(), i.getSqlSelect(), i, null, false))
|
map.put(formatKey(i.getProperty()), new ColumnCache(i.getColumn(), i.getSqlSelect(), i, null, false, null))
|
||||||
);
|
);
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
@ -21,6 +21,9 @@ public class ColumnCache extends com.baomidou.mybatisplus.core.toolkit.support.C
|
|||||||
@Getter
|
@Getter
|
||||||
private boolean isPK;
|
private boolean isPK;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private Class<?> keyType;
|
||||||
|
|
||||||
|
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public ColumnCache(String column, String columnSelect) {
|
public ColumnCache(String column, String columnSelect) {
|
||||||
@ -32,10 +35,11 @@ public class ColumnCache extends com.baomidou.mybatisplus.core.toolkit.support.C
|
|||||||
super(column, columnSelect, mapping);
|
super(column, columnSelect, mapping);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ColumnCache(String column, String columnSelect, TableFieldInfo tableFieldInfo, String keyProperty, boolean isPK) {
|
public ColumnCache(String column, String columnSelect, TableFieldInfo tableFieldInfo, String keyProperty, boolean isPK, Class<?> keyType) {
|
||||||
super(column, columnSelect);
|
super(column, columnSelect);
|
||||||
this.tableFieldInfo = tableFieldInfo;
|
this.tableFieldInfo = tableFieldInfo;
|
||||||
this.keyProperty = keyProperty;
|
this.keyProperty = keyProperty;
|
||||||
this.isPK = isPK;
|
this.isPK = isPK;
|
||||||
|
this.keyType = keyType;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -43,25 +43,31 @@ public class SelectColumn implements UniqueObject {
|
|||||||
*/
|
*/
|
||||||
private final String tagProperty;
|
private final String tagProperty;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 主键类型
|
||||||
|
*/
|
||||||
|
private final Class<?> keyType;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 字段函数
|
* 字段函数
|
||||||
*/
|
*/
|
||||||
private final BaseFuncEnum funcEnum;
|
private final BaseFuncEnum funcEnum;
|
||||||
|
|
||||||
|
|
||||||
private SelectColumn(Class<?> clazz, String columnName, TableFieldInfo tableFieldInfo, String alias, String tagProperty, BaseFuncEnum funcEnum) {
|
private SelectColumn(Class<?> clazz, String columnName, TableFieldInfo tableFieldInfo, String alias, String tagProperty, Class<?> keyType, BaseFuncEnum funcEnum) {
|
||||||
this.clazz = clazz;
|
this.clazz = clazz;
|
||||||
this.columnName = columnName;
|
this.columnName = columnName;
|
||||||
this.tableFieldInfo = tableFieldInfo;
|
this.tableFieldInfo = tableFieldInfo;
|
||||||
this.alias = alias;
|
this.alias = alias;
|
||||||
this.tagProperty = tagProperty;
|
this.tagProperty = tagProperty;
|
||||||
|
this.keyType = keyType;
|
||||||
this.funcEnum = funcEnum;
|
this.funcEnum = funcEnum;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SelectColumn of(Class<?> clazz, String columnName, TableFieldInfo tableFieldInfo, String alias, String tagProperty, BaseFuncEnum funcEnum) {
|
public static SelectColumn of(Class<?> clazz, String columnName, TableFieldInfo tableFieldInfo, String alias, String tagProperty, Class<?> keyType, BaseFuncEnum funcEnum) {
|
||||||
if (tagProperty != null)
|
if (tagProperty != null)
|
||||||
tagProperty = StringUtils.getTargetColumn(tagProperty);
|
tagProperty = StringUtils.getTargetColumn(tagProperty);
|
||||||
return new SelectColumn(clazz, columnName, tableFieldInfo, alias, tagProperty, funcEnum);
|
return new SelectColumn(clazz, columnName, tableFieldInfo, alias, tagProperty, keyType, funcEnum);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -18,12 +18,14 @@ import com.github.yulichang.wrapper.enums.BaseFuncEnum;
|
|||||||
import com.github.yulichang.wrapper.interfaces.LambdaJoin;
|
import com.github.yulichang.wrapper.interfaces.LambdaJoin;
|
||||||
import com.github.yulichang.wrapper.interfaces.Query;
|
import com.github.yulichang.wrapper.interfaces.Query;
|
||||||
import com.github.yulichang.wrapper.interfaces.on.OnFunction;
|
import com.github.yulichang.wrapper.interfaces.on.OnFunction;
|
||||||
import com.github.yulichang.wrapper.resultmap.Collection;
|
import com.github.yulichang.wrapper.resultmap.LabelType;
|
||||||
import com.github.yulichang.wrapper.resultmap.MFunc;
|
import com.github.yulichang.wrapper.resultmap.MFunc;
|
||||||
|
import com.github.yulichang.wrapper.resultmap.MybatisLabel;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
@ -61,10 +63,10 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
|||||||
*/
|
*/
|
||||||
private final List<MPJLambdaWrapper<?>> onWrappers = new ArrayList<>();
|
private final List<MPJLambdaWrapper<?>> onWrappers = new ArrayList<>();
|
||||||
/**
|
/**
|
||||||
* 敌对多映射关系
|
* 映射关系
|
||||||
*/
|
*/
|
||||||
@Getter
|
@Getter
|
||||||
private final List<Collection<?, ?>> resultMapCollection = new ArrayList<>();
|
private final List<MybatisLabel<?, ?>> resultMapMybatisLabel = new ArrayList<>();
|
||||||
/**
|
/**
|
||||||
* 是否构建是否存在一对多
|
* 是否构建是否存在一对多
|
||||||
*/
|
*/
|
||||||
@ -140,13 +142,35 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
|||||||
for (SFunction<S, ?> s : columns) {
|
for (SFunction<S, ?> s : columns) {
|
||||||
ColumnCache cache = getCache(s);
|
ColumnCache cache = getCache(s);
|
||||||
selectColumns.add(SelectColumn.of(LambdaUtils.getEntityClass(s), cache.getColumn(), cache.getTableFieldInfo(),
|
selectColumns.add(SelectColumn.of(LambdaUtils.getEntityClass(s), cache.getColumn(), cache.getTableFieldInfo(),
|
||||||
null, cache.getTableFieldInfo() == null ? cache.getKeyProperty() : cache.getTableFieldInfo().getProperty(), null));
|
null, cache.getTableFieldInfo() == null ? cache.getKeyProperty() : cache.getTableFieldInfo().getProperty(), cache.getKeyType(), null));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return typedThis;
|
return typedThis;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO 重载重复代码简化
|
/**
|
||||||
|
* 一对多查询 调用此方法发必需要调用对应的 left join / right join ... 连表方法,否则会报错
|
||||||
|
* <p>
|
||||||
|
* 举例 UserDO UserAddressDO 为一对多关系 UserDTO 为结果类
|
||||||
|
* <pre>
|
||||||
|
* MPJLambdaQueryWrapper<UserDO> wrapper = new MPJLambdaQueryWrapper();
|
||||||
|
* wrapper.selectAll(UserDO.class)
|
||||||
|
* .selectCollection(UserAddressDO.class, UserDTO::getAddressListDTO)
|
||||||
|
* .leftJoin(UserAddressDO.class, ...... )
|
||||||
|
* .eq(...)
|
||||||
|
* ...
|
||||||
|
* <pre/>
|
||||||
|
* 会自动将 UserAddressDO类中相同属性的字段 以mybatis<collection>的方式映射到UserDTO.addressListDTO属性中
|
||||||
|
*
|
||||||
|
* @since 1.2.5
|
||||||
|
*
|
||||||
|
* @param child 连表数据库实体类
|
||||||
|
* @param dtoField 包装类对应的属性
|
||||||
|
* @param <S> 包装类
|
||||||
|
* @param <C> 对多数据库实体类
|
||||||
|
* @param <Z> 包装类集合泛型
|
||||||
|
* @param <F> 包装类集合字段泛型
|
||||||
|
*/
|
||||||
public <S, C, Z, F extends java.util.Collection<?>> MPJLambdaWrapper<T> selectCollection(Class<C> child, SFunction<S, F> dtoField) {
|
public <S, C, Z, F extends java.util.Collection<?>> MPJLambdaWrapper<T> selectCollection(Class<C> child, SFunction<S, F> dtoField) {
|
||||||
String dtoFieldName = LambdaUtils.getName(dtoField);
|
String dtoFieldName = LambdaUtils.getName(dtoField);
|
||||||
Class<S> dtoClass = LambdaUtils.getEntityClass(dtoField);
|
Class<S> dtoClass = LambdaUtils.getEntityClass(dtoField);
|
||||||
@ -154,23 +178,51 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
|||||||
Field field = fieldMap.get(dtoFieldName);
|
Field field = fieldMap.get(dtoFieldName);
|
||||||
this.resultMap = true;
|
this.resultMap = true;
|
||||||
Class<?> genericType = ReflectionKit.getGenericType(field);
|
Class<?> genericType = ReflectionKit.getGenericType(field);
|
||||||
Collection.Builder<C, Z> builder;
|
MybatisLabel.Builder<C, Z> builder;
|
||||||
if (genericType == null || genericType.isAssignableFrom(child)) {
|
if (genericType == null || genericType.isAssignableFrom(child)) {
|
||||||
//找不到集合泛型 List List<?> List<Object> , 直接查询数据库实体
|
//找不到集合泛型 List List<?> List<Object> , 直接查询数据库实体
|
||||||
builder = new Collection.Builder<>(dtoFieldName, child, field.getType());
|
builder = new MybatisLabel.Builder<>(LabelType.COLLECTION, dtoFieldName, child, field.getType());
|
||||||
} else {
|
} else {
|
||||||
Class<Z> ofType = (Class<Z>) genericType;
|
Class<Z> ofType = (Class<Z>) genericType;
|
||||||
if (ReflectionKit.isPrimitiveOrWrapper(ofType)) {
|
if (ReflectionKit.isPrimitiveOrWrapper(ofType)) {
|
||||||
throw new MPJException("collection 不支持基本数据类型");
|
throw new MPJException("collection 不支持基本数据类型");
|
||||||
}
|
}
|
||||||
builder = new Collection.Builder<>(dtoFieldName, child, field.getType(), ofType, true);
|
builder = new MybatisLabel.Builder<>(LabelType.COLLECTION, dtoFieldName, child, field.getType(), ofType, true);
|
||||||
}
|
}
|
||||||
this.resultMapCollection.add(builder.build());
|
this.resultMapMybatisLabel.add(builder.build());
|
||||||
return typedThis;
|
return typedThis;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 一对多查询 调用此方法发必需要调用对应的 left join / right join ... 连表方法,否则会报错
|
||||||
|
* <p>
|
||||||
|
* 举例 UserDO UserAddressDO 为一对多关系 UserDTO 为结果类
|
||||||
|
* <pre>
|
||||||
|
* MPJLambdaQueryWrapper<UserDO> wrapper = new MPJLambdaQueryWrapper();
|
||||||
|
* wrapper.selectAll(UserDO.class)
|
||||||
|
* .selectCollection(UserAddressDO.class, UserDTO::getAddressListDTO, map -> map
|
||||||
|
* .id(UserAddressDO::getId, AddressDTO::getId) //如果属性名一致 可以传一个
|
||||||
|
* .result(UserAddressDO::getUserId) //如果属性名一致 可以传一个
|
||||||
|
* .result(UserAddressDO::getAddress, AddressDTO::getAddress))) //如果属性名一致 可以传一个
|
||||||
|
* .leftJoin(UserAddressDO.class, ...... )
|
||||||
|
* .eq(...)
|
||||||
|
* ...
|
||||||
|
* <pre/>
|
||||||
|
*
|
||||||
|
* 会自动将 UserAddressDO类中指定的字段 以mybatis<collection>的方式映射到UserDTO.addressListDTO属性中
|
||||||
|
*
|
||||||
|
* @since 1.2.5
|
||||||
|
*
|
||||||
|
* @param child 连表数据库实体类
|
||||||
|
* @param dtoField 包装类对应的属性
|
||||||
|
* @param collection collection标签内容
|
||||||
|
* @param <S> 包装类
|
||||||
|
* @param <C> 对多数据库实体类
|
||||||
|
* @param <Z> 包装类集合泛型
|
||||||
|
* @param <F> 包装类集合字段泛型
|
||||||
|
*/
|
||||||
public <S, C, Z, F extends java.util.Collection<Z>> MPJLambdaWrapper<T>
|
public <S, C, Z, F extends java.util.Collection<Z>> MPJLambdaWrapper<T>
|
||||||
selectCollection(Class<C> child, SFunction<S, F> dtoField, MFunc<Collection.Builder<C, Z>> collection) {
|
selectCollection(Class<C> child, SFunction<S, F> dtoField, MFunc<MybatisLabel.Builder<C, Z>> collection) {
|
||||||
String dtoFieldName = LambdaUtils.getName(dtoField);
|
String dtoFieldName = LambdaUtils.getName(dtoField);
|
||||||
Class<S> dtoClass = LambdaUtils.getEntityClass(dtoField);
|
Class<S> dtoClass = LambdaUtils.getEntityClass(dtoField);
|
||||||
Field field = ReflectionKit.getFieldMap(dtoClass).get(dtoFieldName);
|
Field field = ReflectionKit.getFieldMap(dtoClass).get(dtoFieldName);
|
||||||
@ -178,13 +230,49 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
|||||||
//获取集合泛型
|
//获取集合泛型
|
||||||
Class<?> genericType = ReflectionKit.getGenericType(field);
|
Class<?> genericType = ReflectionKit.getGenericType(field);
|
||||||
Class<Z> ofType = (Class<Z>) genericType;
|
Class<Z> ofType = (Class<Z>) genericType;
|
||||||
Collection.Builder<C, Z> builder = new Collection.Builder<>(dtoFieldName, child, field.getType(), ofType, false);
|
MybatisLabel.Builder<C, Z> builder = new MybatisLabel.Builder<>(LabelType.COLLECTION, dtoFieldName, child, field.getType(), ofType, false);
|
||||||
this.resultMapCollection.add(collection.apply(builder).build());
|
this.resultMapMybatisLabel.add(collection.apply(builder).build());
|
||||||
return typedThis;
|
return typedThis;
|
||||||
}
|
}
|
||||||
|
|
||||||
public <S, C, Z, F> MPJLambdaWrapper<T> selectAssociation(Class<C> child, SFunction<S, F> dtoField, MFunc<Collection.Builder<C, F>> collection) {
|
/**
|
||||||
//TODO
|
* 对一查询 用法参考 selectCollection
|
||||||
|
*
|
||||||
|
* @since 1.2.5
|
||||||
|
*/
|
||||||
|
public <S, C, F> MPJLambdaWrapper<T> selectAssociation(Class<C> child, SFunction<S, F> dtoField) {
|
||||||
|
String dtoFieldName = LambdaUtils.getName(dtoField);
|
||||||
|
Class<S> dtoClass = LambdaUtils.getEntityClass(dtoField);
|
||||||
|
Map<String, Field> fieldMap = ReflectionKit.getFieldMap(dtoClass);
|
||||||
|
Field field = fieldMap.get(dtoFieldName);
|
||||||
|
Assert.isFalse(Collection.class.isAssignableFrom(field.getType()), "association 不支持集合类");
|
||||||
|
if (ReflectionKit.isPrimitiveOrWrapper(field.getType())) {
|
||||||
|
throw new MPJException("association 不支持基本数据类型");
|
||||||
|
}
|
||||||
|
this.resultMap = true;
|
||||||
|
MybatisLabel.Builder<C, F> builder;
|
||||||
|
builder = new MybatisLabel.Builder<>(LabelType.ASSOCIATION, dtoFieldName, child, field.getType(), (Class<F>) field.getType(), true);
|
||||||
|
this.resultMapMybatisLabel.add(builder.build());
|
||||||
|
return typedThis;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对一查询 用法参考 selectCollection
|
||||||
|
*
|
||||||
|
* @since 1.2.5
|
||||||
|
*/
|
||||||
|
public <S, C, F> MPJLambdaWrapper<T> selectAssociation(Class<C> child, SFunction<S, F> dtoField,
|
||||||
|
MFunc<MybatisLabel.Builder<C, F>> collection) {
|
||||||
|
String dtoFieldName = LambdaUtils.getName(dtoField);
|
||||||
|
Class<S> dtoClass = LambdaUtils.getEntityClass(dtoField);
|
||||||
|
Field field = ReflectionKit.getFieldMap(dtoClass).get(dtoFieldName);
|
||||||
|
this.resultMap = true;
|
||||||
|
Assert.isFalse(Collection.class.isAssignableFrom(field.getType()), "association 不支持集合类");
|
||||||
|
if (ReflectionKit.isPrimitiveOrWrapper(field.getType())) {
|
||||||
|
throw new MPJException("association 不支持基本数据类型");
|
||||||
|
}
|
||||||
|
MybatisLabel.Builder<C, F> builder = new MybatisLabel.Builder<>(LabelType.ASSOCIATION, dtoFieldName, child, field.getType(), (Class<F>) child, false);
|
||||||
|
this.resultMapMybatisLabel.add(collection.apply(builder).build());
|
||||||
return typedThis;
|
return typedThis;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -194,7 +282,7 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
|||||||
TableInfo info = TableInfoHelper.getTableInfo(entityClass);
|
TableInfo info = TableInfoHelper.getTableInfo(entityClass);
|
||||||
Assert.notNull(info, "table not find by class <%s>", entityClass.getName());
|
Assert.notNull(info, "table not find by class <%s>", entityClass.getName());
|
||||||
info.getFieldList().stream().filter(predicate).collect(Collectors.toList()).forEach(
|
info.getFieldList().stream().filter(predicate).collect(Collectors.toList()).forEach(
|
||||||
i -> selectColumns.add(SelectColumn.of(entityClass, i.getColumn(), i, null, i.getProperty(), null)));
|
i -> selectColumns.add(SelectColumn.of(entityClass, i.getColumn(), i, null, i.getProperty(), null, null)));
|
||||||
return typedThis;
|
return typedThis;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -205,11 +293,12 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
|||||||
List<Field> tagFields = ReflectionKit.getFieldList(tag);
|
List<Field> tagFields = ReflectionKit.getFieldList(tag);
|
||||||
tableInfo.getFieldList().forEach(i -> {
|
tableInfo.getFieldList().forEach(i -> {
|
||||||
if (tagFields.stream().anyMatch(f -> f.getName().equals(i.getProperty()))) {
|
if (tagFields.stream().anyMatch(f -> f.getName().equals(i.getProperty()))) {
|
||||||
selectColumns.add(SelectColumn.of(source, i.getColumn(), i, null, i.getProperty(), null));
|
selectColumns.add(SelectColumn.of(source, i.getColumn(), i, null, i.getProperty(), null, null));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if (tableInfo.havePK() && tagFields.stream().anyMatch(i -> i.getName().equals(tableInfo.getKeyProperty()))) {
|
if (tableInfo.havePK() && tagFields.stream().anyMatch(i -> i.getName().equals(tableInfo.getKeyProperty()))) {
|
||||||
selectColumns.add(SelectColumn.of(source, tableInfo.getKeyColumn(), null, null, tableInfo.getKeyProperty(), null));
|
selectColumns.add(SelectColumn.of(source, tableInfo.getKeyColumn(), null, null,
|
||||||
|
tableInfo.getKeyProperty(), tableInfo.getKeyType(), null));
|
||||||
}
|
}
|
||||||
return typedThis;
|
return typedThis;
|
||||||
}
|
}
|
||||||
@ -217,14 +306,16 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
|||||||
@Override
|
@Override
|
||||||
public <S> MPJLambdaWrapper<T> selectAs(SFunction<S, ?> column, String alias) {
|
public <S> MPJLambdaWrapper<T> selectAs(SFunction<S, ?> column, String alias) {
|
||||||
ColumnCache cache = getCache(column);
|
ColumnCache cache = getCache(column);
|
||||||
selectColumns.add(SelectColumn.of(LambdaUtils.getEntityClass(column), cache.getColumn(), cache.getTableFieldInfo(), alias, null, null));
|
selectColumns.add(SelectColumn.of(LambdaUtils.getEntityClass(column), cache.getColumn(), cache.getTableFieldInfo(),
|
||||||
|
alias, null, cache.getKeyType(), null));
|
||||||
return typedThis;
|
return typedThis;
|
||||||
}
|
}
|
||||||
|
|
||||||
public <S> MPJLambdaWrapper<T> selectFunc(boolean condition, BaseFuncEnum funcEnum, SFunction<S, ?> column, String alias) {
|
public <S> MPJLambdaWrapper<T> selectFunc(boolean condition, BaseFuncEnum funcEnum, SFunction<S, ?> column, String alias) {
|
||||||
if (condition) {
|
if (condition) {
|
||||||
ColumnCache cache = getCache(column);
|
ColumnCache cache = getCache(column);
|
||||||
selectColumns.add(SelectColumn.of(LambdaUtils.getEntityClass(column), cache.getColumn(), cache.getTableFieldInfo(), alias, alias, funcEnum));
|
selectColumns.add(SelectColumn.of(LambdaUtils.getEntityClass(column), cache.getColumn(),
|
||||||
|
cache.getTableFieldInfo(), alias, alias, cache.getKeyType(), funcEnum));
|
||||||
}
|
}
|
||||||
return typedThis;
|
return typedThis;
|
||||||
}
|
}
|
||||||
@ -232,7 +323,7 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
|||||||
@Override
|
@Override
|
||||||
public MPJLambdaWrapper<T> selectFunc(boolean condition, BaseFuncEnum funcEnum, Object column, String alias) {
|
public MPJLambdaWrapper<T> selectFunc(boolean condition, BaseFuncEnum funcEnum, Object column, String alias) {
|
||||||
if (condition) {
|
if (condition) {
|
||||||
selectColumns.add(SelectColumn.of(null, column.toString(), null, alias, alias, funcEnum));
|
selectColumns.add(SelectColumn.of(null, column.toString(), null, alias, alias, null, funcEnum));
|
||||||
}
|
}
|
||||||
return typedThis;
|
return typedThis;
|
||||||
}
|
}
|
||||||
@ -241,10 +332,11 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
|||||||
TableInfo info = TableInfoHelper.getTableInfo(clazz);
|
TableInfo info = TableInfoHelper.getTableInfo(clazz);
|
||||||
Assert.notNull(info, "table can not be find -> %s", clazz);
|
Assert.notNull(info, "table can not be find -> %s", clazz);
|
||||||
if (info.havePK()) {
|
if (info.havePK()) {
|
||||||
selectColumns.add(SelectColumn.of(clazz, info.getKeyColumn(), null, null, info.getKeyProperty(), null));
|
selectColumns.add(SelectColumn.of(clazz, info.getKeyColumn(), null, null,
|
||||||
|
info.getKeyProperty(), info.getKeyType(), null));
|
||||||
}
|
}
|
||||||
info.getFieldList().forEach(c ->
|
info.getFieldList().forEach(c ->
|
||||||
selectColumns.add(SelectColumn.of(clazz, c.getColumn(), c, null, c.getProperty(), null)));
|
selectColumns.add(SelectColumn.of(clazz, c.getColumn(), c, null, c.getProperty(), null, null)));
|
||||||
return typedThis;
|
return typedThis;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,5 @@
|
|||||||
|
package com.github.yulichang.wrapper.resultmap;
|
||||||
|
|
||||||
|
public enum LabelType {
|
||||||
|
COLLECTION, ASSOCIATION
|
||||||
|
}
|
@ -3,6 +3,7 @@ package com.github.yulichang.wrapper.resultmap;
|
|||||||
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
|
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
|
||||||
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
||||||
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||||
import com.github.yulichang.toolkit.ReflectionKit;
|
import com.github.yulichang.toolkit.ReflectionKit;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
|
||||||
@ -20,7 +21,9 @@ import java.util.stream.Collectors;
|
|||||||
* @since 1.2.5
|
* @since 1.2.5
|
||||||
*/
|
*/
|
||||||
@Getter
|
@Getter
|
||||||
public class Collection<E, T> {
|
public class MybatisLabel<E, T> {
|
||||||
|
|
||||||
|
private LabelType labelType;
|
||||||
|
|
||||||
private String property;
|
private String property;
|
||||||
|
|
||||||
@ -33,26 +36,28 @@ public class Collection<E, T> {
|
|||||||
private List<Result> resultList;
|
private List<Result> resultList;
|
||||||
|
|
||||||
//collection嵌套
|
//collection嵌套
|
||||||
// private List<Collection> collectionList;
|
// private List<Collection> collectionList;
|
||||||
|
|
||||||
private Collection() {
|
private MybatisLabel() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
public static class Builder<E, T> {
|
public static class Builder<E, T> {
|
||||||
|
|
||||||
private final Collection<E, T> collection;
|
private final MybatisLabel<E, T> mybatisLabel;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 自动构建
|
* 自动构建
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public Builder(String property, Class<E> entityClass, Class<?> javaType) {
|
public Builder(LabelType labelType, String property, Class<E> entityClass, Class<?> javaType) {
|
||||||
this.collection = new Collection<>();
|
this.mybatisLabel = new MybatisLabel<>();
|
||||||
collection.property = property;
|
mybatisLabel.labelType = labelType;
|
||||||
collection.entityClass = entityClass;
|
mybatisLabel.property = property;
|
||||||
collection.javaType = javaType;
|
mybatisLabel.entityClass = entityClass;
|
||||||
collection.ofType = (Class<T>) entityClass;
|
mybatisLabel.javaType = javaType;
|
||||||
collection.resultList = new ArrayList<>();
|
mybatisLabel.ofType = (Class<T>) entityClass;
|
||||||
|
mybatisLabel.resultList = new ArrayList<>();
|
||||||
autoBuild(true, entityClass, (Class<T>) entityClass);
|
autoBuild(true, entityClass, (Class<T>) entityClass);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,30 +70,47 @@ public class Collection<E, T> {
|
|||||||
* @param ofType 映射类
|
* @param ofType 映射类
|
||||||
* @param auto 自动映射数据库实体对应的字段
|
* @param auto 自动映射数据库实体对应的字段
|
||||||
*/
|
*/
|
||||||
public Builder(String property, Class<E> entityClass, Class<?> javaType, Class<T> ofType, boolean auto) {
|
public Builder(LabelType labelType, String property, Class<E> entityClass, Class<?> javaType, Class<T> ofType, boolean auto) {
|
||||||
this.collection = new Collection<>();
|
this.mybatisLabel = new MybatisLabel<>();
|
||||||
collection.property = property;
|
mybatisLabel.labelType = labelType;
|
||||||
collection.entityClass = entityClass;
|
mybatisLabel.property = property;
|
||||||
collection.javaType = javaType;
|
mybatisLabel.entityClass = entityClass;
|
||||||
collection.ofType = ofType;
|
mybatisLabel.javaType = javaType;
|
||||||
collection.resultList = new ArrayList<>();
|
mybatisLabel.ofType = ofType;
|
||||||
|
mybatisLabel.resultList = new ArrayList<>();
|
||||||
autoBuild(auto, entityClass, ofType);
|
autoBuild(auto, entityClass, ofType);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Builder<E, T> id(MFunc<Result.Builder<E, T>> result) {
|
public Builder<E, T> id(SFunction<E, ?> entity, SFunction<T, ?> tag) {
|
||||||
Result r = result.apply(new Result.Builder<>(true)).build();
|
Result.Builder<E, T> builder = new Result.Builder<>(true);
|
||||||
collection.resultList.add(r);
|
builder.column(entity).property(tag);
|
||||||
|
mybatisLabel.resultList.add(builder.build());
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Builder<E, T> result(MFunc<Result.Builder<E, T>> result) {
|
public Builder<E, T> id(SFunction<E, ?> entity) {
|
||||||
Result r = result.apply(new Result.Builder<>(false)).build();
|
Result.Builder<E, T> builder = new Result.Builder<>(true);
|
||||||
collection.resultList.add(r);
|
builder.column(entity);
|
||||||
|
mybatisLabel.resultList.add(builder.build());
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Collection<E, T> build() {
|
public Builder<E, T> result(SFunction<E, ?> entity, SFunction<T, ?> tag) {
|
||||||
return collection;
|
Result.Builder<E, T> builder = new Result.Builder<>(false);
|
||||||
|
builder.column(entity).property(tag);
|
||||||
|
mybatisLabel.resultList.add(builder.build());
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder<E, T> result(SFunction<E, ?> entity) {
|
||||||
|
Result.Builder<E, T> builder = new Result.Builder<>(false);
|
||||||
|
builder.column(entity);
|
||||||
|
mybatisLabel.resultList.add(builder.build());
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public MybatisLabel<E, T> build() {
|
||||||
|
return mybatisLabel;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void autoBuild(boolean auto, Class<E> entityClass, Class<T> tagClass) {
|
private void autoBuild(boolean auto, Class<E> entityClass, Class<T> tagClass) {
|
||||||
@ -107,16 +129,15 @@ public class Collection<E, T> {
|
|||||||
};
|
};
|
||||||
if (entityClass == tagClass) {
|
if (entityClass == tagClass) {
|
||||||
if (tableInfo.havePK()) {
|
if (tableInfo.havePK()) {
|
||||||
collection.resultList.add(pkBuild(tableInfo));
|
mybatisLabel.resultList.add(pkBuild(tableInfo));
|
||||||
}
|
}
|
||||||
collection.resultList.addAll(tableInfo.getFieldList().stream().map(build).collect(Collectors.toList()));
|
mybatisLabel.resultList.addAll(tableInfo.getFieldList().stream().map(build).collect(Collectors.toList()));
|
||||||
} else {
|
} else {
|
||||||
if (tableInfo.havePK() && tagMap.containsKey(tableInfo.getKeyProperty())) {
|
if (tableInfo.havePK() && tagMap.containsKey(tableInfo.getKeyProperty())) {
|
||||||
collection.resultList.add(pkBuild(tableInfo));
|
mybatisLabel.resultList.add(pkBuild(tableInfo));
|
||||||
} else {
|
|
||||||
collection.resultList.addAll(tableInfo.getFieldList().stream().filter(i ->
|
|
||||||
tagMap.containsKey(i.getProperty())).map(build).collect(Collectors.toList()));
|
|
||||||
}
|
}
|
||||||
|
mybatisLabel.resultList.addAll(tableInfo.getFieldList().stream().filter(i ->
|
||||||
|
tagMap.containsKey(i.getProperty())).map(build).collect(Collectors.toList()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -4,8 +4,11 @@ import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
|
|||||||
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
||||||
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.Assert;
|
import com.baomidou.mybatisplus.core.toolkit.Assert;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||||
import com.github.yulichang.toolkit.LambdaUtils;
|
import com.github.yulichang.toolkit.LambdaUtils;
|
||||||
|
import com.github.yulichang.toolkit.UniqueObject;
|
||||||
import lombok.AccessLevel;
|
import lombok.AccessLevel;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
@ -20,7 +23,7 @@ import org.apache.ibatis.type.TypeHandler;
|
|||||||
*/
|
*/
|
||||||
@Getter
|
@Getter
|
||||||
@Setter(AccessLevel.PACKAGE)
|
@Setter(AccessLevel.PACKAGE)
|
||||||
public class Result {
|
public class Result implements UniqueObject {
|
||||||
|
|
||||||
private boolean isId;
|
private boolean isId;
|
||||||
|
|
||||||
@ -39,6 +42,12 @@ public class Result {
|
|||||||
public Result() {
|
public Result() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getUniqueKey() {
|
||||||
|
return String.join(StringPool.DASH, String.valueOf(isId), property, column);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings({"UnusedReturnValue", "unused"})
|
||||||
public static class Builder<E, T> {
|
public static class Builder<E, T> {
|
||||||
|
|
||||||
private final Result result;
|
private final Result result;
|
||||||
@ -60,11 +69,21 @@ public class Result {
|
|||||||
Assert.notNull(tableInfo, "table not find by class <%s>", tableInfo);
|
Assert.notNull(tableInfo, "table not find by class <%s>", tableInfo);
|
||||||
if (tableInfo.havePK() && tableInfo.getKeyProperty().equals(name)) {
|
if (tableInfo.havePK() && tableInfo.getKeyProperty().equals(name)) {
|
||||||
result.column = tableInfo.getKeyColumn();
|
result.column = tableInfo.getKeyColumn();
|
||||||
|
if (StringUtils.isBlank(result.property)) {
|
||||||
|
result.property = tableInfo.getKeyProperty();
|
||||||
|
}
|
||||||
|
result.javaType = tableInfo.getKeyType();
|
||||||
} else {
|
} else {
|
||||||
TableFieldInfo fieldInfo = tableInfo.getFieldList().stream().filter(i -> i.getField().getName().equals(name)).findFirst().orElse(null);
|
TableFieldInfo fieldInfo = tableInfo.getFieldList().stream().filter(i -> i.getField().getName().equals(name)).findFirst().orElse(null);
|
||||||
Assert.notNull(fieldInfo, "table <%s> not find column <%>", tableInfo.getTableName(), name);
|
Assert.notNull(fieldInfo, "table <%s> not find column <%>", tableInfo.getTableName(), name);
|
||||||
result.column = fieldInfo.getColumn();
|
result.column = fieldInfo.getColumn();
|
||||||
result.tableFieldInfo = fieldInfo;
|
result.tableFieldInfo = fieldInfo;
|
||||||
|
if (StringUtils.isBlank(result.property)) {
|
||||||
|
result.property = fieldInfo.getProperty();
|
||||||
|
}
|
||||||
|
result.jdbcType = fieldInfo.getJdbcType();
|
||||||
|
result.javaType = fieldInfo.getField().getType();
|
||||||
|
result.typeHandle = fieldInfo.getTypeHandler();
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@ -80,7 +99,6 @@ public class Result {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Result build() {
|
public Result build() {
|
||||||
//TODO 检查数据完整性
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user