feat: Relation 所有注解支持通过中间表进行 join 的场景

This commit is contained in:
开源海哥 2023-07-12 19:38:16 +08:00
parent b5c0426768
commit ba0ce49d50
16 changed files with 480 additions and 211 deletions

View File

@ -35,14 +35,14 @@ public @interface RelationManyToMany {
String selfField() default "";
/**
* 目标实体类对应的表的 schema一般关联数据不是 entity而是 vodto 等需要配置此项
* 目标实体类对应的表的 schema一般情况下关联数据不是 entity而是 vodto 等需要配置此项
*
* @return schema 名称
*/
String targetSchema() default "";
/**
* 目标实体类对应的表一般关联数据不是 entity而是 vodto 等需要配置此项
* 目标实体类对应的表一般情况下关联数据不是 entity而是 vodto 等需要配置此项
*
* @return 表名
*/

View File

@ -35,14 +35,14 @@ public @interface RelationManyToOne {
String selfField();
/**
* 目标实体类对应的表的 schema一般关联数据不是 entity而是 vodto 等需要配置此项
* 目标实体类对应的表的 schema一般情况下关联数据不是 entity而是 vodto 等需要配置此项
*
* @return schema 名称
*/
String targetSchema() default "";
/**
* 目标实体类对应的表一般关联数据不是 entity而是 vodto 等需要配置此项
* 目标实体类对应的表一般情况下关联数据不是 entity而是 vodto 等需要配置此项
*
* @return 表名
*/
@ -55,6 +55,27 @@ public @interface RelationManyToOne {
*/
String targetField() default "";
/**
* 中间表名称一对一的关系是通过通过中间表维护时需要添加此项配置
*
* @return 中间表名称
*/
String joinTable() default "";
/**
* 中间表与当前表的关联字段一对一的关系是通过通过中间表维护时需要添加此项配置
*
* @return 字段名称列名
*/
String joinSelfColumn() default "";
/**
* 目标表的关联字段名称一对一的关系是通过通过中间表维护时需要添加此项配置
*
* @return 字段名称和表
*/
String joinTargetColumn() default "";
/**
* 默认使用哪个数据源若系统找不到该指定的数据源时默认使用第一个数据源
*

View File

@ -35,14 +35,14 @@ public @interface RelationOneToMany {
String selfField() default "";
/**
* 目标实体类对应的表的 schema一般关联数据不是 entity而是 vodto 等需要配置此项
* 目标实体类对应的表的 schema一般情况下关联数据不是 entity而是 vodto 等需要配置此项
*
* @return schema 名称
*/
String targetSchema() default "";
/**
* 目标实体类对应的表一般关联数据不是 entity而是 vodto 等需要配置此项
* 目标实体类对应的表一般情况下关联数据不是 entity而是 vodto 等需要配置此项
*
* @return 表名
*/
@ -55,6 +55,27 @@ public @interface RelationOneToMany {
*/
String targetField();
/**
* 中间表名称一对一的关系是通过通过中间表维护时需要添加此项配置
*
* @return 中间表名称
*/
String joinTable() default "";
/**
* 中间表与当前表的关联字段一对一的关系是通过通过中间表维护时需要添加此项配置
*
* @return 字段名称列名
*/
String joinSelfColumn() default "";
/**
* 目标表的关联字段名称一对一的关系是通过通过中间表维护时需要添加此项配置
*
* @return 字段名称和表
*/
String joinTargetColumn() default "";
/**
* 查询排序
*

View File

@ -35,14 +35,14 @@ public @interface RelationOneToOne {
String selfField() default "";
/**
* 目标实体类对应的表的 schema一般关联数据不是 entity而是 vodto 等需要配置此项
* 目标实体类对应的表的 schema一般情况下关联数据不是 entity而是 vodto 等需要配置此项
*
* @return schema 名称
*/
String targetSchema() default "";
/**
* 目标实体类对应的表一般关联数据不是 entity而是 vodto 等需要配置此项
* 目标实体类对应的表一般情况下关联数据不是 entity而是 vodto 等需要配置此项
*
* @return 表名
*/
@ -55,6 +55,27 @@ public @interface RelationOneToOne {
*/
String targetField();
/**
* 中间表名称一对一的关系是通过通过中间表维护时需要添加此项配置
*
* @return 中间表名称
*/
String joinTable() default "";
/**
* 中间表与当前表的关联字段一对一的关系是通过通过中间表维护时需要添加此项配置
*
* @return 字段名称列名
*/
String joinSelfColumn() default "";
/**
* 目标表的关联字段名称一对一的关系是通过通过中间表维护时需要添加此项配置
*
* @return 字段名称和表
*/
String joinTargetColumn() default "";
/**
* 默认使用哪个数据源若系统找不到该指定的数据源时默认使用第一个数据源
*

View File

@ -15,9 +15,9 @@
*/
package com.mybatisflex.core.relation;
import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.exception.FlexExceptions;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.row.Row;
import com.mybatisflex.core.table.IdInfo;
import com.mybatisflex.core.table.TableInfo;
import com.mybatisflex.core.table.TableInfoFactory;
@ -31,6 +31,8 @@ import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import static com.mybatisflex.core.query.QueryMethods.column;
abstract class AbstractRelation<SelfEntity> {
protected Class<SelfEntity> selfEntityClass;
@ -47,14 +49,23 @@ abstract class AbstractRelation<SelfEntity> {
protected TableInfo targetTableInfo;
protected FieldWrapper targetFieldWrapper;
protected String joinTable;
protected String joinSelfColumn;
protected String joinTargetColumn;
protected String dataSource;
public AbstractRelation(String selfField, String targetSchema, String targetTable, String targetField,
String joinTable, String joinSelfColumn, String joinTargetColumn,
String dataSource, Class<SelfEntity> entityClass, Field relationField) {
this.selfEntityClass = entityClass;
this.relationField = relationField;
this.relationFieldWrapper = FieldWrapper.of(entityClass, relationField.getName());
this.joinTable = joinTable;
this.joinSelfColumn = joinSelfColumn;
this.joinTargetColumn = joinTargetColumn;
this.dataSource = dataSource;
this.selfField = ClassUtil.getFirstField(entityClass, field -> field.getName().equals(selfField));
@ -144,13 +155,52 @@ abstract class AbstractRelation<SelfEntity> {
this.targetFieldWrapper = targetFieldWrapper;
}
protected Set<Object> getSelfFieldValues(List<SelfEntity> list) {
if (list == null || list.isEmpty()) {
return Collections.emptySet();
public String getTargetSchema() {
return targetSchema;
}
public void setTargetSchema(String targetSchema) {
this.targetSchema = targetSchema;
}
public String getTargetTable() {
return targetTable;
}
public void setTargetTable(String targetTable) {
this.targetTable = targetTable;
}
public String getJoinTable() {
return joinTable;
}
public void setJoinTable(String joinTable) {
this.joinTable = joinTable;
}
public String getJoinSelfColumn() {
return joinSelfColumn;
}
public void setJoinSelfColumn(String joinSelfColumn) {
this.joinSelfColumn = joinSelfColumn;
}
public String getJoinTargetColumn() {
return joinTargetColumn;
}
public void setJoinTargetColumn(String joinTargetColumn) {
this.joinTargetColumn = joinTargetColumn;
}
public Set<Object> getSelfFieldValues(List<SelfEntity> selfEntities) {
if (selfEntities == null || selfEntities.isEmpty()) {
return Collections.emptySet();
}
Set<Object> values = new LinkedHashSet<>();
list.forEach(self -> {
selfEntities.forEach(self -> {
Object value = selfFieldWrapper.get(self);
if (value != null && !"".equals(value)) {
values.add(value);
@ -180,6 +230,10 @@ abstract class AbstractRelation<SelfEntity> {
}
}
protected boolean isRelationByMiddleTable() {
return StringUtil.isNotBlank(joinTable);
}
protected static Class<?> getTargetEntityClass(Class<?> entityClass, Field relationField) {
return FieldWrapper.of(entityClass, relationField.getName()).getMappingType();
@ -201,21 +255,42 @@ abstract class AbstractRelation<SelfEntity> {
/**
* Relations 的配置转换为查询 QueryWrapper
* 构建查询目标对象 QueryWrapper
*
* @param selfEntities 当前的实体类
* @param targetValues 条件的值
* @return QueryWrapper
*/
public abstract QueryWrapper toQueryWrapper(List<SelfEntity> selfEntities);
public QueryWrapper buildQueryWrapper(Set<Object> targetValues) {
QueryWrapper queryWrapper = QueryWrapper.create()
.select()
.from(getTargetTableWithSchema());
if (targetValues.size() > 1) {
queryWrapper.where(column(targetTableInfo.getColumnByProperty(targetField.getName())).in(targetValues));
} else {
queryWrapper.where(column(targetTableInfo.getColumnByProperty(targetField.getName())).eq(targetValues.iterator().next()));
}
customizeQueryWrapper(queryWrapper);
return queryWrapper;
}
/**
* 通过 {@link AbstractRelation#toQueryWrapper(List)} 查询到的结果通过此方法进行内存 join
* 方便子类最近自定义的条件
*
* @param queryWrapper 查询条件
*/
public void customizeQueryWrapper(QueryWrapper queryWrapper) {
//do thing
}
/**
* @param selfEntities 当前的实体类列表
* @param targetObjectList 查询到的结果
* @param mapper 查询的 Mapper
* @param queriedClasses
* @param mappingRows 中间表的映射数据非中间表查询的场景下mappingRows 永远为 null
*/
public abstract void join(List<SelfEntity> selfEntities, List<?> targetObjectList, BaseMapper<?> mapper, Set<Class<?>> queriedClasses);
public abstract void join(List<SelfEntity> selfEntities, List<?> targetObjectList, List<Row> mappingRows);
}

View File

@ -16,24 +16,12 @@
package com.mybatisflex.core.relation;
import com.mybatisflex.annotation.RelationManyToMany;
import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.row.Row;
import com.mybatisflex.core.util.ClassUtil;
import com.mybatisflex.core.util.CollectionUtil;
import com.mybatisflex.core.util.MapperUtil;
import com.mybatisflex.core.util.StringUtil;
import java.lang.reflect.Field;
import java.util.*;
import static com.mybatisflex.core.query.QueryMethods.column;
class ManyToMany<SelfEntity> extends AbstractRelation<SelfEntity> {
private String joinTable;
private String joinSelfColumn;
private String joinTargetColumn;
class ManyToMany<SelfEntity> extends ToManyRelation<SelfEntity> {
private String orderBy;
@ -42,100 +30,19 @@ class ManyToMany<SelfEntity> extends AbstractRelation<SelfEntity> {
, annotation.targetSchema()
, annotation.targetTable()
, getDefaultPrimaryProperty(annotation.targetField(), getTargetEntityClass(entityClass, relationField), "@RelationManyToMany.targetField can not be empty in field: \"" + entityClass.getName() + "." + relationField.getName() + "\"")
, annotation.joinTable()
, annotation.joinSelfColumn()
, annotation.joinTargetColumn()
, annotation.dataSource(), entityClass, relationField);
this.joinTable = annotation.joinTable();
this.joinSelfColumn = annotation.joinSelfColumn();
this.joinTargetColumn = annotation.joinTargetColumn();
this.orderBy = annotation.orderBy();
}
@Override
public Class<?> getMappingType() {
return Row.class;
}
@Override
public QueryWrapper toQueryWrapper(List<SelfEntity> selfEntities) {
Set<Object> selfFieldValues = getSelfFieldValues(selfEntities);
if (selfFieldValues.isEmpty()) {
return null;
}
QueryWrapper queryWrapper = QueryWrapper.create().select()
.from(joinTable);
if (selfFieldValues.size() > 1) {
queryWrapper.where(column(joinSelfColumn).in(selfFieldValues));
} else {
queryWrapper.where(column(joinSelfColumn).eq(selfFieldValues.iterator().next()));
}
return queryWrapper;
}
@Override
public void join(List<SelfEntity> selfEntities, List<?> mappingObjectList, BaseMapper<?> mapper, Set<Class<?>> queriedClasses) {
List<Row> mappingRows = (List<Row>) mappingObjectList;
Set<Object> targetValues = new LinkedHashSet<>();
for (Row row : mappingRows) {
Object targetValue = row.getIgnoreCase(joinTargetColumn);
if (targetValue != null) {
targetValues.add(targetValue);
}
}
if (targetValues.isEmpty()) {
return;
}
QueryWrapper queryWrapper = QueryWrapper.create().select()
.from(getTargetTableWithSchema());
if (targetValues.size() > 1) {
queryWrapper.where(column(targetTableInfo.getColumnByProperty(targetField.getName())).in(targetValues));
} else {
queryWrapper.where(column(targetTableInfo.getColumnByProperty(targetField.getName())).eq(targetValues.iterator().next()));
}
public void customizeQueryWrapper(QueryWrapper queryWrapper) {
if (StringUtil.isNotBlank(orderBy)){
queryWrapper.orderBy(orderBy);
}
List<?> targetObjectList = mapper.selectListByQueryAs(queryWrapper, relationFieldWrapper.getMappingType());
RelationManager.doQueryRelations(mapper, targetObjectList, queriedClasses);
if (CollectionUtil.isNotEmpty(targetObjectList)) {
selfEntities.forEach(selfEntity -> {
Object selfValue = selfFieldWrapper.get(selfEntity);
if (selfValue != null) {
selfValue = selfValue.toString();
Set<String> targetMappingValues = new HashSet<>();
for (Row mappingRow : mappingRows) {
if (selfValue.equals(String.valueOf(mappingRow.getIgnoreCase(joinSelfColumn)))) {
Object joinValue = mappingRow.getIgnoreCase(joinTargetColumn);
if (joinValue != null) {
targetMappingValues.add(joinValue.toString());
}
}
}
if (!targetMappingValues.isEmpty()) {
Class<?> wrapType = MapperUtil.getWrapType(relationFieldWrapper.getFieldType());
Collection<Object> collection = (Collection) ClassUtil.newInstance(wrapType);
for (Object targetObject : targetObjectList) {
Object targetValue = targetFieldWrapper.get(targetObject);
if (targetValue != null && targetMappingValues.contains(targetValue.toString())) {
collection.add(targetObject);
}
}
relationFieldWrapper.set(collection, selfEntity);
}
}
});
}
}
}

View File

@ -27,6 +27,9 @@ class ManyToOne<SelfEntity> extends ToOneRelation<SelfEntity> {
, annotation.targetTable()
, getDefaultPrimaryProperty(annotation.targetField(), getTargetEntityClass(entityClass, relationField)
, "@RelationManyToOne.selfField can not be empty in field: \"" + entityClass.getName() + "." + relationField.getName() + "\"")
, annotation.joinTable()
, annotation.joinSelfColumn()
, annotation.joinTargetColumn()
, annotation.dataSource()
, entityClass
, relationField);

View File

@ -16,20 +16,12 @@
package com.mybatisflex.core.relation;
import com.mybatisflex.annotation.RelationOneToMany;
import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.util.ClassUtil;
import com.mybatisflex.core.util.MapperUtil;
import com.mybatisflex.core.util.StringUtil;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import static com.mybatisflex.core.query.QueryMethods.column;
class OneToMany<SelfEntity> extends AbstractRelation<SelfEntity> {
class OneToMany<SelfEntity> extends ToManyRelation<SelfEntity> {
private String orderBy;
private int limit;
@ -39,25 +31,18 @@ class OneToMany<SelfEntity> extends AbstractRelation<SelfEntity> {
super(getDefaultPrimaryProperty(annotation.selfField(), entityClass, "@RelationOneToMany.selfField can not be empty in field: \"" + entityClass.getName() + "." + relationField.getName() + "\"")
, annotation.targetSchema()
, annotation.targetTable()
, annotation.targetField(), annotation.dataSource(), entityClass, relationField);
, annotation.targetField()
, annotation.joinTable()
, annotation.joinSelfColumn()
, annotation.joinTargetColumn()
, annotation.dataSource(), entityClass, relationField);
this.orderBy = annotation.orderBy();
this.limit = annotation.limit();
}
@Override
public QueryWrapper toQueryWrapper(List<SelfEntity> selfEntities) {
Set<Object> selfFieldValues = getSelfFieldValues(selfEntities);
if (selfFieldValues.isEmpty()) {
return null;
}
QueryWrapper queryWrapper = QueryWrapper.create().select()
.from(getTargetTableWithSchema());
if (selfFieldValues.size() > 1) {
queryWrapper.where(column(targetTableInfo.getColumnByProperty(targetField.getName())).in(selfFieldValues));
} else {
queryWrapper.where(column(targetTableInfo.getColumnByProperty(targetField.getName())).eq(selfFieldValues.iterator().next()));
}
@Override
public void customizeQueryWrapper(QueryWrapper queryWrapper) {
if (StringUtil.isNotBlank(orderBy)){
queryWrapper.orderBy(orderBy);
}
@ -65,27 +50,6 @@ class OneToMany<SelfEntity> extends AbstractRelation<SelfEntity> {
if (limit > 0){
queryWrapper.limit(limit);
}
return queryWrapper;
}
@Override
public void join(List<SelfEntity> selfEntities, List<?> targetObjectList, BaseMapper<?> mapper, Set<Class<?>> queriedClasses) {
selfEntities.forEach(selfEntity -> {
Object selfValue = selfFieldWrapper.get(selfEntity);
if (selfValue != null) {
selfValue = selfValue.toString();
Class<?> wrapType = MapperUtil.getWrapType(relationFieldWrapper.getFieldType());
Collection<Object> collection = (Collection) ClassUtil.newInstance(wrapType);
for (Object targetObject : targetObjectList) {
Object targetValue = targetFieldWrapper.get(targetObject);
if (targetValue != null && selfValue.equals(targetValue.toString())) {
collection.add(targetObject);
}
}
relationFieldWrapper.set(collection, selfEntity);
}
});
}
}

View File

@ -27,6 +27,9 @@ class OneToOne<SelfEntity> extends ToOneRelation<SelfEntity> {
, annotation.targetSchema()
, annotation.targetTable()
, annotation.targetField()
, annotation.joinTable()
, annotation.joinSelfColumn()
, annotation.joinTargetColumn()
, annotation.dataSource()
, entityClass
, relationField);

View File

@ -32,6 +32,8 @@ import java.lang.reflect.Field;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import static com.mybatisflex.core.query.QueryMethods.column;
/**
* @author michael
*/
@ -98,8 +100,47 @@ public class RelationManager {
try {
relations.forEach(relation -> {
QueryWrapper queryWrapper = relation.toQueryWrapper(entities);
Class<?> mappingType = relation.getMappingType();
Class mappingType = relation.getMappingType();
if (queriedClasses.contains(mappingType)) {
return;
}
Set<Object> targetValues;
List<Row> mappingRows = null;
//通过中间表关联查询
if (relation.isRelationByMiddleTable()) {
targetValues = new HashSet<>();
Set selfFieldValues = relation.getSelfFieldValues(entities);
QueryWrapper queryWrapper = QueryWrapper.create().select()
.from(relation.getJoinTable());
if (selfFieldValues.size() > 1) {
queryWrapper.where(column(relation.getJoinSelfColumn()).in(selfFieldValues));
} else {
queryWrapper.where(column(relation.getJoinTargetColumn()).eq(selfFieldValues.iterator().next()));
}
mappingRows = mapper.selectListByQueryAs(queryWrapper, Row.class);
if (CollectionUtil.isEmpty(mappingRows)) {
return;
}
for (Row mappingData : mappingRows) {
Object targetValue = mappingData.getIgnoreCase(relation.getJoinTargetColumn());
if (targetValue != null) {
targetValues.add(targetValue);
}
}
}
//通过外键字段关联查询
else {
targetValues = relation.getSelfFieldValues(entities);
}
if (CollectionUtil.isEmpty(targetValues)) {
return;
}
String dataSource = relation.getDataSource();
if (StringUtil.isBlank(dataSource) && currentDsKey != null) {
@ -111,13 +152,11 @@ public class RelationManager {
DataSourceKey.use(dataSource);
}
QueryWrapper queryWrapper = relation.buildQueryWrapper(targetValues);
List<?> targetObjectList = mapper.selectListByQueryAs(queryWrapper, mappingType);
if (mappingType != Row.class) {
doQueryRelations(mapper, targetObjectList, queriedClasses);
}
if (CollectionUtil.isNotEmpty(targetObjectList)) {
relation.join(entities, targetObjectList, mapper, queriedClasses);
doQueryRelations(mapper, targetObjectList, queriedClasses);
relation.join(entities, targetObjectList, mappingRows);
}
} finally {
if (StringUtil.isNotBlank(dataSource)) {

View File

@ -0,0 +1,77 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.core.relation;
import com.mybatisflex.core.row.Row;
import com.mybatisflex.core.util.ClassUtil;
import com.mybatisflex.core.util.MapperUtil;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
class ToManyRelation<SelfEntity> extends AbstractRelation<SelfEntity> {
public ToManyRelation(String selfField, String targetSchema, String targetTable, String targetField,
String joinTable, String joinSelfColumn, String joinTargetColumn,
String dataSource, Class<SelfEntity> selfEntityClass, Field relationField) {
super(selfField, targetSchema, targetTable, targetField,
joinTable, joinSelfColumn, joinTargetColumn,
dataSource, selfEntityClass, relationField);
}
@SuppressWarnings("rawtypes")
@Override
public void join(List<SelfEntity> selfEntities, List<?> targetObjectList, List<Row> mappingRows) {
selfEntities.forEach(selfEntity -> {
Object selfValue = selfFieldWrapper.get(selfEntity);
if (selfValue != null) {
selfValue = selfValue.toString();
Set<String> targetMappingValues = new HashSet<>();
if (mappingRows != null) {
for (Row mappingRow : mappingRows) {
if (selfValue.equals(String.valueOf(mappingRow.getIgnoreCase(joinSelfColumn)))) {
Object joinValue = mappingRow.getIgnoreCase(joinTargetColumn);
if (joinValue != null) {
targetMappingValues.add(joinValue.toString());
}
}
}
} else {
targetMappingValues.add((String) selfValue);
}
if (targetMappingValues.isEmpty()) {
return;
}
Class<?> wrapType = MapperUtil.getWrapType(relationFieldWrapper.getFieldType());
Collection collection = (Collection) ClassUtil.newInstance(wrapType);
for (Object targetObject : targetObjectList) {
Object targetValue = targetFieldWrapper.get(targetObject);
if (targetValue != null && targetMappingValues.contains(targetValue.toString())) {
collection.add(targetObject);
}
}
relationFieldWrapper.set(collection, selfEntity);
}
});
}
}

View File

@ -15,50 +15,42 @@
*/
package com.mybatisflex.core.relation;
import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.row.Row;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Set;
import static com.mybatisflex.core.query.QueryMethods.column;
class ToOneRelation<SelfEntity> extends AbstractRelation<SelfEntity> {
public ToOneRelation(String selfField, String targetSchema, String targetTable, String targetField,
String joinTable, String joinSelfColumn, String joinTargetColumn,
String dataSource, Class<SelfEntity> selfEntityClass, Field relationField) {
super(selfField, targetSchema, targetTable, targetField, dataSource, selfEntityClass, relationField);
super(selfField, targetSchema, targetTable, targetField,
joinTable, joinSelfColumn, joinTargetColumn,
dataSource, selfEntityClass, relationField);
}
@Override
public QueryWrapper toQueryWrapper(List<SelfEntity> selfEntities) {
Set<Object> selfFieldValues = getSelfFieldValues(selfEntities);
if (selfFieldValues.isEmpty()) {
return null;
}
QueryWrapper queryWrapper = QueryWrapper.create().select()
.from(getTargetTableWithSchema());
if (selfFieldValues.size() > 1) {
queryWrapper.where(column(targetTableInfo.getColumnByProperty(targetField.getName())).in(selfFieldValues));
} else {
queryWrapper.where(column(targetTableInfo.getColumnByProperty(targetField.getName())).eq(selfFieldValues.iterator().next()));
}
return queryWrapper;
}
@Override
public void join(List<SelfEntity> selfEntities, List<?> targetObjectList, BaseMapper<?> mapper, Set<Class<?>> queriedClasses) {
public void join(List<SelfEntity> selfEntities, List<?> targetObjectList, List<Row> mappingRows) {
selfEntities.forEach(selfEntity -> {
Object selfValue = selfFieldWrapper.get(selfEntity);
if (selfValue != null) {
selfValue = selfValue.toString();
String targetMappingValue = null;
if (mappingRows != null) {
targetMappingValue = getTargetMappingValue(mappingRows, selfValue);
if (targetMappingValue == null) {
return;
}
} else {
targetMappingValue = (String) selfValue;
}
for (Object targetObject : targetObjectList) {
Object targetValue = targetFieldWrapper.get(targetObject);
if (targetValue != null && selfValue.equals(targetValue.toString())) {
if (targetValue != null && targetMappingValue.equals(targetValue.toString())) {
relationFieldWrapper.set(targetObject, selfEntity);
break;
}
@ -66,4 +58,17 @@ class ToOneRelation<SelfEntity> extends AbstractRelation<SelfEntity> {
}
});
}
private String getTargetMappingValue(List<Row> mappingRows, Object selfValue) {
for (Row mappingRow : mappingRows) {
if (selfValue.equals(String.valueOf(mappingRow.getIgnoreCase(joinSelfColumn)))) {
Object joinValue = mappingRow.getIgnoreCase(joinTargetColumn);
if (joinValue != null) {
return joinValue.toString();
}
}
}
return null;
}
}

View File

@ -32,7 +32,9 @@ public class Account implements Serializable {
private int age;
// @RelationOneToOne(selfField = "id", targetField = "accountId")
// @RelationOneToOne(targetField = "accountId")
@RelationOneToOne(targetField = "accountId")
// @RelationManyToOne(joinTable = "tb_idcard_mapping",joinSelfColumn = "account_id",joinTargetColumn = "idcard_id"
// ,selfField = "id",targetField = "accountId")
private IDCard idCard;
// @RelationOneToMany(selfField = "id", targetField = "accountId")
@ -44,11 +46,11 @@ public class Account implements Serializable {
// selfField = "id", joinSelfColumn = "account_id",
// targetField = "id", joinTargetColumn = "role_id"
// )
@RelationManyToMany(
joinTable = "tb_role_mapping",
joinSelfColumn = "account_id",
joinTargetColumn = "role_id"
)
// @RelationManyToMany(
// joinTable = "tb_role_mapping",
// joinSelfColumn = "account_id",
// joinTargetColumn = "role_id"
// )
private List<Role> roles;

View File

@ -0,0 +1,112 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.test.relation.onetoone;
import com.mybatisflex.annotation.RelationManyToMany;
import java.io.Serializable;
import java.util.List;
public class AccountDTO implements Serializable {
private Long id;
private String userName;
private int age;
// @RelationOneToOne(selfField = "id", targetField = "accountId")
// @RelationOneToOne(targetField = "accountId")
private IDCard idCard;
// @RelationOneToMany(selfField = "id", targetField = "accountId")
// @RelationOneToMany(targetField = "accountId")
private List<Book> books;
// @RelationManyToMany(
// joinTable = "tb_role_mapping",
// selfField = "id", joinSelfColumn = "account_id",
// targetField = "id", joinTargetColumn = "role_id"
// )
@RelationManyToMany(
joinTable = "tb_role_mapping",
joinSelfColumn = "account_id",
joinTargetColumn = "role_id"
)
private List<Role> roles;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public IDCard getIdCard() {
return idCard;
}
public void setIdCard(IDCard idCard) {
this.idCard = idCard;
}
public List<Book> getBooks() {
return books;
}
public void setBooks(List<Book> books) {
this.books = books;
}
public List<Role> getRoles() {
return roles;
}
public void setRoles(List<Role> roles) {
this.roles = roles;
}
@Override
public String toString() {
return "AccountDTO{" +
"id=" + id +
", userName='" + userName + '\'' +
", age=" + age +
", idCard=" + idCard +
", books=" + books +
", roles=" + roles +
'}';
}
}

View File

@ -13,6 +13,16 @@ VALUES (1,'0001', '内容1'),
(4,'0004', '内容4'),
(5,'0005', '内容5');
INSERT INTO tb_idcard_mapping
VALUES (1,1),
(2,2),
(3,3),
(4,4),
(5,5);
INSERT INTO tb_book
VALUES (1,1,'图书1', '内容1'),
(2,2,'图书2', '内容2'),

View File

@ -14,6 +14,15 @@ CREATE TABLE IF NOT EXISTS `tb_idcard`
);
CREATE TABLE IF NOT EXISTS `tb_idcard_mapping`
(
`account_id` Integer,
`idcard_id` Integer
);
CREATE TABLE IF NOT EXISTS `tb_book`
(
`id` INTEGER auto_increment,