From 8cad41d0a679e1ce1d6f0005786c3c4ccb00ef65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Thu, 20 Apr 2023 10:52:00 +0800 Subject: [PATCH] =?UTF-8?q?fixed:=20=E5=AD=90=E6=9F=A5=E8=AF=A2=E6=97=B6?= =?UTF-8?q?=EF=BC=8C=E9=80=BB=E8=BE=91=E5=88=A0=E9=99=A4=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=E7=9A=84=E5=A4=84=E7=90=86=E6=9C=89=E9=94=99=E8=AF=AF=20close?= =?UTF-8?q?=20#I6X4U8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mybatisflex/core/BaseMapper.java | 9 ++++- .../core/query/BaseQueryWrapper.java | 28 ++++++++++++-- .../java/com/mybatisflex/core/query/CPI.java | 21 +++++++++++ .../mybatisflex/core/query/QueryWrapper.java | 14 +++++++ .../mybatisflex/core/query/WrapperUtil.java | 37 +++++++++++++++++-- .../com/mybatisflex/core/table/TableInfo.java | 22 ++++++++++- .../java/com/mybatisflex/test/Account.java | 3 ++ .../test/AccountOnSetListener.java | 2 +- .../mybatisflex/test/EntityTestStarter.java | 16 ++++---- .../src/main/resources/data.sql | 4 +- .../src/main/resources/schema.sql | 3 +- 11 files changed, 138 insertions(+), 21 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/BaseMapper.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/BaseMapper.java index 144c7528..9dee369b 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/BaseMapper.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/BaseMapper.java @@ -420,12 +420,14 @@ public interface BaseMapper { * @return page 数据 */ default Page paginate(@Param("page") Page page, @Param("query") QueryWrapper queryWrapper) { - List groupByColumns = CPI.getGroupByColumns(queryWrapper); + + List groupByColumns = null; // 只有 totalRow 小于 0 的时候才会去查询总量 // 这样方便用户做总数缓存,而非每次都要去查询总量 // 一般的分页场景中,只有第一页的时候有必要去查询总量,第二页以后是不需要的 if (page.getTotalRow() < 0) { + groupByColumns = CPI.getGroupByColumns(queryWrapper); //清除group by 去查询数据 CPI.setGroupByColumns(queryWrapper, null); long count = selectCountByQuery(queryWrapper); @@ -437,7 +439,10 @@ public interface BaseMapper { } //恢复数量查询清除的 groupBy - CPI.setGroupByColumns(queryWrapper, groupByColumns); + if (groupByColumns != null) { + CPI.setGroupByColumns(queryWrapper, groupByColumns); + } + int offset = page.getPageSize() * (page.getPageNumber() - 1); queryWrapper.limit(offset, page.getPageSize()); List rows = selectListByQuery(queryWrapper); diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/BaseQueryWrapper.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/BaseQueryWrapper.java index 695507da..b9967455 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/BaseQueryWrapper.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/BaseQueryWrapper.java @@ -16,12 +16,11 @@ package com.mybatisflex.core.query; import java.io.Serializable; -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.List; +import java.util.*; public class BaseQueryWrapper implements Serializable { + protected List queryTables; protected String datasource; @@ -38,6 +37,8 @@ public class BaseQueryWrapper implements Serializable { protected Integer limitOffset; protected Integer limitRows; + protected Map context; + protected T addSelectColumn(QueryColumn queryColumn) { if (selectColumns == null) { @@ -208,4 +209,23 @@ public class BaseQueryWrapper implements Serializable { protected void setLimitRows(Integer limitRows) { this.limitRows = limitRows; } -} + + protected Map getContext() { + return context; + } + + protected void setContext(Map context) { + this.context = context; + } + + protected void putContext(String key, Object value){ + if (context == null){ + context = new HashMap<>(); + } + context.put(key,value); + } + + protected R getContext(String key){ + return context == null ? null : (R) context.get(key); + } +} \ No newline at end of file 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 f579f4e5..f5227efb 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 @@ -20,6 +20,7 @@ import com.mybatisflex.core.util.CollectionUtil; import com.mybatisflex.core.util.StringUtil; import java.util.List; +import java.util.Map; /** * Cross Package Invoke @@ -34,6 +35,9 @@ public class CPI { return queryWrapper.getValueArray(); } + public static List getChildSelect(QueryWrapper queryWrapper) { + return queryWrapper.getChildSelect(); + } public static List getQueryTables(QueryWrapper queryWrapper) { return queryWrapper.getQueryTables(); @@ -130,6 +134,23 @@ public class CPI { queryWrapper.setLimitRows(limitRows); } + public static Map getContext(QueryWrapper queryWrapper) { + return queryWrapper.getContext(); + } + + public static void setContext(QueryWrapper queryWrapper, Map context) { + queryWrapper.setContext(context); + } + + public static void putContext(QueryWrapper queryWrapper, String key, Object value) { + queryWrapper.putContext(key, value); + } + + + public static R getContext(QueryWrapper queryWrapper, String key) { + return queryWrapper.getContext(key); + } + public static String toConditionSql(QueryColumn queryColumn, List queryTables, IDialect dialect) { return queryColumn.toConditionSql(queryTables, dialect); 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 82c13bb7..992355cf 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 @@ -23,6 +23,7 @@ import com.mybatisflex.core.util.StringUtil; import java.util.ArrayList; import java.util.Arrays; +import java.util.List; import java.util.Map; public class QueryWrapper extends BaseQueryWrapper { @@ -363,4 +364,17 @@ public class QueryWrapper extends BaseQueryWrapper { } + List getChildSelect() { + List childQueryWrappers = new ArrayList<>(); + + List whereChildQuery= WrapperUtil.getChildSelect(whereQueryCondition); + List havingChildQuery = WrapperUtil.getChildSelect(havingQueryCondition); + + childQueryWrappers.addAll(whereChildQuery); + childQueryWrappers.addAll(havingChildQuery); + + return childQueryWrappers; + } + + } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/WrapperUtil.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/WrapperUtil.java index f873dc15..16906b1b 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/WrapperUtil.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/WrapperUtil.java @@ -20,8 +20,9 @@ import com.mybatisflex.core.util.CollectionUtil; import com.mybatisflex.core.util.StringUtil; import java.lang.reflect.Array; +import java.util.ArrayList; import java.util.Arrays; -import java.util.LinkedList; +import java.util.Collections; import java.util.List; class WrapperUtil { @@ -33,12 +34,42 @@ class WrapperUtil { static final Object[] NULL_PARA_ARRAY = new Object[0]; + static List getChildSelect(QueryCondition condition) { + List list = null; + while (condition != null) { + if (condition.checkEffective()) { + Object value = condition.getValue(); + if (value instanceof QueryWrapper) { + if (list == null) { + list = new ArrayList<>(); + } + list.add((QueryWrapper) value); + list.addAll(((QueryWrapper) value).getChildSelect()); + } else if (value != null && value.getClass().isArray()) { + for (int i = 0; i < Array.getLength(value); i++) { + Object arrayValue = Array.get(value, i); + if (arrayValue instanceof QueryWrapper) { + if (list == null) { + list = new ArrayList<>(); + } + list.add((QueryWrapper) arrayValue); + list.addAll(((QueryWrapper) arrayValue).getChildSelect()); + } + } + } + } + condition = condition.next; + } + return list == null ? Collections.emptyList() : list; + } + + static Object[] getValues(QueryCondition condition) { if (condition == null) { return NULL_PARA_ARRAY; } - List paras = new LinkedList<>(); + List paras = new ArrayList<>(); getValues(condition, paras); return paras.isEmpty() ? NULL_PARA_ARRAY : paras.toArray(); @@ -58,7 +89,6 @@ class WrapperUtil { return; } - if (value.getClass().isArray()) { Object[] values = (Object[]) value; for (Object object : values) { @@ -120,4 +150,5 @@ class WrapperUtil { return queryTable; } + } 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 55f37906..0f0b1f36 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 @@ -482,6 +482,13 @@ public class TableInfo { public void appendConditions(Object entity, QueryWrapper queryWrapper) { + Object appendConditions = CPI.getContext(queryWrapper, "appendConditions"); + if (Boolean.TRUE.equals(appendConditions)) { + return; + } else { + CPI.putContext(queryWrapper, "appendConditions", Boolean.TRUE); + } + //添加乐观锁条件,只有在 update 的时候进行处理 if (StringUtil.isNotBlank(versionColumn) && entity != null) { Object versionValue = buildColumnSqlArg(entity, versionColumn); @@ -493,7 +500,6 @@ public class TableInfo { //逻辑删除条件,已删除的数据不能被修改 if (StringUtil.isNotBlank(logicDeleteColumn)) { -// queryWrapper.and(QueryCondition.create(tableName, logicDeleteColumn, QueryCondition.LOGIC_EQUALS, FlexConsts.LOGIC_DELETE_NORMAL)); queryWrapper.and(QueryCondition.create(tableName, logicDeleteColumn, QueryCondition.LOGIC_EQUALS , FlexGlobalConfig.getDefaultConfig().getNormalValueOfLogicDelete())); } @@ -508,6 +514,20 @@ public class TableInfo { } } + //子查询 + List childSelects = CPI.getChildSelect(queryWrapper); + if (CollectionUtil.isNotEmpty(childSelects)) { + for (QueryWrapper childQueryWrapper : childSelects) { + List queryTables = CPI.getQueryTables(childQueryWrapper); + for (QueryTable queryTable : queryTables) { + TableInfo tableInfo = TableInfoFactory.ofTableName(queryTable.getName()); + if (tableInfo != null) { + tableInfo.appendConditions(entity, childQueryWrapper); + } + } + } + } + //union List unions = CPI.getUnions(queryWrapper); if (CollectionUtil.isNotEmpty(unions)) { diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/Account.java b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/Account.java index 6ad76b9e..ea59742f 100644 --- a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/Account.java +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/Account.java @@ -27,6 +27,8 @@ public class Account extends BaseAccount implements Serializable { @Column(typeHandler = Fastjson2TypeHandler.class,isLarge = true) private Map options; + @Column(isLogicDelete = true) + private Boolean isDelete; public Long getId() { @@ -85,6 +87,7 @@ public class Account extends BaseAccount implements Serializable { ", age=" + age + ", birthday=" + birthday + ", options=" + options + + ", isDelete=" + isDelete + '}'; } } diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/AccountOnSetListener.java b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/AccountOnSetListener.java index 522e6c23..2f440d7a 100644 --- a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/AccountOnSetListener.java +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/AccountOnSetListener.java @@ -5,7 +5,7 @@ import com.mybatisflex.annotation.SetListener; public class AccountOnSetListener implements SetListener { @Override public Object onSet(Object entity, String property, Object value) { - System.out.println(">>>>>>> property: " + property +" value:" + value); +// System.out.println(">>>>>>> property: " + property +" value:" + value); return value; } } 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 d743fa20..821a1c34 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 @@ -26,9 +26,7 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; import javax.sql.DataSource; -import java.util.List; - -import static com.mybatisflex.core.query.QueryMethods.count; +import static com.mybatisflex.core.query.QueryMethods.select; import static com.mybatisflex.test.table.Tables.ACCOUNT; public class EntityTestStarter { @@ -55,12 +53,16 @@ public class EntityTestStarter { AccountMapper accountMapper = bootstrap.getMapper(AccountMapper.class); - QueryWrapper wrapper = QueryWrapper.create().select(ACCOUNT.DEFAULT_COLUMNS).select(count()).from(ACCOUNT) - .groupBy(ACCOUNT.ID); - List accounts = accountMapper.selectListByQuery(wrapper); +// QueryWrapper wrapper = QueryWrapper.create().select(ACCOUNT.DEFAULT_COLUMNS).select(count()).from(ACCOUNT) +// .groupBy(ACCOUNT.ID); +// List accounts = accountMapper.selectListByQuery(wrapper); + QueryWrapper queryWrapper = new QueryWrapper(); + queryWrapper.where(ACCOUNT.ID.in( + select(ACCOUNT.ID).from(ACCOUNT) + )); - Page paginate = accountMapper.paginate(new Page<>(1,10), new QueryWrapper()); + Page paginate = accountMapper.paginate(new Page<>(1,10),queryWrapper); System.out.println(paginate); diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/resources/data.sql b/mybatis-flex-test/mybatis-flex-native-test/src/main/resources/data.sql index 55164823..b60573b4 100644 --- a/mybatis-flex-test/mybatis-flex-native-test/src/main/resources/data.sql +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/resources/data.sql @@ -1,3 +1,3 @@ INSERT INTO tb_account -VALUES (1, '张三', 18, 0,'2020-01-11', null), - (2, '王麻子叔叔', 19, 1, '2021-03-21', null); \ No newline at end of file +VALUES (1, '张三', 18, 0,'2020-01-11', null,0), + (2, '王麻子叔叔', 19, 1, '2021-03-21', null,0); \ No newline at end of file diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/resources/schema.sql b/mybatis-flex-test/mybatis-flex-native-test/src/main/resources/schema.sql index b01d95ec..4b9be958 100644 --- a/mybatis-flex-test/mybatis-flex-native-test/src/main/resources/schema.sql +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/resources/schema.sql @@ -5,5 +5,6 @@ CREATE TABLE IF NOT EXISTS `tb_account` `age` Integer, `sex` Integer, `birthday` DATETIME, - `options` VARCHAR(1024) + `options` VARCHAR(1024), + `is_delete` Integer ); \ No newline at end of file