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 67e23b88..96debf3b 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 @@ -65,7 +65,9 @@ public class CPI { public static void setSelectColumnsIfNecessary(QueryWrapper queryWrapper, List selectColumns) { if (CollectionUtil.isEmpty(queryWrapper.getSelectColumns()) - && CollectionUtil.isNotEmpty(selectColumns)) { + && CollectionUtil.isNotEmpty(selectColumns) + && CollectionUtil.isEmpty(CPI.getJoinTables(queryWrapper)) + ) { queryWrapper.setSelectColumns(selectColumns); } } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/Join.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/Join.java index cd9564c7..853ef14a 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/Join.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/Join.java @@ -65,6 +65,9 @@ public class Join implements Serializable { this.on = condition; } + QueryCondition getOnCondition(){ + return on; + } public boolean checkEffective() { return effective; diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapper.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapper.java index 117f844b..0849f869 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapper.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapper.java @@ -348,25 +348,47 @@ public class QueryWrapper extends BaseQueryWrapper { * 在构建 sql 的时候,需要保证 where 在 having 的前面 */ Object[] getValueArray() { + List joinValues = null; + List joins = getJoins(); + if (CollectionUtil.isNotEmpty(joins)) { + for (Join join : joins) { + QueryCondition onCondition = join.getOnCondition(); + Object[] values = WrapperUtil.getValues(onCondition); + if (values.length > 0) { + if (joinValues == null) { + joinValues = new ArrayList<>(); + } + joinValues.addAll(Arrays.asList(values)); + } + } + } + Object[] whereValues = WrapperUtil.getValues(whereQueryCondition); Object[] havingValues = WrapperUtil.getValues(havingQueryCondition); + Object[] values = ArrayUtil.concat(whereValues, havingValues); + if (CollectionUtil.isNotEmpty(unions)) { for (UnionWrapper union : unions) { QueryWrapper queryWrapper = union.getQueryWrapper(); values = ArrayUtil.concat(values, queryWrapper.getValueArray()); } } - return values; + + if (joinValues != null) { + return ArrayUtil.concat(joinValues.toArray(), values); + } else { + return values; + } } List getChildSelect() { - List whereChildQuery= WrapperUtil.getChildSelect(whereQueryCondition); + List whereChildQuery = WrapperUtil.getChildSelect(whereQueryCondition); List havingChildQuery = WrapperUtil.getChildSelect(havingQueryCondition); - if (whereChildQuery.isEmpty() && havingChildQuery.isEmpty()){ + if (whereChildQuery.isEmpty() && havingChildQuery.isEmpty()) { return Collections.emptyList(); } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfoFactory.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfoFactory.java index 836312cd..888cae1f 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfoFactory.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfoFactory.java @@ -232,7 +232,7 @@ public class TableInfoFactory { largeColumns.add(columnName); } - if (column == null || (!column.isLarge() && !column.isLogicDelete())) { + if (column == null || !column.isLarge()) { defaultColumns.add(columnName); } diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/EntityTestStarter.java b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/EntityTestStarter.java index cf346545..73032d18 100644 --- a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/EntityTestStarter.java +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/EntityTestStarter.java @@ -27,6 +27,7 @@ import javax.sql.DataSource; import java.util.List; import static com.mybatisflex.test.table.Tables.ACCOUNT; +import static com.mybatisflex.test.table.Tables.ARTICLE; public class EntityTestStarter { @@ -52,12 +53,21 @@ public class EntityTestStarter { AccountMapper accountMapper = bootstrap.getMapper(AccountMapper.class); +// QueryWrapper wrapper = QueryWrapper.create().select().from(ACCOUNT) +// .and(ACCOUNT.ID.ge(100).and(ACCOUNT.ID.ge(200))) +// .and(ACCOUNT.ID.ge(100).and(ACCOUNT.ID.ge(200))) +// .groupBy(ACCOUNT.ID); +// +// List accounts = accountMapper.selectListByQuery(wrapper); + + QueryWrapper wrapper = QueryWrapper.create().select().from(ACCOUNT) - .and(ACCOUNT.ID.ge(100).and(ACCOUNT.ID.ge(200))) - .and(ACCOUNT.ID.ge(100).and(ACCOUNT.ID.ge(200))) - .groupBy(ACCOUNT.ID); + .leftJoin(ARTICLE).on(ARTICLE.ACCOUNT_ID.eq(ACCOUNT.ID).and(ACCOUNT.ID.ge(100))) + .and(ACCOUNT.ID.ge(100)); List accounts = accountMapper.selectListByQuery(wrapper); + System.out.println(accounts); + // QueryWrapper queryWrapper = new QueryWrapper(); // queryWrapper.where(ACCOUNT.ID.in(