mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 01:28:24 +08:00
feat: Relation 注解 targetSchema 和 targetTable 的支持。
This commit is contained in:
parent
677eabdbe5
commit
695a90562b
@ -34,6 +34,20 @@ public @interface RelationManyToMany {
|
||||
*/
|
||||
String selfField() default "";
|
||||
|
||||
/**
|
||||
* 目标实体类对应的表的 schema,一般关联数据不是 entity,而是 vo、dto 等需要配置此项
|
||||
*
|
||||
* @return schema 名称
|
||||
*/
|
||||
String targetSchema() default "";
|
||||
|
||||
/**
|
||||
* 目标实体类对应的表,一般关联数据不是 entity,而是 vo、dto 等需要配置此项
|
||||
*
|
||||
* @return 表名
|
||||
*/
|
||||
String targetTable() default "";
|
||||
|
||||
/**
|
||||
* 目标实体类的关联属性。
|
||||
*
|
||||
|
||||
@ -34,6 +34,20 @@ public @interface RelationManyToOne {
|
||||
*/
|
||||
String selfField();
|
||||
|
||||
/**
|
||||
* 目标实体类对应的表的 schema,一般关联数据不是 entity,而是 vo、dto 等需要配置此项
|
||||
*
|
||||
* @return schema 名称
|
||||
*/
|
||||
String targetSchema() default "";
|
||||
|
||||
/**
|
||||
* 目标实体类对应的表,一般关联数据不是 entity,而是 vo、dto 等需要配置此项
|
||||
*
|
||||
* @return 表名
|
||||
*/
|
||||
String targetTable() default "";
|
||||
|
||||
/**
|
||||
* 目标实体类的关联属性。
|
||||
*
|
||||
|
||||
@ -34,6 +34,20 @@ public @interface RelationOneToMany {
|
||||
*/
|
||||
String selfField() default "";
|
||||
|
||||
/**
|
||||
* 目标实体类对应的表的 schema,一般关联数据不是 entity,而是 vo、dto 等需要配置此项
|
||||
*
|
||||
* @return schema 名称
|
||||
*/
|
||||
String targetSchema() default "";
|
||||
|
||||
/**
|
||||
* 目标实体类对应的表,一般关联数据不是 entity,而是 vo、dto 等需要配置此项
|
||||
*
|
||||
* @return 表名
|
||||
*/
|
||||
String targetTable() default "";
|
||||
|
||||
/**
|
||||
* 目标实体类的关联属性。
|
||||
*
|
||||
|
||||
@ -34,6 +34,20 @@ public @interface RelationOneToOne {
|
||||
*/
|
||||
String selfField() default "";
|
||||
|
||||
/**
|
||||
* 目标实体类对应的表的 schema,一般关联数据不是 entity,而是 vo、dto 等需要配置此项
|
||||
*
|
||||
* @return schema 名称
|
||||
*/
|
||||
String targetSchema() default "";
|
||||
|
||||
/**
|
||||
* 目标实体类对应的表,一般关联数据不是 entity,而是 vo、dto 等需要配置此项
|
||||
*
|
||||
* @return 表名
|
||||
*/
|
||||
String targetTable() default "";
|
||||
|
||||
/**
|
||||
* 目标实体类的关联属性。
|
||||
*
|
||||
|
||||
@ -40,6 +40,8 @@ abstract class AbstractRelation<SelfEntity> {
|
||||
protected Field selfField;
|
||||
protected FieldWrapper selfFieldWrapper;
|
||||
|
||||
protected String targetSchema;
|
||||
protected String targetTable;
|
||||
protected Field targetField;
|
||||
protected Class<?> targetEntityClass;
|
||||
protected TableInfo targetTableInfo;
|
||||
@ -47,7 +49,8 @@ abstract class AbstractRelation<SelfEntity> {
|
||||
|
||||
protected String dataSource;
|
||||
|
||||
public AbstractRelation(String selfField, String targetField, String dataSource, Class<SelfEntity> entityClass, Field relationField) {
|
||||
public AbstractRelation(String selfField, String targetSchema, String targetTable, String targetField,
|
||||
String dataSource, Class<SelfEntity> entityClass, Field relationField) {
|
||||
this.selfEntityClass = entityClass;
|
||||
this.relationField = relationField;
|
||||
this.relationFieldWrapper = FieldWrapper.of(entityClass, relationField.getName());
|
||||
@ -59,6 +62,8 @@ abstract class AbstractRelation<SelfEntity> {
|
||||
|
||||
|
||||
this.targetEntityClass = relationFieldWrapper.getMappingType();
|
||||
this.targetSchema = targetSchema;
|
||||
this.targetTable = targetTable;
|
||||
|
||||
this.targetField = ClassUtil.getFirstField(targetEntityClass, field -> field.getName().equals(targetField));
|
||||
this.targetFieldWrapper = FieldWrapper.of(targetEntityClass, targetField);
|
||||
@ -167,6 +172,15 @@ abstract class AbstractRelation<SelfEntity> {
|
||||
this.dataSource = dataSource;
|
||||
}
|
||||
|
||||
public String getTargetTableWithSchema() {
|
||||
if (StringUtil.isNotBlank(targetTable)) {
|
||||
return StringUtil.isNotBlank(targetSchema) ? targetSchema + "." + targetTable : targetTable;
|
||||
} else {
|
||||
return targetTableInfo.getTableNameWithSchema();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected static Class<?> getTargetEntityClass(Class<?> entityClass, Field relationField) {
|
||||
return FieldWrapper.of(entityClass, relationField.getName()).getMappingType();
|
||||
}
|
||||
@ -186,9 +200,9 @@ abstract class AbstractRelation<SelfEntity> {
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 把 Relations 的配置转换为查询的 QueryWrapper
|
||||
*
|
||||
* @param selfEntities 当前的实体类
|
||||
* @return QueryWrapper
|
||||
*/
|
||||
@ -197,6 +211,7 @@ abstract class AbstractRelation<SelfEntity> {
|
||||
|
||||
/**
|
||||
* 通过 {@link AbstractRelation#toQueryWrapper(List)} 查询到的结果,通过此方法进行内存 join
|
||||
*
|
||||
* @param selfEntities 当前的实体类列表
|
||||
* @param targetObjectList 查询到的结果
|
||||
* @param mapper 查询的 Mapper
|
||||
|
||||
@ -38,9 +38,11 @@ class ManyToMany<SelfEntity> extends AbstractRelation<SelfEntity> {
|
||||
private String orderBy;
|
||||
|
||||
public ManyToMany(RelationManyToMany annotation, Class<SelfEntity> entityClass, Field relationField) {
|
||||
super(getDefaultPrimaryProperty(annotation.selfField(), entityClass, "@RelationManyToMany.selfField 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.dataSource(), entityClass, relationField);
|
||||
super(getDefaultPrimaryProperty(annotation.selfField(), entityClass, "@RelationManyToMany.selfField can not be empty in field: \"" + entityClass.getName() + "." + relationField.getName() + "\"")
|
||||
, annotation.targetSchema()
|
||||
, annotation.targetTable()
|
||||
, getDefaultPrimaryProperty(annotation.targetField(), getTargetEntityClass(entityClass, relationField), "@RelationManyToMany.targetField can not be empty in field: \"" + entityClass.getName() + "." + relationField.getName() + "\"")
|
||||
, annotation.dataSource(), entityClass, relationField);
|
||||
|
||||
this.joinTable = annotation.joinTable();
|
||||
this.joinSelfColumn = annotation.joinSelfColumn();
|
||||
@ -90,7 +92,7 @@ class ManyToMany<SelfEntity> extends AbstractRelation<SelfEntity> {
|
||||
}
|
||||
|
||||
QueryWrapper queryWrapper = QueryWrapper.create().select()
|
||||
.from(targetTableInfo.getTableNameWithSchema());
|
||||
.from(getTargetTableWithSchema());
|
||||
if (targetValues.size() > 1) {
|
||||
queryWrapper.where(column(targetTableInfo.getColumnByProperty(targetField.getName())).in(targetValues));
|
||||
} else {
|
||||
|
||||
@ -23,6 +23,8 @@ class ManyToOne<SelfEntity> extends ToOneRelation<SelfEntity> {
|
||||
|
||||
public ManyToOne(RelationManyToOne annotation, Class<SelfEntity> entityClass, Field relationField) {
|
||||
super(annotation.selfField()
|
||||
, annotation.targetSchema()
|
||||
, annotation.targetTable()
|
||||
, getDefaultPrimaryProperty(annotation.targetField(), getTargetEntityClass(entityClass, relationField)
|
||||
, "@RelationManyToOne.selfField can not be empty in field: \"" + entityClass.getName() + "." + relationField.getName() + "\"")
|
||||
, annotation.dataSource()
|
||||
|
||||
@ -37,6 +37,8 @@ class OneToMany<SelfEntity> extends AbstractRelation<SelfEntity> {
|
||||
|
||||
public OneToMany(RelationOneToMany annotation, Class<SelfEntity> entityClass, Field relationField) {
|
||||
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);
|
||||
this.orderBy = annotation.orderBy();
|
||||
this.limit = annotation.limit();
|
||||
@ -49,7 +51,7 @@ class OneToMany<SelfEntity> extends AbstractRelation<SelfEntity> {
|
||||
return null;
|
||||
}
|
||||
QueryWrapper queryWrapper = QueryWrapper.create().select()
|
||||
.from(targetTableInfo.getTableNameWithSchema());
|
||||
.from(getTargetTableWithSchema());
|
||||
if (selfFieldValues.size() > 1) {
|
||||
queryWrapper.where(column(targetTableInfo.getColumnByProperty(targetField.getName())).in(selfFieldValues));
|
||||
} else {
|
||||
|
||||
@ -24,6 +24,8 @@ class OneToOne<SelfEntity> extends ToOneRelation<SelfEntity> {
|
||||
public OneToOne(RelationOneToOne annotation, Class<SelfEntity> entityClass, Field relationField) {
|
||||
super(getDefaultPrimaryProperty(annotation.selfField(), entityClass
|
||||
, "@RelationOneToOne.selfField can not be empty in field: \"" + entityClass.getName() + "." + relationField.getName() + "\"")
|
||||
, annotation.targetSchema()
|
||||
, annotation.targetTable()
|
||||
, annotation.targetField()
|
||||
, annotation.dataSource()
|
||||
, entityClass
|
||||
|
||||
@ -27,10 +27,12 @@ import static com.mybatisflex.core.query.QueryMethods.column;
|
||||
class ToOneRelation<SelfEntity> extends AbstractRelation<SelfEntity> {
|
||||
|
||||
|
||||
public ToOneRelation(String selfField, String targetField, String dataSource, Class<SelfEntity> selfEntityClass, Field relationField) {
|
||||
super(selfField, targetField, dataSource, selfEntityClass, relationField);
|
||||
public ToOneRelation(String selfField, String targetSchema, String targetTable, String targetField,
|
||||
String dataSource, Class<SelfEntity> selfEntityClass, Field relationField) {
|
||||
super(selfField, targetSchema, targetTable, targetField, dataSource, selfEntityClass, relationField);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public QueryWrapper toQueryWrapper(List<SelfEntity> selfEntities) {
|
||||
Set<Object> selfFieldValues = getSelfFieldValues(selfEntities);
|
||||
@ -38,7 +40,7 @@ class ToOneRelation<SelfEntity> extends AbstractRelation<SelfEntity> {
|
||||
return null;
|
||||
}
|
||||
QueryWrapper queryWrapper = QueryWrapper.create().select()
|
||||
.from(targetTableInfo.getTableNameWithSchema());
|
||||
.from(getTargetTableWithSchema());
|
||||
if (selfFieldValues.size() > 1) {
|
||||
queryWrapper.where(column(targetTableInfo.getColumnByProperty(targetField.getName())).in(selfFieldValues));
|
||||
} else {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user