更新关联问题

This commit is contained in:
Dftre 2024-05-14 09:23:03 +08:00
parent 3c5d5d260e
commit 3e31d6fc26
4 changed files with 16 additions and 7 deletions

View File

@ -15,5 +15,5 @@ import java.lang.annotation.Target;
public @interface ColumnMap {
String name(); // 对应数据库字段
String target(); // 映射表来源
String targetColumn(); // 映射字段
String on(); // 映射关联字段
}

View File

@ -16,4 +16,6 @@ public @interface EnableTableMap {
String name() default "t";
String dept() default "";
String user() default "";
String userOn() default "user_id";
String deptOn() default "dept_id";
}

View File

@ -55,21 +55,21 @@ public class TableInfo {
this.getMapColumns().stream()
.map(MapColumnInfo::getJoin)
.map(join -> join.target() + " on "
+ join.target() + "." + join.targetColumn() + " = "
+ this.getTableNameT() + "." + join.targetColumn())
+ join.target() + "." + join.on() + " = "
+ this.getTableNameT() + "." + join.on())
.forEach(joinSql::add);
if (this.enableTableMap != null) {
if (StringUtils.isNotEmpty(this.enableTableMap.user())) {
this.joinSql.add("sys_user " + this.enableTableMap.user() + " on "
+ this.enableTableMap.user() + ".user_id = "
+ this.getTableNameT() + "." + ".user_id");
+ this.enableTableMap.user() + "." + this.enableTableMap.userOn() + " = "
+ this.getTableNameT() + "." + this.enableTableMap.userOn());
this.hasDataScopeValue = true;
}
if (StringUtils.isNotEmpty(this.enableTableMap.dept())) {
this.joinSql.add("sys_dept " + this.enableTableMap.dept() + " on "
+ this.enableTableMap.dept() + ".dept_id = "
+ this.getTableNameT() + ".dept_id");
+ this.enableTableMap.dept() + "." + this.enableTableMap.deptOn() + " = "
+ this.getTableNameT() + "." + this.enableTableMap.deptOn());
this.hasDataScopeValue = true;
}
}

View File

@ -9,6 +9,7 @@ import java.util.Map;
import org.apache.ibatis.jdbc.SQL;
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.mybatis.domain.ColumnInfo;
import com.ruoyi.mybatis.domain.MapColumnInfo;
import com.ruoyi.mybatis.domain.TableInfo;
@ -38,12 +39,15 @@ public class SQLUtil {
if (tableInfo.isEnbleMap()) {
tableInfo.getJoinSql().stream()
.filter(StringUtils::isNotEmpty)
.forEach(sql::LEFT_OUTER_JOIN);
tableInfo.getNotNullMapColumns(entity).stream()
.map(MapColumnInfo::getQuerySql)
.filter(StringUtils::isNotEmpty)
.forEach(sql::WHERE);
tableInfo.getNotNullColumnsForQuery(entity).stream()
.map(ColumnInfo::getQuerySql)
.filter(StringUtils::isNotEmpty)
.map(query -> tableInfo.getTableNameT() + "." + query)
.forEach(sql::WHERE);
if (tableInfo.hasDataScope()) {
@ -51,13 +55,16 @@ public class SQLUtil {
}
Arrays.stream(tableInfo.getOrderBy())
.filter(StringUtils::isNotEmpty)
.map(order -> tableInfo.getTableNameT() + "." + order)
.forEach(sql::ORDER_BY);
} else {
tableInfo.getNotNullColumnsForQuery(entity).stream()
.map(ColumnInfo::getQuerySql)
.filter(StringUtils::isNotEmpty)
.forEach(sql::WHERE);
Arrays.stream(tableInfo.getOrderBy())
.filter(StringUtils::isNotEmpty)
.forEach(sql::ORDER_BY);
}
return sql.toString();