From 7d616e0d6c22dc22e26d0ddae03fdb92c1379ffe Mon Sep 17 00:00:00 2001 From: yulichang <570810310@qq.com> Date: Mon, 13 Mar 2023 14:24:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A8=E6=80=81=E8=A1=A8=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../github/yulichang/base/MPJBaseMapper.java | 3 +- .../github/yulichang/base/MPJBaseService.java | 3 +- .../base/mapper/MPJRelationMapper.java | 29 ++++++------ .../base/service/MPJRelationService.java | 46 +++++++++++++++++++ .../test/config/MybatisPlusConfig.java | 17 ++----- .../yulichang/test/util/ThreadLocalUtils.java | 16 ++++++- 6 files changed, 82 insertions(+), 32 deletions(-) create mode 100644 mybatis-plus-join-core/src/main/java/com/github/yulichang/base/service/MPJRelationService.java diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/base/MPJBaseMapper.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/base/MPJBaseMapper.java index e0fbad5..2c5f8a6 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/base/MPJBaseMapper.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/base/MPJBaseMapper.java @@ -1,7 +1,6 @@ package com.github.yulichang.base; import com.baomidou.mybatisplus.core.mapper.BaseMapper; -import com.github.yulichang.base.mapper.MPJDeepMapper; import com.github.yulichang.base.mapper.MPJJoinMapper; import com.github.yulichang.base.mapper.MPJRelationMapper; @@ -9,7 +8,7 @@ import com.github.yulichang.base.mapper.MPJRelationMapper; * @author yulichang * @see BaseMapper */ -public interface MPJBaseMapper extends MPJJoinMapper, MPJRelationMapper, MPJDeepMapper { +public interface MPJBaseMapper extends MPJJoinMapper, MPJRelationMapper { } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/base/MPJBaseService.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/base/MPJBaseService.java index 2cb4648..f459202 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/base/MPJBaseService.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/base/MPJBaseService.java @@ -1,6 +1,5 @@ package com.github.yulichang.base; -import com.github.yulichang.base.service.MPJDeepService; import com.github.yulichang.base.service.MPJJoinService; /** @@ -10,5 +9,5 @@ import com.github.yulichang.base.service.MPJJoinService; * @author yulichang * @see MPJJoinService */ -public interface MPJBaseService extends MPJJoinService, MPJDeepService { +public interface MPJBaseService extends MPJJoinService { } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/base/mapper/MPJRelationMapper.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/base/mapper/MPJRelationMapper.java index b14c257..8d9b5ce 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/base/mapper/MPJRelationMapper.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/base/mapper/MPJRelationMapper.java @@ -21,6 +21,13 @@ import java.util.function.Function; */ public interface MPJRelationMapper extends BaseMapper { + /** + * 通过注解实现单表多次查询 + * + * @param function BaseMapper调用方法 + * @see com.github.yulichang.annotation.EntityMapping + * @see com.github.yulichang.annotation.FieldMapping + */ default > R selectRelation(Function function) { return selectRelation(function, new ArrayList<>()); } @@ -38,8 +45,7 @@ public interface MPJRelationMapper extends BaseMapper { R r = function.apply((M) this); if (Objects.isNull(r)) { return null; - } - if (r instanceof List) { + } else if (r instanceof List) { List data = (List) r; if (CollectionUtils.isEmpty(data)) { return r; @@ -53,8 +59,7 @@ public interface MPJRelationMapper extends BaseMapper { } return (R) Relation.list(data, list); } - } - if (r instanceof IPage) { + } else if (r instanceof IPage) { IPage data = (IPage) r; if (!CollectionUtils.isEmpty(data.getRecords())) { T t = data.getRecords().get(0); @@ -67,20 +72,16 @@ public interface MPJRelationMapper extends BaseMapper { Relation.list(data.getRecords(), list); } return r; - } - if (r instanceof Integer) { + } else if (r instanceof Integer) { return r; - } - if (r instanceof Long) { + } else if (r instanceof Long) { return r; - } - if (r instanceof Boolean) { + } else if (r instanceof Boolean) { return r; - } - if (Object.class == r.getClass()) { + } else if (Object.class == r.getClass()) { return r; + } else { + return (R) Relation.one((T) r, list); } - T data = (T) r; - return (R) Relation.one(data, list); } } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/base/service/MPJRelationService.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/base/service/MPJRelationService.java new file mode 100644 index 0000000..55caeeb --- /dev/null +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/base/service/MPJRelationService.java @@ -0,0 +1,46 @@ +package com.github.yulichang.base.service; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.baomidou.mybatisplus.core.toolkit.support.SFunction; +import com.baomidou.mybatisplus.extension.service.IService; +import com.github.yulichang.annotation.EntityMapping; +import com.github.yulichang.annotation.FieldMapping; +import com.github.yulichang.base.mapper.MPJRelationMapper; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Function; + +/** + * 深度查询 + *

+ * 对配置了映射注解的字段进行查询 + * 目前查询深度只支持2级(只解析当前实体类的映射注解,不会对查询结果再次解析注解) + * 多级查询可能存在循环引用的问题,也可能会导致全量查询 + * 用于替换deep + * + * @author yulichang + * @see EntityMapping + * @see FieldMapping + * @since 1.4.4 + */ +@SuppressWarnings({"unused"}) +public interface MPJRelationService extends IService { + + default > R selectRelation(Function function) { + return selectRelation(function, new ArrayList<>()); + } + + /** + * 通过注解实现单表多次查询 + * + * @param function BaseMapper调用方法 + * @param list 属性过滤, 可以只查询需要映射的属性 + * @see com.github.yulichang.annotation.EntityMapping + * @see com.github.yulichang.annotation.FieldMapping + */ + default > R selectRelation(Function function, List> list) { + return ((MPJRelationMapper) getBaseMapper()).selectRelation(function, list); + } + +} diff --git a/mybatis-plus-join-test/test-base/src/main/java/com/github/yulichang/test/config/MybatisPlusConfig.java b/mybatis-plus-join-test/test-base/src/main/java/com/github/yulichang/test/config/MybatisPlusConfig.java index 60c12d6..563c3a5 100644 --- a/mybatis-plus-join-test/test-base/src/main/java/com/github/yulichang/test/config/MybatisPlusConfig.java +++ b/mybatis-plus-join-test/test-base/src/main/java/com/github/yulichang/test/config/MybatisPlusConfig.java @@ -1,12 +1,11 @@ package com.github.yulichang.test.config; import com.baomidou.mybatisplus.annotation.DbType; +import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; import com.github.yulichang.test.util.ThreadLocalUtils; import lombok.SneakyThrows; import org.apache.ibatis.builder.SqlSourceBuilder; @@ -51,11 +50,8 @@ public class MybatisPlusConfig { @SneakyThrows public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) { String sql = boundSql.getSql(); - String s = ThreadLocalUtils.get(); - if (StringUtils.isNotBlank(s)) { - ObjectMapper mapper = new ObjectMapper(); - List sqlList = mapper.readValue(s, new TypeReference>() { - }); + List sqlList = ThreadLocalUtils.get(); + if (CollectionUtils.isNotEmpty(sqlList)) { if (sqlList.stream().anyMatch(e -> Objects.equals(formatSql(sql), formatSql(e)))) { System.out.println("==============================================="); System.out.println(); @@ -79,11 +75,8 @@ public class MybatisPlusConfig { if (sql.toUpperCase().startsWith("SELECT")) { return; } - String s = ThreadLocalUtils.get(); - if (StringUtils.isNotBlank(s)) { - ObjectMapper mapper = new ObjectMapper(); - List sqlList = mapper.readValue(s, new TypeReference>() { - }); + List sqlList = ThreadLocalUtils.get(); + if (CollectionUtils.isNotEmpty(sqlList)) { if (sqlList.stream().anyMatch(e -> Objects.equals(formatSql(sql), formatSql(e)))) { System.out.println("==============================================="); System.out.println(); diff --git a/mybatis-plus-join-test/test-base/src/main/java/com/github/yulichang/test/util/ThreadLocalUtils.java b/mybatis-plus-join-test/test-base/src/main/java/com/github/yulichang/test/util/ThreadLocalUtils.java index a309fe4..13ee788 100644 --- a/mybatis-plus-join-test/test-base/src/main/java/com/github/yulichang/test/util/ThreadLocalUtils.java +++ b/mybatis-plus-join-test/test-base/src/main/java/com/github/yulichang/test/util/ThreadLocalUtils.java @@ -1,9 +1,13 @@ package com.github.yulichang.test.util; +import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.SneakyThrows; import java.util.Arrays; +import java.util.List; public class ThreadLocalUtils { @@ -24,9 +28,17 @@ public class ThreadLocalUtils { /** * 获取线程中的数据 */ - public static String get() { + @SneakyThrows + public static List get() { String s = userThreadLocal.get(); + if (StringUtils.isBlank(s)) { + return null; + } + ObjectMapper mapper = new ObjectMapper(); + List sqlList = mapper.readValue(s, new TypeReference>() { + }); + sqlList.removeIf(StringUtils::isBlank); set(""); - return s; + return sqlList; } }