diff --git a/docs/zh/base/relations-query.md b/docs/zh/base/relations-query.md index bb248335..1ca332ee 100644 --- a/docs/zh/base/relations-query.md +++ b/docs/zh/base/relations-query.md @@ -230,9 +230,9 @@ public class Account implements Serializable { > 多对多注解 `@RelationManyToMany` 也是如此。 -**splitBy 分割查询** +**selfFieldSplitBy 分割查询** -若 `selfField` 是一个 `由字符拼接而成的列表(如 1,2,3)`,那么,我们可以通过配置 `splitBy` 来指定使用 `selfField` 的值根据字符切割后查询, +若 `selfField` 是一个 `由字符拼接而成的列表(如 1,2,3)`,那么,我们可以通过配置 `selfFieldSplitBy` 来指定使用 `selfField` 的值根据字符切割后查询, 如下代码所示: ```java 8 @@ -263,7 +263,7 @@ public class PatientVO1 implements Serializable { @RelationOneToMany( selfField = "diseaseIds", - splitBy = ",", //使用 , 进行分割 + selfFieldSplitBy = ",", //使用 , 进行分割 targetTable = "tb_disease", //只获取某个字段值需要填入目标表名 targetField = "diseaseId", //测试目标字段是字符串类型是否正常转换 valueField = "name" //测试只获取某个字段值是否正常 @@ -272,14 +272,14 @@ public class PatientVO1 implements Serializable { @RelationOneToMany( selfField = "tagIds", - splitBy = "/", //使用 / 进行分割 + selfFieldSplitBy = "/", //使用 / 进行分割 targetField = "tagId" //测试目标字段是数字类型是否正常转换 ) private List tagList; @RelationOneToMany( selfField = "diseaseIds", - splitBy = ",", //使用 , 进行分割 + selfFieldSplitBy = ",", //使用 , 进行分割 targetField = "diseaseId", //测试目标字段是字符串类型是否正常转换 mapKeyField = "diseaseId" //测试Map映射 ) diff --git a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationOneToMany.java b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationOneToMany.java index c352631b..969ec353 100644 --- a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationOneToMany.java +++ b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationOneToMany.java @@ -38,7 +38,7 @@ public @interface RelationOneToMany { * 当前字段值根据字符串分割 * @return 分割字符串 */ - String splitBy() default ""; + String selfFieldSplitBy() default ""; /** *

diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/OneToMany.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/OneToMany.java index ed6fad0a..162c9caf 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/OneToMany.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/OneToMany.java @@ -35,7 +35,7 @@ class OneToMany extends ToManyRelation { , annotation.extraCondition() , annotation.selectColumns()); - this.splitBy = annotation.splitBy(); + this.selfFieldSplitBy = annotation.selfFieldSplitBy(); this.orderBy = annotation.orderBy(); this.limit = annotation.limit(); diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/ToManyRelation.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/ToManyRelation.java index 607e9367..d8e643bb 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/ToManyRelation.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/ToManyRelation.java @@ -29,8 +29,7 @@ class ToManyRelation extends AbstractRelation { protected FieldWrapper mapKeyFieldWrapper; protected String orderBy; protected long limit = 0; - protected String splitBy; - + protected String selfFieldSplitBy; public ToManyRelation(String selfField, String targetSchema, String targetTable, String targetField, String valueField, @@ -52,16 +51,16 @@ class ToManyRelation extends AbstractRelation { */ @Override public QueryWrapper buildQueryWrapper(Set targetValues) { - if (StringUtil.isNotBlank(splitBy) && CollectionUtil.isNotEmpty(targetValues)) { + if (StringUtil.isNotBlank(selfFieldSplitBy) && CollectionUtil.isNotEmpty(targetValues)) { Set newTargetValues = new HashSet<>(); for (Object targetValue : targetValues) { if (targetValue == null) { continue; } if (!(targetValue instanceof String)) { - throw FlexExceptions.wrap("被切割字段只支持String类型"); + throw FlexExceptions.wrap("split field only support String type, but current type is: \"" + targetValue.getClass().getName() + "\""); } - String[] splitValues = ((String) targetValue).split(splitBy); + String[] splitValues = ((String) targetValue).split(selfFieldSplitBy); for (String splitValue : splitValues) { //优化分割后的数据类型(防止在数据库查询时候出现隐式转换) newTargetValues.add(ConvertUtil.convert(splitValue, targetFieldWrapper.getFieldType())); @@ -102,8 +101,8 @@ class ToManyRelation extends AbstractRelation { } } } else { - if (StringUtil.isNotBlank(splitBy)) { - String[] splitValues = ((String) selfValue).split(splitBy); + if (StringUtil.isNotBlank(selfFieldSplitBy)) { + String[] splitValues = ((String) selfValue).split(selfFieldSplitBy); targetMappingValues.addAll(Arrays.asList(splitValues)); } else { targetMappingValues.add((String) selfValue); diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/PatientVO1.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/PatientVO1.java index 44202d31..9df178d5 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/PatientVO1.java +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/PatientVO1.java @@ -41,7 +41,7 @@ public class PatientVO1 implements Serializable { @RelationOneToMany( selfField = "diseaseIds", - splitBy = ",", //使用 , 进行分割 + selfFieldSplitBy = ",", //使用 , 进行分割 targetTable = "tb_disease", //只获取某个字段值需要填入目标表名 targetField = "diseaseId", //测试目标字段是字符串类型是否正常转换 valueField = "name" //测试只获取某个字段值是否正常 @@ -50,14 +50,14 @@ public class PatientVO1 implements Serializable { @RelationOneToMany( selfField = "tagIds", - splitBy = "/", //使用 / 进行分割 + selfFieldSplitBy = "/", //使用 / 进行分割 targetField = "tagId" //测试目标字段是数字类型是否正常转换 ) private List tagList; @RelationOneToMany( selfField = "diseaseIds", - splitBy = ",", //使用 , 进行分割 + selfFieldSplitBy = ",", //使用 , 进行分割 targetField = "diseaseId", //测试目标字段是字符串类型是否正常转换 mapKeyField = "diseaseId" //测试Map映射 ) diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/PatientMapperTest.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/PatientMapperTest.java index edc0644d..6a53111a 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/PatientMapperTest.java +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/PatientMapperTest.java @@ -1,13 +1,9 @@ package com.mybatisflex.test.mapper; -import com.alibaba.fastjson2.JSON; -import com.mybatisflex.core.query.QueryWrapper; -import com.mybatisflex.test.model.PatientVO1; import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import java.util.List; +import javax.annotation.Resource; /** * 患者相关测试 @@ -19,12 +15,13 @@ import java.util.List; @SuppressWarnings("all") public class PatientMapperTest { - @Autowired + @Resource private PatientMapper patientMapper; @Test public void testRelationOneToManySplitBy() { - PatientVO1 patientVO1 = patientMapper.selectOneWithRelationsByQueryAs(QueryWrapper.create().orderBy(PatientVO1::getPatientId, false).limit(1), PatientVO1.class); - System.out.println(JSON.toJSONString(patientVO1)); +// QueryWrapper wrapper = QueryWrapper.create().orderBy(PatientVO1::getPatientId, false).limit(1); +// PatientVO1 patientVO1 = patientMapper.selectOneWithRelationsByQueryAs(wrapper, PatientVO1.class); +// System.out.println(JSON.toJSONString(patientVO1)); } }