refactor: rename "splitBy" to "selfFieldSplitBy"

This commit is contained in:
开源海哥 2023-09-26 19:30:23 +08:00
parent ab8cbbc07a
commit f189888d0f
6 changed files with 21 additions and 25 deletions

View File

@ -230,9 +230,9 @@ public class Account implements Serializable {
> 多对多注解 `@RelationManyToMany` 也是如此。
**splitBy 分割查询** <Badge type="tip" text="v1.6.8" />
**selfFieldSplitBy 分割查询** <Badge type="tip" text="v1.6.8" />
`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<Tag> tagList;
@RelationOneToMany(
selfField = "diseaseIds",
splitBy = ",", //使用 , 进行分割
selfFieldSplitBy = ",", //使用 , 进行分割
targetField = "diseaseId", //测试目标字段是字符串类型是否正常转换
mapKeyField = "diseaseId" //测试Map映射
)

View File

@ -38,7 +38,7 @@ public @interface RelationOneToMany {
* 当前字段值根据字符串分割
* @return 分割字符串
*/
String splitBy() default "";
String selfFieldSplitBy() default "";
/**
* <p>

View File

@ -35,7 +35,7 @@ class OneToMany<SelfEntity> extends ToManyRelation<SelfEntity> {
, annotation.extraCondition()
, annotation.selectColumns());
this.splitBy = annotation.splitBy();
this.selfFieldSplitBy = annotation.selfFieldSplitBy();
this.orderBy = annotation.orderBy();
this.limit = annotation.limit();

View File

@ -29,8 +29,7 @@ class ToManyRelation<SelfEntity> extends AbstractRelation<SelfEntity> {
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<SelfEntity> extends AbstractRelation<SelfEntity> {
*/
@Override
public QueryWrapper buildQueryWrapper(Set<Object> targetValues) {
if (StringUtil.isNotBlank(splitBy) && CollectionUtil.isNotEmpty(targetValues)) {
if (StringUtil.isNotBlank(selfFieldSplitBy) && CollectionUtil.isNotEmpty(targetValues)) {
Set<Object> 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<SelfEntity> extends AbstractRelation<SelfEntity> {
}
}
} 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);

View File

@ -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<Tag> tagList;
@RelationOneToMany(
selfField = "diseaseIds",
splitBy = ",", //使用 , 进行分割
selfFieldSplitBy = ",", //使用 , 进行分割
targetField = "diseaseId", //测试目标字段是字符串类型是否正常转换
mapKeyField = "diseaseId" //测试Map映射
)

View File

@ -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));
}
}