feat: Relation 注解 targetSchema 和 targetTable 的支持。

This commit is contained in:
开源海哥 2023-07-12 11:32:05 +08:00
parent 677eabdbe5
commit 695a90562b
10 changed files with 195 additions and 114 deletions

View File

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

View File

@ -34,6 +34,20 @@ public @interface RelationManyToOne {
*/ */
String selfField(); String selfField();
/**
* 目标实体类对应的表的 schema一般关联数据不是 entity而是 vodto 等需要配置此项
*
* @return schema 名称
*/
String targetSchema() default "";
/**
* 目标实体类对应的表一般关联数据不是 entity而是 vodto 等需要配置此项
*
* @return 表名
*/
String targetTable() default "";
/** /**
* 目标实体类的关联属性 * 目标实体类的关联属性
* *

View File

@ -34,6 +34,20 @@ public @interface RelationOneToMany {
*/ */
String selfField() default ""; String selfField() default "";
/**
* 目标实体类对应的表的 schema一般关联数据不是 entity而是 vodto 等需要配置此项
*
* @return schema 名称
*/
String targetSchema() default "";
/**
* 目标实体类对应的表一般关联数据不是 entity而是 vodto 等需要配置此项
*
* @return 表名
*/
String targetTable() default "";
/** /**
* 目标实体类的关联属性 * 目标实体类的关联属性
* *

View File

@ -34,6 +34,20 @@ public @interface RelationOneToOne {
*/ */
String selfField() default ""; String selfField() default "";
/**
* 目标实体类对应的表的 schema一般关联数据不是 entity而是 vodto 等需要配置此项
*
* @return schema 名称
*/
String targetSchema() default "";
/**
* 目标实体类对应的表一般关联数据不是 entity而是 vodto 等需要配置此项
*
* @return 表名
*/
String targetTable() default "";
/** /**
* 目标实体类的关联属性 * 目标实体类的关联属性
* *

View File

@ -40,6 +40,8 @@ abstract class AbstractRelation<SelfEntity> {
protected Field selfField; protected Field selfField;
protected FieldWrapper selfFieldWrapper; protected FieldWrapper selfFieldWrapper;
protected String targetSchema;
protected String targetTable;
protected Field targetField; protected Field targetField;
protected Class<?> targetEntityClass; protected Class<?> targetEntityClass;
protected TableInfo targetTableInfo; protected TableInfo targetTableInfo;
@ -47,7 +49,8 @@ abstract class AbstractRelation<SelfEntity> {
protected String dataSource; 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.selfEntityClass = entityClass;
this.relationField = relationField; this.relationField = relationField;
this.relationFieldWrapper = FieldWrapper.of(entityClass, relationField.getName()); this.relationFieldWrapper = FieldWrapper.of(entityClass, relationField.getName());
@ -59,6 +62,8 @@ abstract class AbstractRelation<SelfEntity> {
this.targetEntityClass = relationFieldWrapper.getMappingType(); this.targetEntityClass = relationFieldWrapper.getMappingType();
this.targetSchema = targetSchema;
this.targetTable = 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);
@ -167,6 +172,15 @@ abstract class AbstractRelation<SelfEntity> {
this.dataSource = dataSource; 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) { protected static Class<?> getTargetEntityClass(Class<?> entityClass, Field relationField) {
return FieldWrapper.of(entityClass, relationField.getName()).getMappingType(); return FieldWrapper.of(entityClass, relationField.getName()).getMappingType();
} }
@ -186,9 +200,9 @@ abstract class AbstractRelation<SelfEntity> {
} }
/** /**
* Relations 的配置转换为查询的 QueryWrapper * Relations 的配置转换为查询的 QueryWrapper
*
* @param selfEntities 当前的实体类 * @param selfEntities 当前的实体类
* @return QueryWrapper * @return QueryWrapper
*/ */
@ -197,6 +211,7 @@ abstract class AbstractRelation<SelfEntity> {
/** /**
* 通过 {@link AbstractRelation#toQueryWrapper(List)} 查询到的结果通过此方法进行内存 join * 通过 {@link AbstractRelation#toQueryWrapper(List)} 查询到的结果通过此方法进行内存 join
*
* @param selfEntities 当前的实体类列表 * @param selfEntities 当前的实体类列表
* @param targetObjectList 查询到的结果 * @param targetObjectList 查询到的结果
* @param mapper 查询的 Mapper * @param mapper 查询的 Mapper

View File

@ -38,9 +38,11 @@ class ManyToMany<SelfEntity> extends AbstractRelation<SelfEntity> {
private String orderBy; private String orderBy;
public ManyToMany(RelationManyToMany annotation, Class<SelfEntity> entityClass, Field relationField) { 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() + "\""), 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.targetSchema()
annotation.dataSource(), entityClass, relationField); , 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.joinTable = annotation.joinTable();
this.joinSelfColumn = annotation.joinSelfColumn(); this.joinSelfColumn = annotation.joinSelfColumn();
@ -90,7 +92,7 @@ class ManyToMany<SelfEntity> extends AbstractRelation<SelfEntity> {
} }
QueryWrapper queryWrapper = QueryWrapper.create().select() QueryWrapper queryWrapper = QueryWrapper.create().select()
.from(targetTableInfo.getTableNameWithSchema()); .from(getTargetTableWithSchema());
if (targetValues.size() > 1) { if (targetValues.size() > 1) {
queryWrapper.where(column(targetTableInfo.getColumnByProperty(targetField.getName())).in(targetValues)); queryWrapper.where(column(targetTableInfo.getColumnByProperty(targetField.getName())).in(targetValues));
} else { } else {

View File

@ -23,6 +23,8 @@ class ManyToOne<SelfEntity> extends ToOneRelation<SelfEntity> {
public ManyToOne(RelationManyToOne annotation, Class<SelfEntity> entityClass, Field relationField) { public ManyToOne(RelationManyToOne annotation, Class<SelfEntity> entityClass, Field relationField) {
super(annotation.selfField() super(annotation.selfField()
, annotation.targetSchema()
, 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.dataSource() , annotation.dataSource()

View File

@ -37,6 +37,8 @@ class OneToMany<SelfEntity> extends AbstractRelation<SelfEntity> {
public OneToMany(RelationOneToMany annotation, Class<SelfEntity> entityClass, Field relationField) { 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() + "\"") 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.dataSource(), entityClass, relationField);
this.orderBy = annotation.orderBy(); this.orderBy = annotation.orderBy();
this.limit = annotation.limit(); this.limit = annotation.limit();
@ -49,7 +51,7 @@ class OneToMany<SelfEntity> extends AbstractRelation<SelfEntity> {
return null; return null;
} }
QueryWrapper queryWrapper = QueryWrapper.create().select() QueryWrapper queryWrapper = QueryWrapper.create().select()
.from(targetTableInfo.getTableNameWithSchema()); .from(getTargetTableWithSchema());
if (selfFieldValues.size() > 1) { if (selfFieldValues.size() > 1) {
queryWrapper.where(column(targetTableInfo.getColumnByProperty(targetField.getName())).in(selfFieldValues)); queryWrapper.where(column(targetTableInfo.getColumnByProperty(targetField.getName())).in(selfFieldValues));
} else { } else {

View File

@ -24,6 +24,8 @@ class OneToOne<SelfEntity> extends ToOneRelation<SelfEntity> {
public OneToOne(RelationOneToOne annotation, Class<SelfEntity> entityClass, Field relationField) { public OneToOne(RelationOneToOne annotation, Class<SelfEntity> entityClass, Field relationField) {
super(getDefaultPrimaryProperty(annotation.selfField(), entityClass super(getDefaultPrimaryProperty(annotation.selfField(), entityClass
, "@RelationOneToOne.selfField can not be empty in field: \"" + entityClass.getName() + "." + relationField.getName() + "\"") , "@RelationOneToOne.selfField can not be empty in field: \"" + entityClass.getName() + "." + relationField.getName() + "\"")
, annotation.targetSchema()
, annotation.targetTable()
, annotation.targetField() , annotation.targetField()
, annotation.dataSource() , annotation.dataSource()
, entityClass , entityClass

View File

@ -27,10 +27,12 @@ import static com.mybatisflex.core.query.QueryMethods.column;
class ToOneRelation<SelfEntity> extends AbstractRelation<SelfEntity> { class ToOneRelation<SelfEntity> extends AbstractRelation<SelfEntity> {
public ToOneRelation(String selfField, String targetField, String dataSource, Class<SelfEntity> selfEntityClass, Field relationField) { public ToOneRelation(String selfField, String targetSchema, String targetTable, String targetField,
super(selfField, targetField, dataSource, selfEntityClass, relationField); String dataSource, Class<SelfEntity> selfEntityClass, Field relationField) {
super(selfField, targetSchema, targetTable, targetField, dataSource, selfEntityClass, relationField);
} }
@Override @Override
public QueryWrapper toQueryWrapper(List<SelfEntity> selfEntities) { public QueryWrapper toQueryWrapper(List<SelfEntity> selfEntities) {
Set<Object> selfFieldValues = getSelfFieldValues(selfEntities); Set<Object> selfFieldValues = getSelfFieldValues(selfEntities);
@ -38,7 +40,7 @@ class ToOneRelation<SelfEntity> extends AbstractRelation<SelfEntity> {
return null; return null;
} }
QueryWrapper queryWrapper = QueryWrapper.create().select() QueryWrapper queryWrapper = QueryWrapper.create().select()
.from(targetTableInfo.getTableNameWithSchema()); .from(getTargetTableWithSchema());
if (selfFieldValues.size() > 1) { if (selfFieldValues.size() > 1) {
queryWrapper.where(column(targetTableInfo.getColumnByProperty(targetField.getName())).in(selfFieldValues)); queryWrapper.where(column(targetTableInfo.getColumnByProperty(targetField.getName())).in(selfFieldValues));
} else { } else {