diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/CPI.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/CPI.java index 09cc40cb..2512763d 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/CPI.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/CPI.java @@ -30,7 +30,8 @@ import java.util.Map; public class CPI { - private CPI() {} + private CPI() { + } public static Object[] getValueArray(QueryWrapper queryWrapper) { return queryWrapper.getValueArray(); @@ -97,6 +98,15 @@ public class CPI { return join.getQueryTable(); } + public static QueryCondition getJoinQueryCondition(Join join) { + return join.on; + } + + public static void setJoinQueryCondition(Join join, QueryCondition queryCondition) { + join.on = queryCondition; + } + + public static List getJoinTables(QueryWrapper queryWrapper) { return queryWrapper.getJoinTables(); } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java index 7a85ae43..87b237ef 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java @@ -632,7 +632,6 @@ public class TableInfo { } private static final String APPEND_CONDITIONS_FLAG = "appendConditions"; - private static final String APPEND_JOIN_FLAG = "appendJoins"; public void appendConditions(Object entity, QueryWrapper queryWrapper) { @@ -699,23 +698,24 @@ public class TableInfo { //join - if (!Boolean.TRUE.equals(CPI.getContext(queryWrapper, APPEND_JOIN_FLAG))) { - List joins = CPI.getJoins(queryWrapper); - if (CollectionUtil.isNotEmpty(joins)) { - for (Join join : joins) { - QueryTable joinQueryTable = CPI.getJoinQueryTable(join); - if (joinQueryTable instanceof SelectQueryTable) { - QueryWrapper childQuery = ((SelectQueryTable) joinQueryTable).getQueryWrapper(); - doAppendConditions(entity, childQuery); - } else { - String nameWithSchema = joinQueryTable.getNameWithSchema(); - if (StringUtil.isNotBlank(nameWithSchema)) { - TableInfo tableInfo = TableInfoFactory.ofTableName(nameWithSchema); - if (tableInfo != null) { - CPI.putContext(queryWrapper, APPEND_CONDITIONS_FLAG, Boolean.FALSE); - CPI.putContext(queryWrapper, APPEND_JOIN_FLAG, Boolean.TRUE); - tableInfo.appendConditions(entity, queryWrapper); - } + List joins = CPI.getJoins(queryWrapper); + if (CollectionUtil.isNotEmpty(joins)) { + for (Join join : joins) { + QueryTable joinQueryTable = CPI.getJoinQueryTable(join); + if (joinQueryTable instanceof SelectQueryTable) { + QueryWrapper childQuery = ((SelectQueryTable) joinQueryTable).getQueryWrapper(); + doAppendConditions(entity, childQuery); + } else { + + String nameWithSchema = joinQueryTable.getNameWithSchema(); + if (StringUtil.isNotBlank(nameWithSchema)) { + TableInfo tableInfo = TableInfoFactory.ofTableName(nameWithSchema); + if (tableInfo != null) { + QueryCondition joinQueryCondition = CPI.getJoinQueryCondition(join); + QueryWrapper newWrapper = QueryWrapper.create() + .where(joinQueryCondition); + tableInfo.appendConditions(entity, newWrapper); + CPI.setJoinQueryCondition(join, CPI.getWhereQueryCondition(newWrapper)); } } }