diff --git a/README.md b/README.md
index 1b1f481..0e1f2e1 100644
--- a/README.md
+++ b/README.md
@@ -1,12 +1,12 @@
# mybatis-plus-join
-* 对 [mybatis-plus](https://gitee.com/baomidou/mybatis-plus) 多表查询的扩展
-* [演示工程](https://gitee.com/best_handsome/mybatis-plus-join-demo)
+* 对 mybatis-plus 多表查询的扩展
+* 演示工程
* 文档
* 点个Star支持一下吧 :)
QQ群:1022221898
-[加入微信群](https://gitee.com/best_handsome/mybatis-plus-join/issues/I65N2M)
+加入微信群
### 文档
@@ -28,7 +28,7 @@ QQ群:1022221898
```
或者clone代码到本地执行 mvn install, 再引入以上依赖
- 注意: mybatis plus version >= 3.4.0
+ 注意: mybatis plus version >= 3.3.0
### 使用
@@ -252,7 +252,7 @@ ORDER BY
addr.id DESC
```
-# [wiki](https://ylctmh.com)
+# wiki
diff --git a/mybatis-plus-join-core/src/main/java/com/baomidou/mybatisplus/core/metadata/MPJTableInfoHelper.java b/mybatis-plus-join-core/src/main/java/com/baomidou/mybatisplus/core/metadata/MPJTableInfoHelper.java
index 1fe8dd7..8f208fd 100644
--- a/mybatis-plus-join-core/src/main/java/com/baomidou/mybatisplus/core/metadata/MPJTableInfoHelper.java
+++ b/mybatis-plus-join-core/src/main/java/com/baomidou/mybatisplus/core/metadata/MPJTableInfoHelper.java
@@ -5,7 +5,9 @@ import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
import com.github.yulichang.annotation.EntityMapping;
import com.github.yulichang.annotation.FieldMapping;
import com.github.yulichang.exception.MPJException;
+import com.github.yulichang.mapper.MPJTableFieldInfo;
import com.github.yulichang.mapper.MPJTableInfo;
+import com.github.yulichang.toolkit.MPJReflectionKit;
import com.github.yulichang.toolkit.TableHelper;
import org.apache.ibatis.session.Configuration;
@@ -28,7 +30,6 @@ import java.util.concurrent.ConcurrentHashMap;
* @author yulichang
* @see TableInfoHelper
*/
-@SuppressWarnings("deprecation")
public class MPJTableInfoHelper {
/**
@@ -45,7 +46,7 @@ public class MPJTableInfoHelper {
* @return 数据库表反射信息
*/
public static MPJTableInfo getTableInfo(Class> clazz) {
- if (clazz == null || ReflectionKit.isPrimitiveOrWrapper(clazz) || clazz == String.class || clazz.isInterface()) {
+ if (clazz == null || MPJReflectionKit.isPrimitiveOrWrapper(clazz) || clazz == String.class || clazz.isInterface()) {
return null;
}
return TABLE_INFO_CACHE.get(clazz);
@@ -76,10 +77,14 @@ public class MPJTableInfoHelper {
}
MPJTableInfo mpjTableInfo = new MPJTableInfo();
mpjTableInfo.setMapperClass(mapperClass);
+ mpjTableInfo.setEntityClass(clazz);
TableInfo tableInfo = TableHelper.get(clazz);
if (tableInfo == null) {
- return;
+ if (mapperClass != null) {
+ return;
+ }
}
+ mpjTableInfo.setDto(tableInfo == null);
mpjTableInfo.setTableInfo(tableInfo);
initMapping(mpjTableInfo);
TABLE_INFO_CACHE.put(clazz, mpjTableInfo);
@@ -98,26 +103,26 @@ public class MPJTableInfoHelper {
*/
public static void initMapping(MPJTableInfo mpjTableInfo) {
// 是否存在 @EntityMapping 注解
- boolean existMapping = isExistMapping(mpjTableInfo.getTableInfo().getEntityType());
+ boolean existMapping = isExistMapping(mpjTableInfo.getEntityClass());
mpjTableInfo.setHasMapping(existMapping);
// 是否存在 @FieldMapping 注解
- boolean existMappingField = isExistMappingField(mpjTableInfo.getTableInfo().getEntityType());
+ boolean existMappingField = isExistMappingField(mpjTableInfo.getEntityClass());
mpjTableInfo.setHasMappingField(existMappingField);
mpjTableInfo.setHasMappingOrField(existMapping || existMappingField);
/* 关系映射初始化 */
- List mpjFieldList = new ArrayList<>();
- List fields = ReflectionKit.getFieldList(ClassUtils.getUserClass(mpjTableInfo.getTableInfo().getEntityType()));
+ List mpjFieldList = new ArrayList<>();
+ List fields = ReflectionKit.getFieldList(ClassUtils.getUserClass(mpjTableInfo.getEntityClass()));
for (Field field : fields) {
if (existMapping) {
EntityMapping mapping = field.getAnnotation(EntityMapping.class);
if (mapping != null) {
- mpjFieldList.add(new com.github.yulichang.mapper.MPJTableFieldInfo(mpjTableInfo.getTableInfo().getEntityType(), mapping, field));
+ mpjFieldList.add(new MPJTableFieldInfo(mpjTableInfo.getEntityClass(), mapping, field));
}
}
if (existMappingField) {
FieldMapping mapping = field.getAnnotation(FieldMapping.class);
if (mapping != null) {
- mpjFieldList.add(new com.github.yulichang.mapper.MPJTableFieldInfo(mpjTableInfo.getTableInfo().getEntityType(), mapping, field));
+ mpjFieldList.add(new MPJTableFieldInfo(mpjTableInfo.getEntityClass(), mapping, field));
}
}
}
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 0d43424..e0fbad5 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
@@ -3,12 +3,13 @@ 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;
/**
* @author yulichang
* @see BaseMapper
*/
-public interface MPJBaseMapper extends MPJJoinMapper, MPJDeepMapper {
+public interface MPJBaseMapper extends MPJJoinMapper, MPJRelationMapper, MPJDeepMapper {
}
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 77430d7..2cb4648 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
@@ -9,7 +9,6 @@ import com.github.yulichang.base.service.MPJJoinService;
*
* @author yulichang
* @see MPJJoinService
- * @see MPJDeepService
*/
public interface MPJBaseService extends MPJJoinService, MPJDeepService {
}
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
new file mode 100644
index 0000000..5da191e
--- /dev/null
+++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/base/mapper/MPJRelationMapper.java
@@ -0,0 +1,77 @@
+package com.github.yulichang.base.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
+import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
+import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
+import com.github.yulichang.relation.Relation;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.function.Function;
+
+public interface MPJRelationMapper extends BaseMapper {
+
+ /**
+ * 通过注解实现单表多次查询
+ *
+ * @param function BaseMapper调用方法
+ * @param deep 是否深度查询
+ * @param prop 属性过滤, 可以只查询需要映射的属性
+ * @see com.github.yulichang.annotation.EntityMapping
+ * @see com.github.yulichang.annotation.FieldMapping
+ */
+ @SuppressWarnings("unchecked")
+ default > R selectRelation(Function function, boolean deep, SFunction... prop) {
+ R r = function.apply((M) this);
+ if (Objects.isNull(r)) {
+ return null;
+ }
+ if (r instanceof List) {
+ List data = (List) r;
+ if (CollectionUtils.isEmpty(data)) {
+ return r;
+ } else {
+ T t = data.get(0);
+ if (Map.class.isAssignableFrom(t.getClass())) {
+ throw ExceptionUtils.mpe("暂不支持Map类型映射");
+ }
+ if (Object.class == t.getClass()) {
+ return r;
+ }
+ return (R) Relation.list(data, Arrays.asList(prop), deep);
+ }
+ }
+ if (r instanceof IPage) {
+ IPage data = (IPage) r;
+ if (!CollectionUtils.isEmpty(data.getRecords())) {
+ T t = data.getRecords().get(0);
+ if (Map.class.isAssignableFrom(t.getClass())) {
+ throw ExceptionUtils.mpe("暂不支持Map类型映射");
+ }
+ if (Object.class == t.getClass()) {
+ return r;
+ }
+ Relation.list(data.getRecords(), Arrays.asList(prop), deep);
+ }
+ return r;
+ }
+ if (r instanceof Integer) {
+ return r;
+ }
+ if (r instanceof Long) {
+ return r;
+ }
+ if (r instanceof Boolean) {
+ return r;
+ }
+ if (Object.class == r.getClass()) {
+ return r;
+ }
+ T data = (T) r;
+ return (R) Relation.one(data, Arrays.asList(prop), deep);
+ }
+}
diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/base/service/MPJDeepService.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/base/service/MPJDeepService.java
index d8325f8..1926c33 100644
--- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/base/service/MPJDeepService.java
+++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/base/service/MPJDeepService.java
@@ -7,7 +7,7 @@ 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.MPJBaseMapper;
+import com.github.yulichang.base.mapper.MPJDeepMapper;
import java.io.Serializable;
import java.util.Arrays;
@@ -38,7 +38,7 @@ public interface MPJDeepService extends IService {
* @param id 主键ID列表
*/
default T getByIdDeep(Serializable id) {
- return ((MPJBaseMapper) getBaseMapper()).selectByIdDeep(id);
+ return ((MPJDeepMapper) getBaseMapper()).selectByIdDeep(id);
}
/**
@@ -50,7 +50,7 @@ public interface MPJDeepService extends IService {
* @param property 需要关联的字段
*/
default T getByIdDeep(Serializable id, SFunction... property) {
- return ((MPJBaseMapper) getBaseMapper()).selectByIdDeep(id, property);
+ return ((MPJDeepMapper) getBaseMapper()).selectByIdDeep(id, property);
}
/**
@@ -63,7 +63,7 @@ public interface MPJDeepService extends IService {
* @param property 需要关联的字段
*/
default T getByIdDeep(Serializable id, List> property) {
- return ((MPJBaseMapper) getBaseMapper()).selectByIdDeep(id, property);
+ return ((MPJDeepMapper) getBaseMapper()).selectByIdDeep(id, property);
}
/**
@@ -72,7 +72,7 @@ public interface MPJDeepService extends IService {
* @param idList 主键ID列表
*/
default List listByIdsDeep(Collection extends Serializable> idList) {
- return ((MPJBaseMapper) getBaseMapper()).selectBatchIdsDeep(idList);
+ return ((MPJDeepMapper) getBaseMapper()).selectBatchIdsDeep(idList);
}
/**
@@ -84,7 +84,7 @@ public interface MPJDeepService extends IService {
* @param property 需要关联的字段
*/
default List listByIdsDeep(Collection extends Serializable> idList, SFunction... property) {
- return ((MPJBaseMapper) getBaseMapper()).selectBatchIdsDeep(idList, Arrays.asList(property));
+ return ((MPJDeepMapper) getBaseMapper()).selectBatchIdsDeep(idList, Arrays.asList(property));
}
/**
@@ -97,7 +97,7 @@ public interface MPJDeepService extends IService {
* @param property 需要关联的字段
*/
default List listByIdsDeep(Collection extends Serializable> idList, List> property) {
- return ((MPJBaseMapper) getBaseMapper()).selectBatchIdsDeep(idList, property);
+ return ((MPJDeepMapper) getBaseMapper()).selectBatchIdsDeep(idList, property);
}
/**
@@ -106,7 +106,7 @@ public interface MPJDeepService extends IService {
* @param columnMap 表字段 map 对象
*/
default List listByMapDeep(Map columnMap) {
- return ((MPJBaseMapper) getBaseMapper()).selectByMapDeep(columnMap);
+ return ((MPJDeepMapper) getBaseMapper()).selectByMapDeep(columnMap);
}
/**
@@ -118,7 +118,7 @@ public interface MPJDeepService extends IService {
* @param property 需要关联的字段
*/
default List listByMapDeep(Map columnMap, SFunction... property) {
- return ((MPJBaseMapper) getBaseMapper()).selectByMapDeep(columnMap, Arrays.asList(property));
+ return ((MPJDeepMapper) getBaseMapper()).selectByMapDeep(columnMap, Arrays.asList(property));
}
/**
@@ -131,7 +131,7 @@ public interface MPJDeepService extends IService {
* @param property 需要关联的字段
*/
default List listByMapDeep(Map columnMap, List> property) {
- return ((MPJBaseMapper) getBaseMapper()).selectByMapDeep(columnMap, property);
+ return ((MPJDeepMapper) getBaseMapper()).selectByMapDeep(columnMap, property);
}
@@ -142,7 +142,7 @@ public interface MPJDeepService extends IService {
* @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
*/
default T getOneDeep(Wrapper queryWrapper) {
- return ((MPJBaseMapper) getBaseMapper()).selectOneDeep(queryWrapper);
+ return ((MPJDeepMapper) getBaseMapper()).selectOneDeep(queryWrapper);
}
/**
@@ -155,7 +155,7 @@ public interface MPJDeepService extends IService {
* @param property 需要关联的字段
*/
default T getOneDeep(Wrapper queryWrapper, SFunction... property) {
- return ((MPJBaseMapper) getBaseMapper()).selectOneDeep(queryWrapper, Arrays.asList(property));
+ return ((MPJDeepMapper) getBaseMapper()).selectOneDeep(queryWrapper, Arrays.asList(property));
}
/**
@@ -169,7 +169,7 @@ public interface MPJDeepService extends IService {
* @param property 需要关联的字段
*/
default T getOneDeep(Wrapper queryWrapper, List> property) {
- return ((MPJBaseMapper) getBaseMapper()).selectOneDeep(queryWrapper, property);
+ return ((MPJDeepMapper) getBaseMapper()).selectOneDeep(queryWrapper, property);
}
/**
@@ -179,7 +179,7 @@ public interface MPJDeepService extends IService {
* @param throwEx 有多个 result 是否抛出异常
*/
default T getOneDeep(Wrapper queryWrapper, boolean throwEx) {
- return ((MPJBaseMapper) getBaseMapper()).mpjQueryMapping(getOne(queryWrapper, throwEx), null);
+ return ((MPJDeepMapper) getBaseMapper()).mpjQueryMapping(getOne(queryWrapper, throwEx), null);
}
/**
@@ -192,7 +192,7 @@ public interface MPJDeepService extends IService {
* @param property 需要关联的字段
*/
default T getOneDeep(Wrapper queryWrapper, boolean throwEx, SFunction... property) {
- return ((MPJBaseMapper) getBaseMapper()).mpjQueryMapping(getOne(queryWrapper, throwEx), Arrays.asList(property));
+ return ((MPJDeepMapper) getBaseMapper()).mpjQueryMapping(getOne(queryWrapper, throwEx), Arrays.asList(property));
}
/**
@@ -206,7 +206,7 @@ public interface MPJDeepService extends IService {
* @param property 需要关联的字段
*/
default T getOneDeep(Wrapper queryWrapper, boolean throwEx, List> property) {
- return ((MPJBaseMapper) getBaseMapper()).mpjQueryMapping(getOne(queryWrapper, throwEx), property);
+ return ((MPJDeepMapper) getBaseMapper()).mpjQueryMapping(getOne(queryWrapper, throwEx), property);
}
/**
@@ -215,7 +215,7 @@ public interface MPJDeepService extends IService {
* @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
*/
default Map getMapDeep(Wrapper queryWrapper) {
- return ((MPJBaseMapper) getBaseMapper()).mpjQueryMapMapping(getMap(queryWrapper), currentModelClass(),
+ return ((MPJDeepMapper) getBaseMapper()).mpjQueryMapMapping(getMap(queryWrapper), currentModelClass(),
null);
}
@@ -228,7 +228,7 @@ public interface MPJDeepService extends IService {
* @param property 需要关联的字段
*/
default Map getMapDeep(Wrapper queryWrapper, SFunction... property) {
- return ((MPJBaseMapper) getBaseMapper()).mpjQueryMapMapping(getMap(queryWrapper), currentModelClass(),
+ return ((MPJDeepMapper) getBaseMapper()).mpjQueryMapMapping(getMap(queryWrapper), currentModelClass(),
Arrays.asList(property));
}
@@ -242,7 +242,7 @@ public interface MPJDeepService extends IService {
* @param property 需要关联的字段
*/
default Map getMapDeep(Wrapper queryWrapper, List> property) {
- return ((MPJBaseMapper) getBaseMapper()).mpjQueryMapMapping(getMap(queryWrapper), currentModelClass(), property);
+ return ((MPJDeepMapper) getBaseMapper()).mpjQueryMapMapping(getMap(queryWrapper), currentModelClass(), property);
}
/**
@@ -251,7 +251,7 @@ public interface MPJDeepService extends IService {
* @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
*/
default List listDeep(Wrapper queryWrapper) {
- return ((MPJBaseMapper) getBaseMapper()).selectListDeep(queryWrapper);
+ return ((MPJDeepMapper) getBaseMapper()).selectListDeep(queryWrapper);
}
/**
@@ -263,7 +263,7 @@ public interface MPJDeepService extends IService {
* @param property 需要关联的字段
*/
default List listDeep(Wrapper queryWrapper, SFunction... property) {
- return ((MPJBaseMapper) getBaseMapper()).selectListDeep(queryWrapper, Arrays.asList(property));
+ return ((MPJDeepMapper) getBaseMapper()).selectListDeep(queryWrapper, Arrays.asList(property));
}
/**
@@ -276,7 +276,7 @@ public interface MPJDeepService extends IService {
* @param property 需要关联的字段
*/
default List listDeep(Wrapper queryWrapper, List> property) {
- return ((MPJBaseMapper) getBaseMapper()).selectListDeep(queryWrapper, property);
+ return ((MPJDeepMapper) getBaseMapper()).selectListDeep(queryWrapper, property);
}
/**
@@ -285,7 +285,7 @@ public interface MPJDeepService extends IService {
* @see Wrappers#emptyWrapper()
*/
default List listDeep() {
- return ((MPJBaseMapper) getBaseMapper()).selectListDeep(Wrappers.emptyWrapper());
+ return ((MPJDeepMapper) getBaseMapper()).selectListDeep(Wrappers.emptyWrapper());
}
/**
@@ -297,7 +297,7 @@ public interface MPJDeepService extends IService {
* @see Wrappers#emptyWrapper()
*/
default List listDeep(SFunction... property) {
- return ((MPJBaseMapper) getBaseMapper()).selectListDeep(Wrappers.emptyWrapper(), Arrays.asList(property));
+ return ((MPJDeepMapper) getBaseMapper()).selectListDeep(Wrappers.emptyWrapper(), Arrays.asList(property));
}
/**
@@ -310,7 +310,7 @@ public interface MPJDeepService extends IService {
* @see Wrappers#emptyWrapper()
*/
default List listDeep(List> property) {
- return ((MPJBaseMapper) getBaseMapper()).selectListDeep(Wrappers.emptyWrapper(), property);
+ return ((MPJDeepMapper) getBaseMapper()).selectListDeep(Wrappers.emptyWrapper(), property);
}
/**
@@ -321,7 +321,7 @@ public interface MPJDeepService extends IService {
*/
default > E pageDeep(E page, Wrapper queryWrapper) {
E e = page(page, queryWrapper);
- ((MPJBaseMapper) getBaseMapper()).mpjQueryMapping(e.getRecords(), null);
+ ((MPJDeepMapper) getBaseMapper()).mpjQueryMapping(e.getRecords(), null);
return e;
}
@@ -336,7 +336,7 @@ public interface MPJDeepService extends IService {
*/
default > E pageDeep(E page, Wrapper queryWrapper, SFunction... property) {
E e = page(page, queryWrapper);
- ((MPJBaseMapper) getBaseMapper()).mpjQueryMapping(e.getRecords(), Arrays.asList(property));
+ ((MPJDeepMapper) getBaseMapper()).mpjQueryMapping(e.getRecords(), Arrays.asList(property));
return e;
}
@@ -352,7 +352,7 @@ public interface MPJDeepService extends IService {
*/
default > E pageDeep(E page, Wrapper queryWrapper, List> property) {
E e = page(page, queryWrapper);
- ((MPJBaseMapper) getBaseMapper()).mpjQueryMapping(e.getRecords(), property);
+ ((MPJDeepMapper) getBaseMapper()).mpjQueryMapping(e.getRecords(), property);
return e;
}
@@ -364,7 +364,7 @@ public interface MPJDeepService extends IService {
*/
default > E pageDeep(E page) {
E e = page(page);
- ((MPJBaseMapper) getBaseMapper()).mpjQueryMapping(e.getRecords(), null);
+ ((MPJDeepMapper) getBaseMapper()).mpjQueryMapping(e.getRecords(), null);
return e;
}
@@ -379,7 +379,7 @@ public interface MPJDeepService extends IService {
*/
default > E pageDeep(E page, SFunction... property) {
E e = page(page);
- ((MPJBaseMapper) getBaseMapper()).mpjQueryMapping(e.getRecords(), Arrays.asList(property));
+ ((MPJDeepMapper) getBaseMapper()).mpjQueryMapping(e.getRecords(), Arrays.asList(property));
return e;
}
@@ -395,7 +395,7 @@ public interface MPJDeepService extends IService {
*/
default > E pageDeep(E page, List> property) {
E e = page(page);
- ((MPJBaseMapper) getBaseMapper()).mpjQueryMapping(e.getRecords(), property);
+ ((MPJDeepMapper) getBaseMapper()).mpjQueryMapping(e.getRecords(), property);
return e;
}
@@ -405,7 +405,7 @@ public interface MPJDeepService extends IService {
* @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
*/
default List