diff --git a/mybatis-plus-join-boot-starter/src/main/java/com/github/yulichang/autoconfigure/MybatisPlusJoinAutoConfiguration.java b/mybatis-plus-join-boot-starter/src/main/java/com/github/yulichang/autoconfigure/MybatisPlusJoinAutoConfiguration.java index 6940776..e203d8b 100644 --- a/mybatis-plus-join-boot-starter/src/main/java/com/github/yulichang/autoconfigure/MybatisPlusJoinAutoConfiguration.java +++ b/mybatis-plus-join-boot-starter/src/main/java/com/github/yulichang/autoconfigure/MybatisPlusJoinAutoConfiguration.java @@ -33,8 +33,10 @@ import javax.sql.DataSource; import java.util.List; /** + * springboot 自动配置类 + * * @author yulichang - * @since 1.3.2 + * @since 2.0.0 */ @SuppressWarnings("unused") @Configuration(proxyBeanMethods = false) diff --git a/mybatis-plus-join-boot-starter/src/main/java/com/github/yulichang/autoconfigure/MybatisPlusJoinProperties.java b/mybatis-plus-join-boot-starter/src/main/java/com/github/yulichang/autoconfigure/MybatisPlusJoinProperties.java index 054835a..bb23df8 100644 --- a/mybatis-plus-join-boot-starter/src/main/java/com/github/yulichang/autoconfigure/MybatisPlusJoinProperties.java +++ b/mybatis-plus-join-boot-starter/src/main/java/com/github/yulichang/autoconfigure/MybatisPlusJoinProperties.java @@ -5,8 +5,10 @@ import lombok.experimental.Accessors; import org.springframework.boot.context.properties.ConfigurationProperties; /** + * 配置类 + * * @author yulichang - * @since 1.3.2 + * @since 2.0.0 */ @Data @Accessors(chain = true) diff --git a/mybatis-plus-join-core/pom.xml b/mybatis-plus-join-core/pom.xml index cbf88f2..d202a4e 100644 --- a/mybatis-plus-join-core/pom.xml +++ b/mybatis-plus-join-core/pom.xml @@ -1,4 +1,5 @@ + diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java index 94a860d..067353d 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java @@ -209,45 +209,57 @@ public class MPJInterceptor implements Interceptor { //移除result中不存在的标签 resultMappings.removeIf(i -> !fieldMap.containsKey(i.getProperty())); if (wrapper.isResultMap()) { - List> mybatisLabel = wrapper.getResultMapMybatisLabel(); - for (MybatisLabel mpjColl : mybatisLabel) { - List list = mpjColl.getResultList(); - if (CollectionUtils.isEmpty(list)) { - continue; - } - List 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, true, null)); - ResultMapping.Builder builder = new ResultMapping.Builder(ms.getConfiguration(), r.getProperty(), - StringUtils.getTargetColumn(columnName), r.getJavaType()); - if (r.isId()) {//主键标记为id标签 - 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)) { - ResultMap build = new ResultMap.Builder(ms.getConfiguration(), childId, mpjColl.getOfType(), childMapping).build(); - MPJInterceptor.addResultMap(ms, childId, build); - } - } + buildResult(ms, wrapper.getResultMapMybatisLabel(), columnSet, resultMappings, columnList); } result.add(new ResultMap.Builder(ms.getConfiguration(), id, resultType, resultMappings).build()); return result; } + private void buildResult(MappedStatement ms, List> mybatisLabel, Set columnSet, + List parentMappings, List columnList) { + for (MybatisLabel mpjColl : mybatisLabel) { + List list = mpjColl.getResultList(); + if (CollectionUtils.isEmpty(list)) { + continue; + } + List 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, true, null)); + ResultMapping.Builder builder = new ResultMapping.Builder(ms.getConfiguration(), r.getProperty(), + StringUtils.getTargetColumn(columnName), r.getJavaType()); + if (r.isId()) {//主键标记为id标签 + builder.flags(Collections.singletonList(ResultFlag.ID)); + } + //TypeHandle + TableFieldInfo info = r.getTableFieldInfo(); + if (info != null && info.getTypeHandler() != null && info.getTypeHandler() != UnknownTypeHandler.class) { + builder.typeHandler(getTypeHandler(ms, info)); + } + childMapping.add(builder.build()); + } + //嵌套处理 + if (CollectionUtils.isNotEmpty(mpjColl.getMybatisLabels())) { + this.buildResult(ms, mpjColl.getMybatisLabels(), columnSet, childMapping, columnList); + } + String childId = "MPJ_" + mpjColl.getEntityClass().getName() + StringPool.UNDERSCORE + mpjColl.getOfType().getName() + + 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)); + parentMappings.add(new ResultMapping.Builder(ms.getConfiguration(), mpjColl.getProperty()) + .javaType(mpjColl.getJavaType()) + .nestedResultMapId(childId) + .build()); + //双检 + if (!ms.getConfiguration().getResultMapNames().contains(childId)) { + ResultMap build = new ResultMap.Builder(ms.getConfiguration(), childId, mpjColl.getOfType(), childMapping).build(); + MPJInterceptor.addResultMap(ms, childId, build); + } + } + } + /** * 获取字段typeHandle */ @@ -268,12 +280,13 @@ public class MPJInterceptor implements Interceptor { * @return 唯一列名 */ private String getColumn(Set pool, String columnName) { - if (!pool.contains(columnName)) { - pool.add(columnName); - return columnName; + String tagName = StringUtils.getTargetColumn(columnName); + if (!pool.contains(tagName)) { + pool.add(tagName); + return tagName; } - columnName = "mpj_" + StringUtils.getTargetColumn(columnName); - return getColumn(pool, columnName); + tagName = "mpj_" + tagName; + return getColumn(pool, tagName); } /** diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java index 1fba22e..bbadbb7 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java @@ -11,14 +11,16 @@ import com.baomidou.mybatisplus.core.toolkit.StringPool; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.core.toolkit.support.SFunction; import com.github.yulichang.exception.MPJException; -import com.github.yulichang.toolkit.*; +import com.github.yulichang.toolkit.Constant; +import com.github.yulichang.toolkit.LambdaUtils; +import com.github.yulichang.toolkit.MPJWrappers; +import com.github.yulichang.toolkit.ReflectionKit; import com.github.yulichang.toolkit.support.ColumnCache; import com.github.yulichang.toolkit.support.SelectColumn; import com.github.yulichang.wrapper.enums.BaseFuncEnum; import com.github.yulichang.wrapper.interfaces.LambdaJoin; import com.github.yulichang.wrapper.interfaces.Query; import com.github.yulichang.wrapper.interfaces.on.OnFunction; -import com.github.yulichang.wrapper.resultmap.LabelType; import com.github.yulichang.wrapper.resultmap.MFunc; import com.github.yulichang.wrapper.resultmap.MybatisLabel; import lombok.Getter; @@ -182,13 +184,13 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper builder; if (genericType == null || genericType.isAssignableFrom(child)) { //找不到集合泛型 List List List , 直接查询数据库实体 - builder = new MybatisLabel.Builder<>(LabelType.COLLECTION, dtoFieldName, child, field.getType()); + builder = new MybatisLabel.Builder<>(dtoFieldName, child, field.getType()); } else { Class ofType = (Class) genericType; if (ReflectionKit.isPrimitiveOrWrapper(ofType)) { throw new MPJException("collection 不支持基本数据类型"); } - builder = new MybatisLabel.Builder<>(LabelType.COLLECTION, dtoFieldName, child, field.getType(), ofType, true); + builder = new MybatisLabel.Builder<>(dtoFieldName, child, field.getType(), ofType, true); } this.resultMapMybatisLabel.add(builder.build()); return typedThis; @@ -231,7 +233,7 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper genericType = ReflectionKit.getGenericType(field); Class ofType = (Class) genericType; - MybatisLabel.Builder builder = new MybatisLabel.Builder<>(LabelType.COLLECTION, dtoFieldName, child, field.getType(), ofType, false); + MybatisLabel.Builder builder = new MybatisLabel.Builder<>(dtoFieldName, child, field.getType(), ofType, false); this.resultMapMybatisLabel.add(collection.apply(builder).build()); return typedThis; } @@ -252,7 +254,7 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper builder; - builder = new MybatisLabel.Builder<>(LabelType.ASSOCIATION, dtoFieldName, child, field.getType(), (Class) field.getType(), true); + builder = new MybatisLabel.Builder<>(dtoFieldName, child, field.getType(), (Class) field.getType(), true); this.resultMapMybatisLabel.add(builder.build()); return typedThis; } @@ -272,7 +274,7 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper builder = new MybatisLabel.Builder<>(LabelType.ASSOCIATION, dtoFieldName, child, field.getType(), (Class) child, false); + MybatisLabel.Builder builder = new MybatisLabel.Builder<>(dtoFieldName, child, field.getType(), (Class) child, false); this.resultMapMybatisLabel.add(collection.apply(builder).build()); return typedThis; } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/LabelType.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/LabelType.java deleted file mode 100644 index 6d33b2a..0000000 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/LabelType.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.github.yulichang.wrapper.resultmap; - -public enum LabelType { - COLLECTION, ASSOCIATION -} diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/MybatisLabel.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/MybatisLabel.java index f8de60e..59d2093 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/MybatisLabel.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/MybatisLabel.java @@ -3,12 +3,17 @@ package com.github.yulichang.wrapper.resultmap; import com.baomidou.mybatisplus.core.metadata.TableFieldInfo; import com.baomidou.mybatisplus.core.metadata.TableInfo; import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; +import com.baomidou.mybatisplus.core.toolkit.Assert; +import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.support.SFunction; +import com.github.yulichang.exception.MPJException; +import com.github.yulichang.toolkit.LambdaUtils; import com.github.yulichang.toolkit.ReflectionKit; import lombok.Getter; import java.lang.reflect.Field; import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.Map; import java.util.function.Function; @@ -23,8 +28,6 @@ import java.util.stream.Collectors; @Getter public class MybatisLabel { - private LabelType labelType; - private String property; private Class entityClass; @@ -35,13 +38,15 @@ public class MybatisLabel { private List resultList; - //collection嵌套 - // private List collectionList; + /** + * wrapper里面的引用 + */ + private List> mybatisLabels; private MybatisLabel() { } - @SuppressWarnings("unused") + @SuppressWarnings({"unused", "unchecked"}) public static class Builder { private final MybatisLabel mybatisLabel; @@ -50,14 +55,14 @@ public class MybatisLabel { * 自动构建 */ @SuppressWarnings("unchecked") - public Builder(LabelType labelType, String property, Class entityClass, Class javaType) { + public Builder(String property, Class entityClass, Class javaType) { this.mybatisLabel = new MybatisLabel<>(); - mybatisLabel.labelType = labelType; mybatisLabel.property = property; mybatisLabel.entityClass = entityClass; mybatisLabel.javaType = javaType; mybatisLabel.ofType = (Class) entityClass; mybatisLabel.resultList = new ArrayList<>(); + mybatisLabel.mybatisLabels = new ArrayList<>(); autoBuild(true, entityClass, (Class) entityClass); } @@ -70,14 +75,14 @@ public class MybatisLabel { * @param ofType 映射类 * @param auto 自动映射数据库实体对应的字段 */ - public Builder(LabelType labelType, String property, Class entityClass, Class javaType, Class ofType, boolean auto) { + public Builder(String property, Class entityClass, Class javaType, Class ofType, boolean auto) { this.mybatisLabel = new MybatisLabel<>(); - mybatisLabel.labelType = labelType; mybatisLabel.property = property; mybatisLabel.entityClass = entityClass; mybatisLabel.javaType = javaType; mybatisLabel.ofType = ofType; mybatisLabel.resultList = new ArrayList<>(); + mybatisLabel.mybatisLabels = new ArrayList<>(); autoBuild(auto, entityClass, ofType); } @@ -109,7 +114,85 @@ public class MybatisLabel { return this; } + /** + * 嵌套 + */ + public > Builder collection(Class entityClass, SFunction func) { + String dtoFieldName = LambdaUtils.getName(func); + Class dtoClass = LambdaUtils.getEntityClass(func); + Map fieldMap = ReflectionKit.getFieldMap(dtoClass); + Field field = fieldMap.get(dtoFieldName); + Class genericType = ReflectionKit.getGenericType(field); + MybatisLabel.Builder builder; + if (genericType == null || genericType.isAssignableFrom(entityClass)) { + //找不到集合泛型 List List List , 直接查询数据库实体 + builder = new Builder<>(dtoFieldName, entityClass, field.getType()); + } else { + Class ofType = (Class) genericType; + if (ReflectionKit.isPrimitiveOrWrapper(ofType)) { + throw new MPJException("collection 不支持基本数据类型"); + } + builder = new Builder<>(dtoFieldName, entityClass, field.getType(), ofType, true); + } + mybatisLabel.mybatisLabels.add(builder.build()); + return this; + } + + /** + * 嵌套 + */ + public > Builder collection(Class entityClass, SFunction func, MFunc> mFunc) { + String dtoFieldName = LambdaUtils.getName(func); + Class dtoClass = LambdaUtils.getEntityClass(func); + Field field = ReflectionKit.getFieldMap(dtoClass).get(dtoFieldName); + //获取集合泛型 + Class genericType = ReflectionKit.getGenericType(field); + Class ofType = (Class) genericType; + MybatisLabel.Builder builder = new MybatisLabel.Builder<>(dtoFieldName, entityClass, field.getType(), ofType, false); + mybatisLabel.mybatisLabels.add(mFunc.apply(builder).build()); + return this; + } + + /** + * 嵌套 + */ + public Builder association(Class child, SFunction dtoField) { + Class dtoClass = LambdaUtils.getEntityClass(dtoField); + Map fieldMap = ReflectionKit.getFieldMap(dtoClass); + String dtoFieldName = LambdaUtils.getName(dtoField); + Field field = fieldMap.get(dtoFieldName); + Assert.isFalse(Collection.class.isAssignableFrom(field.getType()), "association 不支持集合类"); + if (ReflectionKit.isPrimitiveOrWrapper(field.getType())) { + throw new MPJException("association 不支持基本数据类型"); + } + MybatisLabel.Builder builder; + builder = new MybatisLabel.Builder<>(dtoFieldName, child, field.getType(), (Class) field.getType(), true); + mybatisLabel.mybatisLabels.add(builder.build()); + return this; + } + + /** + * 嵌套 + */ + public Builder selectAssociation(Class child, SFunction dtoField, + MFunc> collection) { + String dtoFieldName = LambdaUtils.getName(dtoField); + Class dtoClass = LambdaUtils.getEntityClass(dtoField); + Field field = ReflectionKit.getFieldMap(dtoClass).get(dtoFieldName); + Assert.isFalse(Collection.class.isAssignableFrom(field.getType()), "association 不支持集合类"); + if (ReflectionKit.isPrimitiveOrWrapper(field.getType())) { + throw new MPJException("association 不支持基本数据类型"); + } + MybatisLabel.Builder builder = new MybatisLabel.Builder<>(dtoFieldName, child, field.getType(), (Class) child, false); + mybatisLabel.mybatisLabels.add(collection.apply(builder).build()); + return this; + } + + public MybatisLabel build() { + if (CollectionUtils.isEmpty(mybatisLabel.resultList)) { + autoBuild(true, mybatisLabel.entityClass, mybatisLabel.ofType); + } return mybatisLabel; } @@ -120,6 +203,7 @@ public class MybatisLabel { Function build = field -> { Result result = new Result(); result.setId(false); + result.setTableFieldInfo(field); result.setColumn(field.getColumn()); result.setProperty(field.getProperty()); result.setJavaType(field.getField().getType()); diff --git a/mybatis-plus-join-test/pom.xml b/mybatis-plus-join-test/pom.xml index fc888f6..3317236 100644 --- a/mybatis-plus-join-test/pom.xml +++ b/mybatis-plus-join-test/pom.xml @@ -83,6 +83,11 @@ pagehelper-spring-boot-starter 1.4.5 + + com.fasterxml.jackson.core + jackson-databind + 2.14.0 + \ No newline at end of file diff --git a/mybatis-plus-join-test/src/main/java/com/github/yulichang/test/dto/AddressDTO.java b/mybatis-plus-join-test/src/main/java/com/github/yulichang/test/dto/AddressDTO.java new file mode 100644 index 0000000..e516ebf --- /dev/null +++ b/mybatis-plus-join-test/src/main/java/com/github/yulichang/test/dto/AddressDTO.java @@ -0,0 +1,26 @@ +package com.github.yulichang.test.dto; + +import com.github.yulichang.test.entity.AreaDO; +import lombok.Data; + +import java.util.List; + +@Data +public class AddressDTO { + + private Integer id; + + private Integer userId; + + private Integer areaId; + + private String tel; + + private String address; + + private Boolean del; + + private List areaList; + + private AreaDO area; +} diff --git a/mybatis-plus-join-test/src/main/java/com/github/yulichang/test/dto/AreaDTO.java b/mybatis-plus-join-test/src/main/java/com/github/yulichang/test/dto/AreaDTO.java new file mode 100644 index 0000000..ba2ad34 --- /dev/null +++ b/mybatis-plus-join-test/src/main/java/com/github/yulichang/test/dto/AreaDTO.java @@ -0,0 +1,27 @@ +package com.github.yulichang.test.dto; + +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.ToString; +import lombok.experimental.Accessors; + +@Data +@ToString +@Accessors(chain = true) +@EqualsAndHashCode +@TableName("area") +public class AreaDTO { + + private Integer id; + + private String province; + + private String city; + + private String area; + + private String postcode; + + private Boolean del; +} diff --git a/mybatis-plus-join-test/src/main/java/com/github/yulichang/test/dto/UserDTO.java b/mybatis-plus-join-test/src/main/java/com/github/yulichang/test/dto/UserDTO.java index 67a3ef9..82b5d62 100644 --- a/mybatis-plus-join-test/src/main/java/com/github/yulichang/test/dto/UserDTO.java +++ b/mybatis-plus-join-test/src/main/java/com/github/yulichang/test/dto/UserDTO.java @@ -1,11 +1,11 @@ package com.github.yulichang.test.dto; -import com.github.yulichang.test.entity.AddressDO; import com.github.yulichang.test.enums.Sex; import lombok.Data; import lombok.ToString; import java.util.List; +import java.util.Map; /** @@ -17,7 +17,7 @@ public class UserDTO { /** user */ private Integer id; /** user */ - private String nameName; + private Map name; /** user */ private Sex sex; /** user */ @@ -37,5 +37,5 @@ public class UserDTO { /** area */ private String area; - private List addressList; + private List addressList; } diff --git a/mybatis-plus-join-test/src/main/java/com/github/yulichang/test/entity/UserDO.java b/mybatis-plus-join-test/src/main/java/com/github/yulichang/test/entity/UserDO.java index ecb05a3..309b873 100644 --- a/mybatis-plus-join-test/src/main/java/com/github/yulichang/test/entity/UserDO.java +++ b/mybatis-plus-join-test/src/main/java/com/github/yulichang/test/entity/UserDO.java @@ -4,24 +4,27 @@ import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableLogic; import com.baomidou.mybatisplus.annotation.TableName; +import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler; import com.github.yulichang.test.enums.Sex; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.ToString; import lombok.experimental.Accessors; +import java.util.Map; + @Data @ToString @Accessors(chain = true) @EqualsAndHashCode -@TableName("`user`") +@TableName(value = "`user`",autoResultMap = true) public class UserDO { @TableId private Integer id; - @TableField("name") - private String sdafrrvnbioiure; + @TableField(value = "`name`", typeHandler = JacksonTypeHandler.class) + private Map name; private Sex sex; diff --git a/mybatis-plus-join-test/src/main/resources/db/data.sql b/mybatis-plus-join-test/src/main/resources/db/data.sql index 19f1926..2f8baa0 100644 --- a/mybatis-plus-join-test/src/main/resources/db/data.sql +++ b/mybatis-plus-join-test/src/main/resources/db/data.sql @@ -29,28 +29,28 @@ INSERT INTO area (id, province, city, area, postcode, del) VALUES (10022, '北 DELETE FROM `user`; -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 1, '张三01', 1, 1, 'https://url-01', false); -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 2, '李四02', 1, 0, 'https://url-02', false); -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 3, '李四02', 1, 0, 'https://url-03', false); -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 4, '李四04', 1, 0, 'https://url-04', false); -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 5, '李四05', 1, 0, 'https://url-05', false); -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 6, '李四06', 1, 0, 'https://url-06', false); -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 7, '李四07', 1, 0, 'https://url-07', false); -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 8, '李四08', 1, 0, 'https://url-08', false); -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 9, '李四09', 1, 0, 'https://url-09', false); -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (10, '李四10', 1, 0, 'https://url-10', false); -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (11, '李四11', 1, 0, 'https://url-11', false); -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (12, '李四12', 1, 0, 'https://url-12', false); -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (13, '李四13', 1, 0, 'https://url-13', false); -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (14, '李四14', 1, 0, 'https://url-14', false); -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (15, '李四15', 1, 0, 'https://url-15', false); -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (16, '李四16', 1, 0, 'https://url-16', false); -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (17, '李四17', 1, 0, 'https://url-17', false); -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (18, '李四18', 1, 0, 'https://url-18', false); -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (19, '李四19', 1, 0, 'https://url-19', false); -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (20, '李四20', 1, 0, 'https://url-20', false); -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (21, '李四21', 1, 0, 'https://url-21', false); -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (22, '李四22', 1, 0, 'https://url-22', false); +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 1, '{"aa":"aaa","bb":"bbb"}', 1, 1, 'https://url-01', false); +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 2, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-02', false); +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 3, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-03', false); +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 4, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-04', false); +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 5, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-05', false); +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 6, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-06', false); +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 7, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-07', false); +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 8, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-08', false); +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 9, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-09', false); +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (10, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-10', false); +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (11, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-11', false); +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (12, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-12', false); +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (13, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-13', false); +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (14, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-14', false); +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (15, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-15', false); +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (16, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-16', false); +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (17, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-17', false); +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (18, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-18', false); +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (19, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-19', false); +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (20, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-20', false); +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (21, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-21', false); +INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (22, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-22', false); DELETE FROM address; diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/JoinTest.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/LambdaWrapperTest.java similarity index 94% rename from mybatis-plus-join-test/src/test/java/com/github/yulichang/test/JoinTest.java rename to mybatis-plus-join-test/src/test/java/com/github/yulichang/test/LambdaWrapperTest.java index bde0395..798c61a 100644 --- a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/JoinTest.java +++ b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/LambdaWrapperTest.java @@ -2,6 +2,7 @@ package com.github.yulichang.test; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.github.yulichang.test.dto.AddressDTO; import com.github.yulichang.test.dto.UserDTO; import com.github.yulichang.test.entity.AddressDO; import com.github.yulichang.test.entity.AreaDO; @@ -27,7 +28,7 @@ import java.util.Map; */ @SpringBootTest @SuppressWarnings("unused") -class JoinTest { +class LambdaWrapperTest { @Resource private UserMapper userMapper; @Resource @@ -40,10 +41,11 @@ class JoinTest { void testJoin() { MPJLambdaWrapper wrapper = new MPJLambdaWrapper() .selectAll(UserDO.class) - .selectCollection(AddressDO.class, UserDTO::getAddressList) + .selectCollection(AddressDO.class, UserDTO::getAddressList, addr -> addr + .association(AreaDO.class, AddressDTO::getArea)) .leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId) + .leftJoin(AreaDO.class, AreaDO::getId, AddressDO::getAreaId) .orderByDesc(UserDO::getId); - List list = userMapper.selectJoinList(UserDTO.class, wrapper); list.forEach(System.out::println); } diff --git a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/MPJ131Test.java b/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/MPJ131Test.java deleted file mode 100644 index f273116..0000000 --- a/mybatis-plus-join-test/src/test/java/com/github/yulichang/test/MPJ131Test.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.github.yulichang.test; - -import com.github.yulichang.test.mapper.UserMapper; -import org.junit.jupiter.api.Test; -import org.springframework.boot.test.context.SpringBootTest; - -import javax.annotation.Resource; - -/** - * 测试 - */ -@SpringBootTest -@SuppressWarnings("unused") -class MPJ131Test { - @Resource - private UserMapper userMapper; - - /** - * 先明确 你要干啥? - * 在mapper中定义lambda形式的resultMap? - * 要解决问题 - * 1、只初始化了部分数据库实体类? NPE - *

- * stater - * core - * anntion - * test - *

- * 超高自由度的sql拼接器? - * 解决了自连接和子查询和各种函数 - * 如果底层调用MPJLambdaWrapper那样还有什么意义 - */ - @Test - void testJoin() { - System.out.println(1); - } -}