This commit is contained in:
yulichang 2023-02-23 14:41:16 +08:00
parent bfab4f289e
commit bb5c5df261
3 changed files with 22 additions and 17 deletions

View File

@ -7,25 +7,34 @@ import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction; import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.github.yulichang.relation.Relation; import com.github.yulichang.relation.Relation;
import java.util.Arrays; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.function.Function; import java.util.function.Function;
/**
* 注解映射Mapper 用于替代 MPJDeepMapper
*
* @author yulichang
* @since 1.4.3
*/
public interface MPJRelationMapper<T> extends BaseMapper<T> { public interface MPJRelationMapper<T> extends BaseMapper<T> {
default <R, M extends BaseMapper<T>> R selectRelation(Function<M, R> function) {
return selectRelation(function, new ArrayList<>());
}
/** /**
* 通过注解实现单表多次查询 * 通过注解实现单表多次查询
* *
* @param function BaseMapper调用方法 * @param function BaseMapper调用方法
* @param deep 是否深度查询 * @param list 属性过滤, 可以只查询需要映射的属性
* @param prop 属性过滤, 可以只查询需要映射的属性
* @see com.github.yulichang.annotation.EntityMapping * @see com.github.yulichang.annotation.EntityMapping
* @see com.github.yulichang.annotation.FieldMapping * @see com.github.yulichang.annotation.FieldMapping
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
default <R, M extends BaseMapper<T>> R selectRelation(Function<M, R> function, boolean deep, SFunction<T, ?>... prop) { default <R, M extends BaseMapper<T>> R selectRelation(Function<M, R> function, List<SFunction<T, ?>> list) {
R r = function.apply((M) this); R r = function.apply((M) this);
if (Objects.isNull(r)) { if (Objects.isNull(r)) {
return null; return null;
@ -42,7 +51,7 @@ public interface MPJRelationMapper<T> extends BaseMapper<T> {
if (Object.class == t.getClass()) { if (Object.class == t.getClass()) {
return r; return r;
} }
return (R) Relation.list(data, Arrays.asList(prop), deep); return (R) Relation.list(data, list);
} }
} }
if (r instanceof IPage) { if (r instanceof IPage) {
@ -55,7 +64,7 @@ public interface MPJRelationMapper<T> extends BaseMapper<T> {
if (Object.class == t.getClass()) { if (Object.class == t.getClass()) {
return r; return r;
} }
Relation.list(data.getRecords(), Arrays.asList(prop), deep); Relation.list(data.getRecords(), list);
} }
return r; return r;
} }
@ -72,6 +81,6 @@ public interface MPJRelationMapper<T> extends BaseMapper<T> {
return r; return r;
} }
T data = (T) r; T data = (T) r;
return (R) Relation.one(data, Arrays.asList(prop), deep); return (R) Relation.one(data, list);
} }
} }

View File

@ -16,9 +16,8 @@ import java.util.stream.Collectors;
@SuppressWarnings("all") @SuppressWarnings("all")
public class Relation { public class Relation {
// private static final List EMPTY_LIST = new ArrayList<>();
public static <T> List<T> list(List<T> data, List<SFunction<T, ?>> property, boolean deep) { public static <T> List<T> list(List<T> data, List<SFunction<T, ?>> property) {
if (CollectionUtils.isEmpty(data)) { if (CollectionUtils.isEmpty(data)) {
return data; return data;
} }
@ -38,12 +37,8 @@ public class Relation {
data.forEach(i -> mpjBindData(i, fieldInfo, joinList)); data.forEach(i -> mpjBindData(i, fieldInfo, joinList));
fieldInfo.removeJoinField(joinList); fieldInfo.removeJoinField(joinList);
if (CollectionUtils.isEmpty(joinList)) { if (CollectionUtils.isEmpty(joinList)) {
// continue; continue;
} }
// //深度查询
// if (deep) {
//
// }
} else { } else {
data.forEach(i -> fieldInfo.fieldSet(i, new ArrayList<>())); data.forEach(i -> fieldInfo.fieldSet(i, new ArrayList<>()));
} }
@ -62,7 +57,7 @@ public class Relation {
* *
* @param t 第一次查询结果 * @param t 第一次查询结果
*/ */
public static <T> T one(T t, List<SFunction<T, ?>> property, boolean deep) { public static <T> T one(T t, List<SFunction<T, ?>> property) {
if (t == null) { if (t == null) {
return null; return null;
} }

View File

@ -9,6 +9,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.Collections;
import java.util.List; import java.util.List;
/** /**
@ -23,7 +24,7 @@ class MappingTest {
@Test @Test
public void test() { public void test() {
List<UserDO> dos = userMapper.selectRelation(e -> e.selectList(new QueryWrapper<>()), true); List<UserDO> dos = userMapper.selectRelation(e -> e.selectList(new QueryWrapper<>()), Collections.singletonList(UserDO::getAddressId));
System.out.println(1); System.out.println(1);
} }
@ -32,7 +33,7 @@ class MappingTest {
MPJLambdaWrapper<UserDO> wrapper = new MPJLambdaWrapper<UserDO>() MPJLambdaWrapper<UserDO> wrapper = new MPJLambdaWrapper<UserDO>()
.selectAll(UserDO.class) .selectAll(UserDO.class)
.leftJoin(AddressDO.class, AddressDO::getId, UserDO::getAddressId); .leftJoin(AddressDO.class, AddressDO::getId, UserDO::getAddressId);
List<UserDO> dos = userMapper.selectRelation(e -> e.selectList(wrapper), true); List<UserDO> dos = userMapper.selectRelation(e -> e.selectList(wrapper));
System.out.println(1); System.out.println(1);
} }
} }