mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
refactor: rename "splitBy" to "selfFieldSplitBy"
This commit is contained in:
parent
ab8cbbc07a
commit
f189888d0f
@ -230,9 +230,9 @@ public class Account implements Serializable {
|
|||||||
|
|
||||||
> 多对多注解 `@RelationManyToMany` 也是如此。
|
> 多对多注解 `@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
|
```java 8
|
||||||
@ -263,7 +263,7 @@ public class PatientVO1 implements Serializable {
|
|||||||
|
|
||||||
@RelationOneToMany(
|
@RelationOneToMany(
|
||||||
selfField = "diseaseIds",
|
selfField = "diseaseIds",
|
||||||
splitBy = ",", //使用 , 进行分割
|
selfFieldSplitBy = ",", //使用 , 进行分割
|
||||||
targetTable = "tb_disease", //只获取某个字段值需要填入目标表名
|
targetTable = "tb_disease", //只获取某个字段值需要填入目标表名
|
||||||
targetField = "diseaseId", //测试目标字段是字符串类型是否正常转换
|
targetField = "diseaseId", //测试目标字段是字符串类型是否正常转换
|
||||||
valueField = "name" //测试只获取某个字段值是否正常
|
valueField = "name" //测试只获取某个字段值是否正常
|
||||||
@ -272,14 +272,14 @@ public class PatientVO1 implements Serializable {
|
|||||||
|
|
||||||
@RelationOneToMany(
|
@RelationOneToMany(
|
||||||
selfField = "tagIds",
|
selfField = "tagIds",
|
||||||
splitBy = "/", //使用 / 进行分割
|
selfFieldSplitBy = "/", //使用 / 进行分割
|
||||||
targetField = "tagId" //测试目标字段是数字类型是否正常转换
|
targetField = "tagId" //测试目标字段是数字类型是否正常转换
|
||||||
)
|
)
|
||||||
private List<Tag> tagList;
|
private List<Tag> tagList;
|
||||||
|
|
||||||
@RelationOneToMany(
|
@RelationOneToMany(
|
||||||
selfField = "diseaseIds",
|
selfField = "diseaseIds",
|
||||||
splitBy = ",", //使用 , 进行分割
|
selfFieldSplitBy = ",", //使用 , 进行分割
|
||||||
targetField = "diseaseId", //测试目标字段是字符串类型是否正常转换
|
targetField = "diseaseId", //测试目标字段是字符串类型是否正常转换
|
||||||
mapKeyField = "diseaseId" //测试Map映射
|
mapKeyField = "diseaseId" //测试Map映射
|
||||||
)
|
)
|
||||||
|
|||||||
@ -38,7 +38,7 @@ public @interface RelationOneToMany {
|
|||||||
* 当前字段值根据字符串分割
|
* 当前字段值根据字符串分割
|
||||||
* @return 分割字符串
|
* @return 分割字符串
|
||||||
*/
|
*/
|
||||||
String splitBy() default "";
|
String selfFieldSplitBy() default "";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
|
|||||||
@ -35,7 +35,7 @@ class OneToMany<SelfEntity> extends ToManyRelation<SelfEntity> {
|
|||||||
, annotation.extraCondition()
|
, annotation.extraCondition()
|
||||||
, annotation.selectColumns());
|
, annotation.selectColumns());
|
||||||
|
|
||||||
this.splitBy = annotation.splitBy();
|
this.selfFieldSplitBy = annotation.selfFieldSplitBy();
|
||||||
this.orderBy = annotation.orderBy();
|
this.orderBy = annotation.orderBy();
|
||||||
this.limit = annotation.limit();
|
this.limit = annotation.limit();
|
||||||
|
|
||||||
|
|||||||
@ -29,8 +29,7 @@ class ToManyRelation<SelfEntity> extends AbstractRelation<SelfEntity> {
|
|||||||
protected FieldWrapper mapKeyFieldWrapper;
|
protected FieldWrapper mapKeyFieldWrapper;
|
||||||
protected String orderBy;
|
protected String orderBy;
|
||||||
protected long limit = 0;
|
protected long limit = 0;
|
||||||
protected String splitBy;
|
protected String selfFieldSplitBy;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public ToManyRelation(String selfField, String targetSchema, String targetTable, String targetField, String valueField,
|
public ToManyRelation(String selfField, String targetSchema, String targetTable, String targetField, String valueField,
|
||||||
@ -52,16 +51,16 @@ class ToManyRelation<SelfEntity> extends AbstractRelation<SelfEntity> {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public QueryWrapper buildQueryWrapper(Set<Object> targetValues) {
|
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<>();
|
Set<Object> newTargetValues = new HashSet<>();
|
||||||
for (Object targetValue : targetValues) {
|
for (Object targetValue : targetValues) {
|
||||||
if (targetValue == null) {
|
if (targetValue == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (!(targetValue instanceof String)) {
|
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) {
|
for (String splitValue : splitValues) {
|
||||||
//优化分割后的数据类型(防止在数据库查询时候出现隐式转换)
|
//优化分割后的数据类型(防止在数据库查询时候出现隐式转换)
|
||||||
newTargetValues.add(ConvertUtil.convert(splitValue, targetFieldWrapper.getFieldType()));
|
newTargetValues.add(ConvertUtil.convert(splitValue, targetFieldWrapper.getFieldType()));
|
||||||
@ -102,8 +101,8 @@ class ToManyRelation<SelfEntity> extends AbstractRelation<SelfEntity> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (StringUtil.isNotBlank(splitBy)) {
|
if (StringUtil.isNotBlank(selfFieldSplitBy)) {
|
||||||
String[] splitValues = ((String) selfValue).split(splitBy);
|
String[] splitValues = ((String) selfValue).split(selfFieldSplitBy);
|
||||||
targetMappingValues.addAll(Arrays.asList(splitValues));
|
targetMappingValues.addAll(Arrays.asList(splitValues));
|
||||||
} else {
|
} else {
|
||||||
targetMappingValues.add((String) selfValue);
|
targetMappingValues.add((String) selfValue);
|
||||||
|
|||||||
@ -41,7 +41,7 @@ public class PatientVO1 implements Serializable {
|
|||||||
|
|
||||||
@RelationOneToMany(
|
@RelationOneToMany(
|
||||||
selfField = "diseaseIds",
|
selfField = "diseaseIds",
|
||||||
splitBy = ",", //使用 , 进行分割
|
selfFieldSplitBy = ",", //使用 , 进行分割
|
||||||
targetTable = "tb_disease", //只获取某个字段值需要填入目标表名
|
targetTable = "tb_disease", //只获取某个字段值需要填入目标表名
|
||||||
targetField = "diseaseId", //测试目标字段是字符串类型是否正常转换
|
targetField = "diseaseId", //测试目标字段是字符串类型是否正常转换
|
||||||
valueField = "name" //测试只获取某个字段值是否正常
|
valueField = "name" //测试只获取某个字段值是否正常
|
||||||
@ -50,14 +50,14 @@ public class PatientVO1 implements Serializable {
|
|||||||
|
|
||||||
@RelationOneToMany(
|
@RelationOneToMany(
|
||||||
selfField = "tagIds",
|
selfField = "tagIds",
|
||||||
splitBy = "/", //使用 / 进行分割
|
selfFieldSplitBy = "/", //使用 / 进行分割
|
||||||
targetField = "tagId" //测试目标字段是数字类型是否正常转换
|
targetField = "tagId" //测试目标字段是数字类型是否正常转换
|
||||||
)
|
)
|
||||||
private List<Tag> tagList;
|
private List<Tag> tagList;
|
||||||
|
|
||||||
@RelationOneToMany(
|
@RelationOneToMany(
|
||||||
selfField = "diseaseIds",
|
selfField = "diseaseIds",
|
||||||
splitBy = ",", //使用 , 进行分割
|
selfFieldSplitBy = ",", //使用 , 进行分割
|
||||||
targetField = "diseaseId", //测试目标字段是字符串类型是否正常转换
|
targetField = "diseaseId", //测试目标字段是字符串类型是否正常转换
|
||||||
mapKeyField = "diseaseId" //测试Map映射
|
mapKeyField = "diseaseId" //测试Map映射
|
||||||
)
|
)
|
||||||
|
|||||||
@ -1,13 +1,9 @@
|
|||||||
package com.mybatisflex.test.mapper;
|
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.junit.jupiter.api.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
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")
|
@SuppressWarnings("all")
|
||||||
public class PatientMapperTest {
|
public class PatientMapperTest {
|
||||||
|
|
||||||
@Autowired
|
@Resource
|
||||||
private PatientMapper patientMapper;
|
private PatientMapper patientMapper;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRelationOneToManySplitBy() {
|
public void testRelationOneToManySplitBy() {
|
||||||
PatientVO1 patientVO1 = patientMapper.selectOneWithRelationsByQueryAs(QueryWrapper.create().orderBy(PatientVO1::getPatientId, false).limit(1), PatientVO1.class);
|
// QueryWrapper wrapper = QueryWrapper.create().orderBy(PatientVO1::getPatientId, false).limit(1);
|
||||||
System.out.println(JSON.toJSONString(patientVO1));
|
// PatientVO1 patientVO1 = patientMapper.selectOneWithRelationsByQueryAs(wrapper, PatientVO1.class);
|
||||||
|
// System.out.println(JSON.toJSONString(patientVO1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user