映射过滤

This commit is contained in:
yulichang 2023-04-11 18:11:21 +08:00
parent 5b1031b2e1
commit 59a85f6414
3 changed files with 57 additions and 2 deletions

View File

@ -14,6 +14,7 @@ import com.github.yulichang.wrapper.segments.SelectCache;
import lombok.Getter;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
@ -87,6 +88,25 @@ public class MybatisLabel<E, T> implements Label<T> {
autoBuild(auto, entityClass, ofType);
}
/**
* 映射实体全部字段
*/
public Builder<E, T> all() {
autoBuild(true, mybatisLabel.entityClass, mybatisLabel.ofType);
return this;
}
/**
* 映射实体字段过滤(含主键)
*/
public Builder<E, T> filter(Predicate<SelectCache> predicate) {
Map<String, FieldCache> fieldMap = MPJReflectionKit.getFieldMap(mybatisLabel.ofType);
ColumnCache.getListField(mybatisLabel.entityClass).stream().filter(predicate)
.filter(p -> fieldMap.containsKey(p.getColumProperty())).forEach(c ->
mybatisLabel.resultList.add(new Result.Builder<T>(c.isPk(), mybatisLabel.index, c).build()));
return this;
}
public Builder<E, T> id(SFunction<E, ?> entity, SFunction<T, ?> tag) {
Result.Builder<T> builder = new Result.Builder<>(true, mybatisLabel.index);
builder.column(entity).property(tag);

View File

@ -13,6 +13,7 @@ import com.github.yulichang.wrapper.segments.SelectCache;
import lombok.Getter;
import java.util.*;
import java.util.function.Predicate;
/**
* 无泛型约束 实现自由映射
@ -60,16 +61,38 @@ public class MybatisLabelFree<T> implements Label<T> {
mybatisLabel.mybatisLabels = new ArrayList<>();
}
public <E> Builder<T> all(Class<?> entityClass) {
public <E> Builder<T> all(Class<E> entityClass) {
allBuild(null, entityClass);
return this;
}
public <E> Builder<T> all(String prefix, Class<?> entityClass) {
public <E> Builder<T> all(String prefix, Class<E> entityClass) {
allBuild(prefix, entityClass);
return this;
}
/**
* 映射实体字段过滤(含主键)
*/
public <E> Builder<T> filter(Class<E> entityClass, Predicate<SelectCache> predicate) {
Map<String, FieldCache> fieldMap = MPJReflectionKit.getFieldMap(mybatisLabel.ofType);
ColumnCache.getListField(entityClass).stream().filter(predicate)
.filter(p -> fieldMap.containsKey(p.getColumProperty())).forEach(c ->
mybatisLabel.resultList.add(new Result.Builder<T>(false, null, c).build()));
return this;
}
/**
* 映射实体字段过滤(含主键)
*/
public <E> Builder<T> filter(String prefix, Class<E> entityClass, Predicate<SelectCache> predicate) {
Map<String, FieldCache> fieldMap = MPJReflectionKit.getFieldMap(mybatisLabel.ofType);
ColumnCache.getListField(entityClass).stream().filter(predicate)
.filter(p -> fieldMap.containsKey(p.getColumProperty())).forEach(c ->
mybatisLabel.resultList.add(new Result.Builder<T>(false, prefix, c).build()));
return this;
}
public <E> Builder<T> id(SFunction<E, ?> entity, SFunction<T, ?> tag) {
Result.Builder<T> builder = new Result.Builder<>(true, null);
builder.column(entity).property(tag);

View File

@ -1,5 +1,6 @@
package com.github.yulichang.wrapper.resultmap;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.github.yulichang.toolkit.LambdaUtils;
@ -12,6 +13,7 @@ import org.apache.ibatis.type.JdbcType;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
/**
* result 标签
@ -50,6 +52,16 @@ public class Result {
result.index = index;
}
public Builder(boolean isId, String index, SelectCache selectCache) {
this.result = new Result();
result.isId = isId;
result.index = index;
result.selectNormal = selectCache;
result.property = selectCache.getColumProperty();
result.javaType = selectCache.getColumnType();
result.jdbcType = Optional.ofNullable(selectCache.getTableFieldInfo()).map(TableFieldInfo::getJdbcType).orElse(null);
}
public Builder<T> property(SFunction<T, ?> property) {
result.property = LambdaUtils.getName(property);
return this;