mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 17:48:25 +08:00
feat:Relation注解新增targetFieldBind属性,当不为空串时值进行某个字段赋值
test:增加Relation注解单字段赋值Springboot测试
This commit is contained in:
parent
6f3f91460a
commit
39d8375e44
@ -67,6 +67,14 @@ public @interface RelationManyToMany {
|
|||||||
*/
|
*/
|
||||||
String targetField() default "";
|
String targetField() default "";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 目标实体类的字段绑定
|
||||||
|
* <p>
|
||||||
|
* 当字段不为空串时,只进行某个字段赋值(使用对应字段类型接收)
|
||||||
|
* @return 属性名称
|
||||||
|
*/
|
||||||
|
String targetFieldBind() default "";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 当映射是一个 map 时,使用哪个内容来当做 map 的 Key
|
* 当映射是一个 map 时,使用哪个内容来当做 map 的 Key
|
||||||
* @return 指定的列
|
* @return 指定的列
|
||||||
|
|||||||
@ -67,6 +67,14 @@ public @interface RelationManyToOne {
|
|||||||
*/
|
*/
|
||||||
String targetField() default "";
|
String targetField() default "";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 目标实体类的字段绑定
|
||||||
|
* <p>
|
||||||
|
* 当字段不为空串时,只进行某个字段赋值(使用对应字段类型接收)
|
||||||
|
* @return 属性名称
|
||||||
|
*/
|
||||||
|
String targetFieldBind() default "";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 中间表名称,一对一的关系是通过通过中间表维护时,需要添加此项配置。
|
* 中间表名称,一对一的关系是通过通过中间表维护时,需要添加此项配置。
|
||||||
*
|
*
|
||||||
|
|||||||
@ -67,6 +67,14 @@ public @interface RelationOneToMany {
|
|||||||
*/
|
*/
|
||||||
String targetField();
|
String targetField();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 目标实体类的字段绑定
|
||||||
|
* <p>
|
||||||
|
* 当字段不为空串时,只进行某个字段赋值(使用对应字段类型接收)
|
||||||
|
* @return 属性名称
|
||||||
|
*/
|
||||||
|
String targetFieldBind() default "";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 当映射是一个 map 时,使用哪个内容来当做 map 的 Key
|
* 当映射是一个 map 时,使用哪个内容来当做 map 的 Key
|
||||||
* @return 指定的列
|
* @return 指定的列
|
||||||
|
|||||||
@ -67,6 +67,14 @@ public @interface RelationOneToOne {
|
|||||||
*/
|
*/
|
||||||
String targetField();
|
String targetField();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 目标实体类的字段绑定
|
||||||
|
* <p>
|
||||||
|
* 当字段不为空串时,只进行某个字段赋值(使用对应字段类型接收)
|
||||||
|
* @return 属性名称
|
||||||
|
*/
|
||||||
|
String targetFieldBind() default "";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 中间表名称,一对一的关系是通过通过中间表维护时,需要添加此项配置。
|
* 中间表名称,一对一的关系是通过通过中间表维护时,需要添加此项配置。
|
||||||
*
|
*
|
||||||
|
|||||||
@ -46,6 +46,8 @@ abstract class AbstractRelation<SelfEntity> {
|
|||||||
protected String targetSchema;
|
protected String targetSchema;
|
||||||
protected String targetTable;
|
protected String targetTable;
|
||||||
protected Field targetField;
|
protected Field targetField;
|
||||||
|
protected String targetFieldBind;
|
||||||
|
protected boolean onlyTargetFieldBind;
|
||||||
protected Class<?> targetEntityClass;
|
protected Class<?> targetEntityClass;
|
||||||
protected TableInfo targetTableInfo;
|
protected TableInfo targetTableInfo;
|
||||||
protected FieldWrapper targetFieldWrapper;
|
protected FieldWrapper targetFieldWrapper;
|
||||||
@ -62,7 +64,7 @@ abstract class AbstractRelation<SelfEntity> {
|
|||||||
protected QueryColumn conditionColumn;
|
protected QueryColumn conditionColumn;
|
||||||
protected String[] selectColumns;
|
protected String[] selectColumns;
|
||||||
|
|
||||||
public AbstractRelation(String selfField, String targetSchema, String targetTable, String targetField,
|
public AbstractRelation(String selfField, String targetSchema, String targetTable, String targetField, String targetFieldBind,
|
||||||
String joinTable, String joinSelfColumn, String joinTargetColumn,
|
String joinTable, String joinSelfColumn, String joinTargetColumn,
|
||||||
String dataSource, Class<SelfEntity> entityClass, Field relationField,
|
String dataSource, Class<SelfEntity> entityClass, Field relationField,
|
||||||
String extraCondition, String[] selectColumns
|
String extraCondition, String[] selectColumns
|
||||||
@ -82,18 +84,24 @@ abstract class AbstractRelation<SelfEntity> {
|
|||||||
this.selfField = ClassUtil.getFirstField(entityClass, field -> field.getName().equals(selfField));
|
this.selfField = ClassUtil.getFirstField(entityClass, field -> field.getName().equals(selfField));
|
||||||
this.selfFieldWrapper = FieldWrapper.of(entityClass, selfField);
|
this.selfFieldWrapper = FieldWrapper.of(entityClass, selfField);
|
||||||
|
|
||||||
|
//以使用者注解配置为主
|
||||||
this.targetEntityClass = relationFieldWrapper.getMappingType();
|
this.targetTableInfo = StringUtil.isBlank(targetTable) ? TableInfoFactory.ofEntityClass(relationFieldWrapper.getMappingType()) : TableInfoFactory.ofTableName(targetTable);
|
||||||
|
this.targetEntityClass = targetTableInfo != null ? targetTableInfo.getEntityClass() : relationFieldWrapper.getMappingType();
|
||||||
this.targetSchema = targetSchema;
|
this.targetSchema = targetSchema;
|
||||||
this.targetTable = targetTable;
|
this.targetTable = targetTableInfo != null ? targetTableInfo.getTableName() : targetTable;
|
||||||
|
|
||||||
this.targetField = ClassUtil.getFirstField(targetEntityClass, field -> field.getName().equals(targetField));
|
this.targetField = ClassUtil.getFirstField(targetEntityClass, field -> field.getName().equals(targetField));
|
||||||
this.targetFieldWrapper = FieldWrapper.of(targetEntityClass, targetField);
|
this.targetFieldWrapper = FieldWrapper.of(targetEntityClass, targetField);
|
||||||
|
|
||||||
this.targetTableInfo = TableInfoFactory.ofEntityClass(targetEntityClass);
|
this.targetFieldBind = targetFieldBind;
|
||||||
|
this.onlyTargetFieldBind = StringUtil.isNotBlank(targetFieldBind);
|
||||||
|
|
||||||
this.conditionColumn = column(targetTable, targetTableInfo.getColumnByProperty(this.targetField.getName()));
|
this.conditionColumn = column(targetTable, targetTableInfo.getColumnByProperty(this.targetField.getName()));
|
||||||
|
|
||||||
|
if (onlyTargetFieldBind) {
|
||||||
|
//仅绑定字段时只需要查询关联列和该字段列即可
|
||||||
|
this.selectColumns = new String[]{conditionColumn.getName(), targetTableInfo != null ? targetTableInfo.getColumnByProperty(this.targetFieldBind) : StringUtil.camelToUnderline(this.targetFieldBind)};
|
||||||
|
} else {
|
||||||
if (ArrayUtil.isNotEmpty(selectColumns)) {
|
if (ArrayUtil.isNotEmpty(selectColumns)) {
|
||||||
if (ArrayUtil.contains(selectColumns, conditionColumn.getName())) {
|
if (ArrayUtil.contains(selectColumns, conditionColumn.getName())) {
|
||||||
this.selectColumns = selectColumns;
|
this.selectColumns = selectColumns;
|
||||||
@ -103,6 +111,8 @@ abstract class AbstractRelation<SelfEntity> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
initExtraCondition(extraCondition);
|
initExtraCondition(extraCondition);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -249,6 +259,22 @@ abstract class AbstractRelation<SelfEntity> {
|
|||||||
this.targetTable = targetTable;
|
this.targetTable = targetTable;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getTargetFieldBind() {
|
||||||
|
return targetFieldBind;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTargetFieldBind(String targetFieldBind) {
|
||||||
|
this.targetFieldBind = targetFieldBind;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isOnlyTargetFieldBind() {
|
||||||
|
return onlyTargetFieldBind;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOnlyTargetFieldBind(boolean onlyTargetFieldBind) {
|
||||||
|
this.onlyTargetFieldBind = onlyTargetFieldBind;
|
||||||
|
}
|
||||||
|
|
||||||
public String getJoinTable() {
|
public String getJoinTable() {
|
||||||
return joinTable;
|
return joinTable;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -26,6 +26,7 @@ class ManyToMany<SelfEntity> extends ToManyRelation<SelfEntity> {
|
|||||||
, annotation.targetSchema()
|
, annotation.targetSchema()
|
||||||
, annotation.targetTable()
|
, annotation.targetTable()
|
||||||
, getDefaultPrimaryProperty(annotation.targetField(), getTargetEntityClass(entityClass, relationField), "@RelationManyToMany.targetField can not be empty in field: \"" + entityClass.getName() + "." + relationField.getName() + "\"")
|
, getDefaultPrimaryProperty(annotation.targetField(), getTargetEntityClass(entityClass, relationField), "@RelationManyToMany.targetField can not be empty in field: \"" + entityClass.getName() + "." + relationField.getName() + "\"")
|
||||||
|
, annotation.targetFieldBind()
|
||||||
, annotation.joinTable()
|
, annotation.joinTable()
|
||||||
, annotation.joinSelfColumn()
|
, annotation.joinSelfColumn()
|
||||||
, annotation.joinTargetColumn()
|
, annotation.joinTargetColumn()
|
||||||
|
|||||||
@ -27,6 +27,7 @@ class ManyToOne<SelfEntity> extends ToOneRelation<SelfEntity> {
|
|||||||
, annotation.targetTable()
|
, annotation.targetTable()
|
||||||
, getDefaultPrimaryProperty(annotation.targetField(), getTargetEntityClass(entityClass, relationField)
|
, getDefaultPrimaryProperty(annotation.targetField(), getTargetEntityClass(entityClass, relationField)
|
||||||
, "@RelationManyToOne.selfField can not be empty in field: \"" + entityClass.getName() + "." + relationField.getName() + "\"")
|
, "@RelationManyToOne.selfField can not be empty in field: \"" + entityClass.getName() + "." + relationField.getName() + "\"")
|
||||||
|
, annotation.targetFieldBind()
|
||||||
, annotation.joinTable()
|
, annotation.joinTable()
|
||||||
, annotation.joinSelfColumn()
|
, annotation.joinSelfColumn()
|
||||||
, annotation.joinTargetColumn()
|
, annotation.joinTargetColumn()
|
||||||
|
|||||||
@ -27,6 +27,7 @@ class OneToMany<SelfEntity> extends ToManyRelation<SelfEntity> {
|
|||||||
, annotation.targetSchema()
|
, annotation.targetSchema()
|
||||||
, annotation.targetTable()
|
, annotation.targetTable()
|
||||||
, annotation.targetField()
|
, annotation.targetField()
|
||||||
|
, annotation.targetFieldBind()
|
||||||
, annotation.joinTable()
|
, annotation.joinTable()
|
||||||
, annotation.joinSelfColumn()
|
, annotation.joinSelfColumn()
|
||||||
, annotation.joinTargetColumn()
|
, annotation.joinTargetColumn()
|
||||||
|
|||||||
@ -27,6 +27,7 @@ class OneToOne<SelfEntity> extends ToOneRelation<SelfEntity> {
|
|||||||
, annotation.targetSchema()
|
, annotation.targetSchema()
|
||||||
, annotation.targetTable()
|
, annotation.targetTable()
|
||||||
, annotation.targetField()
|
, annotation.targetField()
|
||||||
|
, annotation.targetFieldBind()
|
||||||
, annotation.joinTable()
|
, annotation.joinTable()
|
||||||
, annotation.joinSelfColumn()
|
, annotation.joinSelfColumn()
|
||||||
, annotation.joinTargetColumn()
|
, annotation.joinTargetColumn()
|
||||||
|
|||||||
@ -359,8 +359,9 @@ public class RelationManager {
|
|||||||
DataSourceKey.use(configDsKey);
|
DataSourceKey.use(configDsKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//仅绑定字段:As目标实体类 不进行字段绑定:As映射类型
|
||||||
QueryWrapper queryWrapper = relation.buildQueryWrapper(targetValues);
|
QueryWrapper queryWrapper = relation.buildQueryWrapper(targetValues);
|
||||||
List<?> targetObjectList = mapper.selectListByQueryAs(queryWrapper, relation.getMappingType());
|
List<?> targetObjectList = mapper.selectListByQueryAs(queryWrapper, relation.isOnlyTargetFieldBind() ? relation.getTargetEntityClass() : relation.getMappingType());
|
||||||
if (CollectionUtil.isNotEmpty(targetObjectList)) {
|
if (CollectionUtil.isNotEmpty(targetObjectList)) {
|
||||||
|
|
||||||
//递归查询
|
//递归查询
|
||||||
|
|||||||
@ -31,11 +31,11 @@ class ToManyRelation<SelfEntity> extends AbstractRelation<SelfEntity> {
|
|||||||
protected long limit = 0;
|
protected long limit = 0;
|
||||||
|
|
||||||
|
|
||||||
public ToManyRelation(String selfField, String targetSchema, String targetTable, String targetField,
|
public ToManyRelation(String selfField, String targetSchema, String targetTable, String targetField, String targetFieldBind,
|
||||||
String joinTable, String joinSelfColumn, String joinTargetColumn,
|
String joinTable, String joinSelfColumn, String joinTargetColumn,
|
||||||
String dataSource, Class<SelfEntity> selfEntityClass, Field relationField,
|
String dataSource, Class<SelfEntity> selfEntityClass, Field relationField,
|
||||||
String extraCondition, String[] selectColumns) {
|
String extraCondition, String[] selectColumns) {
|
||||||
super(selfField, targetSchema, targetTable, targetField,
|
super(selfField, targetSchema, targetTable, targetField, targetFieldBind,
|
||||||
joinTable, joinSelfColumn, joinTargetColumn,
|
joinTable, joinSelfColumn, joinTargetColumn,
|
||||||
dataSource, selfEntityClass, relationField,
|
dataSource, selfEntityClass, relationField,
|
||||||
extraCondition, selectColumns
|
extraCondition, selectColumns
|
||||||
@ -101,9 +101,14 @@ class ToManyRelation<SelfEntity> extends AbstractRelation<SelfEntity> {
|
|||||||
for (Object targetObject : targetObjectList) {
|
for (Object targetObject : targetObjectList) {
|
||||||
Object targetValue = targetFieldWrapper.get(targetObject);
|
Object targetValue = targetFieldWrapper.get(targetObject);
|
||||||
if (targetValue != null && targetMappingValues.contains(targetValue.toString())) {
|
if (targetValue != null && targetMappingValues.contains(targetValue.toString())) {
|
||||||
|
if (onlyTargetFieldBind) {
|
||||||
|
//仅绑定某个字段
|
||||||
|
collection.add(FieldWrapper.of(targetObject.getClass(), targetFieldBind).get(targetObject));
|
||||||
|
} else {
|
||||||
collection.add(targetObject);
|
collection.add(targetObject);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
relationFieldWrapper.set(collection, selfEntity);
|
relationFieldWrapper.set(collection, selfEntity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -16,6 +16,7 @@
|
|||||||
package com.mybatisflex.core.relation;
|
package com.mybatisflex.core.relation;
|
||||||
|
|
||||||
import com.mybatisflex.core.row.Row;
|
import com.mybatisflex.core.row.Row;
|
||||||
|
import com.mybatisflex.core.util.FieldWrapper;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -23,10 +24,10 @@ import java.util.List;
|
|||||||
class ToOneRelation<SelfEntity> extends AbstractRelation<SelfEntity> {
|
class ToOneRelation<SelfEntity> extends AbstractRelation<SelfEntity> {
|
||||||
|
|
||||||
|
|
||||||
public ToOneRelation(String selfField, String targetSchema, String targetTable, String targetField,
|
public ToOneRelation(String selfField, String targetSchema, String targetTable, String targetField, String targetFieldBind,
|
||||||
String joinTable, String joinSelfColumn, String joinTargetColumn,
|
String joinTable, String joinSelfColumn, String joinTargetColumn,
|
||||||
String dataSource, Class<SelfEntity> selfEntityClass, Field relationField, String[] selectColumns) {
|
String dataSource, Class<SelfEntity> selfEntityClass, Field relationField, String[] selectColumns) {
|
||||||
super(selfField, targetSchema, targetTable, targetField,
|
super(selfField, targetSchema, targetTable, targetField, targetFieldBind,
|
||||||
joinTable, joinSelfColumn, joinTargetColumn,
|
joinTable, joinSelfColumn, joinTargetColumn,
|
||||||
dataSource, selfEntityClass, relationField,
|
dataSource, selfEntityClass, relationField,
|
||||||
null, selectColumns
|
null, selectColumns
|
||||||
@ -53,7 +54,12 @@ class ToOneRelation<SelfEntity> extends AbstractRelation<SelfEntity> {
|
|||||||
for (Object targetObject : targetObjectList) {
|
for (Object targetObject : targetObjectList) {
|
||||||
Object targetValue = targetFieldWrapper.get(targetObject);
|
Object targetValue = targetFieldWrapper.get(targetObject);
|
||||||
if (targetValue != null && targetMappingValue.equals(targetValue.toString())) {
|
if (targetValue != null && targetMappingValue.equals(targetValue.toString())) {
|
||||||
|
if (onlyTargetFieldBind) {
|
||||||
|
//仅绑定某个字段
|
||||||
|
relationFieldWrapper.set(FieldWrapper.of(targetObject.getClass(), targetFieldBind).get(targetObject), selfEntity);
|
||||||
|
} else {
|
||||||
relationFieldWrapper.set(targetObject, selfEntity);
|
relationFieldWrapper.set(targetObject, selfEntity);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,96 @@
|
|||||||
|
package com.mybatisflex.test.model;
|
||||||
|
|
||||||
|
import com.mybatisflex.annotation.*;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 字段绑定测试
|
||||||
|
* @author Ice 2023/09/16
|
||||||
|
* @version 1.0
|
||||||
|
*/
|
||||||
|
@Table("tb_user")
|
||||||
|
public class UserVO5 implements Serializable {
|
||||||
|
private static final long serialVersionUID = 474700189859144273L;
|
||||||
|
|
||||||
|
@Id
|
||||||
|
private Integer userId;
|
||||||
|
private String userName;
|
||||||
|
private String password;
|
||||||
|
|
||||||
|
@RelationOneToOne(
|
||||||
|
selfField = "userId",
|
||||||
|
targetTable = "tb_id_card",
|
||||||
|
targetField = "id",
|
||||||
|
targetFieldBind = "idNumber"
|
||||||
|
)
|
||||||
|
private String idNumberCustomFieldName;
|
||||||
|
|
||||||
|
@RelationOneToMany(
|
||||||
|
selfField = "userId",
|
||||||
|
targetTable = "tb_user_order",
|
||||||
|
targetField = "userId",
|
||||||
|
targetFieldBind = "orderId"
|
||||||
|
)
|
||||||
|
private List<Integer> orderIdList;
|
||||||
|
|
||||||
|
@RelationManyToMany(
|
||||||
|
selfField = "userId",
|
||||||
|
targetTable = "tb_role",
|
||||||
|
targetField = "roleId",
|
||||||
|
targetFieldBind = "roleName",
|
||||||
|
joinTable = "tb_user_role",
|
||||||
|
joinSelfColumn = "user_id",
|
||||||
|
joinTargetColumn = "role_id"
|
||||||
|
)
|
||||||
|
private List<String> roleNameList;
|
||||||
|
|
||||||
|
public Integer getUserId() {
|
||||||
|
return userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserId(Integer userId) {
|
||||||
|
this.userId = userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUserName() {
|
||||||
|
return userName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserName(String userName) {
|
||||||
|
this.userName = userName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPassword() {
|
||||||
|
return password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPassword(String password) {
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getIdNumberCustomFieldName() {
|
||||||
|
return idNumberCustomFieldName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIdNumberCustomFieldName(String idNumberCustomFieldName) {
|
||||||
|
this.idNumberCustomFieldName = idNumberCustomFieldName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Integer> getOrderIdList() {
|
||||||
|
return orderIdList;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOrderIdList(List<Integer> orderIdList) {
|
||||||
|
this.orderIdList = orderIdList;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getRoleNameList() {
|
||||||
|
return roleNameList;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRoleNameList(List<String> roleNameList) {
|
||||||
|
this.roleNameList = roleNameList;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -280,4 +280,10 @@ class UserMapperTest {
|
|||||||
System.err.println(user);
|
System.err.println(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFieldBindRelations() {
|
||||||
|
List<UserVO5> userVO5List = userMapper.selectListWithRelationsByQueryAs(QueryWrapper.create(), UserVO5.class);
|
||||||
|
System.out.println(userVO5List);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user