From 5017ce7643e036e3d6fa27f566eea37a29b8b59a Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Sat, 8 Jul 2023 22:59:44 +0800 Subject: [PATCH 1/4] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E9=87=8D?= =?UTF-8?q?=E8=BD=BD=E6=96=B9=E6=B3=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mybatisflex/core/BaseMapper.java | 189 +++++++++++++++--- 1 file changed, 159 insertions(+), 30 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 c3be373f..83b505f8 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 @@ -51,7 +51,14 @@ import static com.mybatisflex.core.query.QueryMethods.count; public interface BaseMapper { /** - * 插入实体类数据。 + * 默认批量处理切片数量。 + */ + int DEFAULT_BATCH_SIZE = 1000; + + // === 增(insert) === + + /** + * 插入实体类数据,不忽略 {@code null} 值。 * * @param entity 实体类 * @return 受影响的行数 @@ -83,20 +90,19 @@ public interface BaseMapper { int insert(@Param(FlexConsts.ENTITY) T entity, @Param(FlexConsts.IGNORE_NULLS) boolean ignoreNulls); /** - * 插入带有主键的实体类。 + * 插入带有主键的实体类,不忽略 {@code null} 值。 * - * @param entity 实体类,不忽略 {@code null} 值 + * @param entity 实体类 * @return 受影响的行数 */ default int insertWithPk(T entity) { return insertWithPk(entity, false); } - /** - * 插入带有主键的实体类。 + * 插入带有主键的实体类,忽略 {@code null} 值。 * - * @param entity 实体类,忽略 {@code null} 值 + * @param entity 实体类 * @return 受影响的行数 */ default int insertSelectiveWithPk(T entity) { @@ -134,8 +140,7 @@ public interface BaseMapper { */ default int insertBatch(List entities, int size) { if (size <= 0) { - // 默认 1000 - size = 1000; + size = DEFAULT_BATCH_SIZE; } int sum = 0; int entitiesSize = entities.size(); @@ -148,7 +153,7 @@ public interface BaseMapper { } /** - * 新增 或者 更新,若主键有值,则更新,若没有主键值,则插入。 + * 插入或者更新,若主键有值,则更新,若没有主键值,则插入,插入或者更新都不会忽略 {@code null} 值。 * * @param entity 实体类 * @return 受影响的行数 @@ -158,7 +163,17 @@ public interface BaseMapper { } /** - * 新增 或者 更新,若主键有值,则更新,若没有主键值,则插入。 + * 插入或者更新,若主键有值,则更新,若没有主键值,则插入,插入或者更新都会忽略 {@code null} 值。 + * + * @param entity 实体类 + * @return 受影响的行数 + */ + default int insertOrUpdateSelective(T entity) { + return insertOrUpdate(entity, true); + } + + /** + * 插入或者更新,若主键有值,则更新,若没有主键值,则插入。 * * @param entity 实体类 * @param ignoreNulls 是否忽略 {@code null} 值 @@ -174,6 +189,8 @@ public interface BaseMapper { } } + // === 删(delete) === + /** * 根据主键删除数据。如果是多个主键的情况下,需要传入数组,例如:{@code new Integer[]{100,101}}。 * @@ -204,7 +221,7 @@ public interface BaseMapper { */ default int deleteBatchByIds(List ids, int size) { if (size <= 0) { - size = 1000;//默认1000 + size = DEFAULT_BATCH_SIZE; } int sum = 0; int entitiesSize = ids.size(); @@ -248,6 +265,8 @@ public interface BaseMapper { @DeleteProvider(type = EntitySqlProvider.class, method = "deleteByQuery") int deleteByQuery(@Param(FlexConsts.QUERY) QueryWrapper queryWrapper); + // === 改(update) === + /** * 根据主键来更新数据,若实体类属性数据为 {@code null},该属性不会新到数据库。 * @@ -323,6 +342,7 @@ public interface BaseMapper { * @param entity 实体类 * @param ignoreNulls 是否忽略空值 * @param queryWrapper 条件 + * @return 受影响的行数 * @see com.mybatisflex.core.provider.EntitySqlProvider#updateByQuery(Map, ProviderContext) */ @UpdateProvider(type = EntitySqlProvider.class, method = "updateByQuery") @@ -334,6 +354,7 @@ public interface BaseMapper { * @param fieldName 字段名 * @param value 值(大于等于 0 加,小于 0 减) * @param queryWrapper 条件 + * @return 受影响的行数 * @see EntitySqlProvider#updateNumberAddByQuery(Map, ProviderContext) */ @UpdateProvider(type = EntitySqlProvider.class, method = "updateNumberAddByQuery") @@ -345,6 +366,7 @@ public interface BaseMapper { * @param column 字段名 * @param value 值(大于等于 0 加,小于 0 减) * @param queryWrapper 条件 + * @return 受影响的行数 * @see EntitySqlProvider#updateNumberAddByQuery(Map, ProviderContext) */ default int updateNumberAddByQuery(QueryColumn column, Number value, QueryWrapper queryWrapper) { @@ -358,6 +380,7 @@ public interface BaseMapper { * @param fn 字段名 * @param value 值(大于等于 0 加,小于 0 减) * @param queryWrapper 条件 + * @return 受影响的行数 * @see EntitySqlProvider#updateNumberAddByQuery(Map, ProviderContext) */ default int updateNumberAddByQuery(LambdaGetter fn, Number value, QueryWrapper queryWrapper) { @@ -367,6 +390,8 @@ public interface BaseMapper { return updateNumberAddByQuery(column, value, queryWrapper); } + // === 查(select) === + /** * 根据主键查询数据。 * @@ -414,14 +439,14 @@ public interface BaseMapper { * * @param queryWrapper 条件 * @param asType 接收数据类型 - * @return 数据内容 + * @return 实体类数据 */ default R selectOneByQueryAs(QueryWrapper queryWrapper, Class asType) { return MapperUtil.getSelectOneResult(selectListByQueryAs(queryWrapper, asType)); } /** - * 根据 Map 构建的条件来查询数据。 + * 根据 Map 构建的条件来查询 1 条数据。 * * @param whereConditions 条件 * @return 实体类数据 @@ -432,7 +457,7 @@ public interface BaseMapper { } /** - * 根据查询条件查询数据。 + * 根据查询条件查询 1 条数据。 * * @param whereConditions 条件 * @return 实体类数据 @@ -457,7 +482,7 @@ public interface BaseMapper { * * @param queryWrapper 条件 * @param asType 接收数据类型 - * @return 数据内容 + * @return 实体类数据 */ default R selectOneWithRelationsByQueryAs(QueryWrapper queryWrapper, Class asType) { return MapperUtil.queryRelations(this, MapperUtil.getSelectOneResult(selectListByQueryAs(queryWrapper, asType))); @@ -497,7 +522,7 @@ public interface BaseMapper { } /** - * 根据查询条件查询数据。 + * 根据查询条件查询多条数据。 * * @param whereConditions 条件 * @return 数据列表 @@ -508,7 +533,7 @@ public interface BaseMapper { } /** - * 根据查询条件查询数据。 + * 根据查询条件查询多条数据。 * * @param whereConditions 条件 * @param count 数据量 @@ -565,7 +590,7 @@ public interface BaseMapper { /** * 根据查询条件查询数据列表,要求返回的数据为 asType。这种场景一般用在 left join 时, - * 有多出了实体类本身的字段内容,可以转换为 dto、vo 等场景时。 + * 有多出了实体类本身的字段内容,可以转换为 dto、vo 等场景。 * * @param queryWrapper 条件 * @param asType 接收数据类型 @@ -608,7 +633,7 @@ public interface BaseMapper { } /** - * 查询实体类及其 relation 注解字段。 + * 查询实体类及其 Relation 注解字段。 * * @param queryWrapper 条件 */ @@ -617,7 +642,7 @@ public interface BaseMapper { } /** - * 查询实体类及其 relation 注解字段。 + * 查询实体类及其 Relation 注解字段。 * * @param queryWrapper 条件 * @param asType 要求返回的数据类型 @@ -642,7 +667,7 @@ public interface BaseMapper { } /** - * 查询实体类及其 relation 注解字段。 + * 查询实体类及其 Relation 注解字段。 * * @param queryWrapper 条件 * @param asType 返回的类型 @@ -670,7 +695,7 @@ public interface BaseMapper { } /** - * 查询全部数据,及其 relation 字段内容。 + * 查询全部数据,及其 Relation 字段内容。 * * @return 数据列表 */ @@ -800,7 +825,7 @@ public interface BaseMapper { } /** - * 分页查询,及其 relation 字段内容。 + * 分页查询,及其 Relation 字段内容。 * * @param pageNumber 当前页码 * @param pageSize 每页的数据量 @@ -826,7 +851,7 @@ public interface BaseMapper { } /** - * 分页查询,及其 relation 字段内容。 + * 分页查询,及其 Relation 字段内容。 * * @param pageNumber 当前页码 * @param pageSize 每页的数据量 @@ -853,7 +878,7 @@ public interface BaseMapper { } /** - * 分页查询,及其 relation 字段内容。 + * 分页查询,及其 Relation 字段内容。 * * @param pageNumber 当前页码 * @param pageSize 每页的数据量 @@ -882,7 +907,7 @@ public interface BaseMapper { } /** - * 分页查询。 + * 分页查询,及其 Relation 字段内容。 * * @param pageNumber 当前页码 * @param pageSize 每页的数据量 @@ -901,11 +926,10 @@ public interface BaseMapper { * * @param page 包含了页码、每页的数据量,可能包含数据总量 * @param queryWrapper 条件 - * @param consumers 字段查询 * @return page 数据 */ - default Page paginate(Page page, QueryWrapper queryWrapper, Consumer>... consumers) { - return paginateAs(page, queryWrapper, null, consumers); + default Page paginate(Page page, QueryWrapper queryWrapper) { + return paginateAs(page, queryWrapper, null); } /** @@ -914,12 +938,76 @@ public interface BaseMapper { * @param page 包含了页码、每页的数据量,可能包含数据总量 * @param queryWrapper 条件 * @param consumers 字段查询 + * @return page 数据 + */ + default Page paginate(Page page, QueryWrapper queryWrapper, Consumer>... consumers) { + return paginateAs(page, queryWrapper, null, consumers); + } + + /** + * 分页查询,及其 Relation 字段内容。 + * + * @param page 包含了页码、每页的数据量,可能包含数据总量 + * @param queryWrapper 条件 + * @return 分页数据 + */ + default Page paginateWithRelations(Page page, QueryWrapper queryWrapper) { + return paginateWithRelationsAs(page, queryWrapper, null); + } + + /** + * 分页查询,及其 Relation 字段内容。 + * + * @param page 包含了页码、每页的数据量,可能包含数据总量 + * @param queryWrapper 条件 + * @param consumers 字段查询 * @return 分页数据 */ default Page paginateWithRelations(Page page, QueryWrapper queryWrapper, Consumer>... consumers) { return paginateWithRelationsAs(page, queryWrapper, null, consumers); } + /** + * 分页查询。 + * + * @param pageNumber 当前页码 + * @param pageSize 每页的数据量 + * @param queryWrapper 条件 + * @param asType 接收数据类型 + * @return 分页数据 + */ + default Page paginateAs(int pageNumber, int pageSize, QueryWrapper queryWrapper, Class asType) { + Page page = new Page<>(pageNumber, pageSize); + return MapperUtil.doPaginate(this, page, queryWrapper, asType, false); + } + + /** + * 分页查询。 + * + * @param pageNumber 当前页码 + * @param pageSize 每页的数据量 + * @param totalRow 数据总量 + * @param queryWrapper 条件 + * @param asType 接收数据类型 + * @return 分页数据 + */ + default Page paginateAs(int pageNumber, int pageSize, int totalRow, QueryWrapper queryWrapper, Class asType) { + Page page = new Page<>(pageNumber, pageSize, totalRow); + return MapperUtil.doPaginate(this, page, queryWrapper, asType, false); + } + + /** + * 分页查询。 + * + * @param page 包含了页码、每页的数据量,可能包含数据总量 + * @param queryWrapper 条件 + * @param asType 接收数据类型 + * @return 分页数据 + */ + default Page paginateAs(Page page, QueryWrapper queryWrapper, Class asType) { + return MapperUtil.doPaginate(this, page, queryWrapper, asType, false); + } + /** * 分页查询。 * @@ -934,7 +1022,48 @@ public interface BaseMapper { } /** - * 分页查询。 + * 分页查询,及其 Relation 字段内容。 + * + * @param pageNumber 当前页码 + * @param pageSize 每页的数据量 + * @param queryWrapper 条件 + * @param asType 接收数据类型 + * @return 分页数据 + */ + default Page paginateWithRelationsAs(int pageNumber, int pageSize, QueryWrapper queryWrapper, Class asType) { + Page page = new Page<>(pageNumber, pageSize); + return MapperUtil.doPaginate(this, page, queryWrapper, asType, true); + } + + /** + * 分页查询,及其 Relation 字段内容。 + * + * @param pageNumber 当前页码 + * @param pageSize 每页的数据量 + * @param totalRow 数据总量 + * @param queryWrapper 条件 + * @param asType 接收数据类型 + * @return 分页数据 + */ + default Page paginateWithRelationsAs(int pageNumber, int pageSize, int totalRow, QueryWrapper queryWrapper, Class asType) { + Page page = new Page<>(pageNumber, pageSize, totalRow); + return MapperUtil.doPaginate(this, page, queryWrapper, asType, true); + } + + /** + * 分页查询,及其 Relation 字段内容。 + * + * @param page 包含了页码、每页的数据量,可能包含数据总量 + * @param queryWrapper 条件 + * @param asType 接收数据类型 + * @return 分页数据 + */ + default Page paginateWithRelationsAs(Page page, QueryWrapper queryWrapper, Class asType) { + return MapperUtil.doPaginate(this, page, queryWrapper, asType, true); + } + + /** + * 分页查询,及其 Relation 字段内容。 * * @param page 包含了页码、每页的数据量,可能包含数据总量 * @param queryWrapper 条件 From d5b733847f49ae62943f21bb9da80ae327bd5fb4 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Sat, 8 Jul 2023 23:00:10 +0800 Subject: [PATCH 2/4] =?UTF-8?q?style:=20=E6=B7=BB=E5=8A=A0=20package-info.?= =?UTF-8?q?java=20=E5=8C=85=E6=B3=A8=E9=87=8A=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/relation/package-info.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/package-info.java diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/package-info.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/package-info.java new file mode 100644 index 00000000..51b873b1 --- /dev/null +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * 多表关联注解实现。 + */ +package com.mybatisflex.core.relation; \ No newline at end of file From dc1ef4d194041bf3f7fc3898188305fc2121169e Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Sat, 8 Jul 2023 23:00:40 +0800 Subject: [PATCH 3/4] =?UTF-8?q?fix:=20=E6=89=BE=E4=B8=8D=E5=88=B0=E8=B0=83?= =?UTF-8?q?=E7=94=A8=E6=96=B9=E6=B3=95=E9=94=99=E8=AF=AF=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mybatisflex/core/dialect/impl/OracleDialect.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/OracleDialect.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/OracleDialect.java index 83f8de9e..54678350 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/OracleDialect.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/OracleDialect.java @@ -22,6 +22,7 @@ import com.mybatisflex.core.row.Row; import com.mybatisflex.core.row.RowCPI; import com.mybatisflex.core.table.TableInfo; import com.mybatisflex.core.util.CollectionUtil; +import com.mybatisflex.core.util.SqlUtil; import com.mybatisflex.core.util.StringUtil; import java.util.List; @@ -167,7 +168,7 @@ public class OracleDialect extends CommonsDialectImpl { String tableNameWrap = StringUtil.isNotBlank(schema) ? wrap(getRealSchema(schema)) + REFERENCE + wrap(getRealTable(tableName)) : wrap(getRealTable(tableName)); - String questionStrings = buildQuestion(attrs.size()); + String questionStrings = SqlUtil.buildSqlParamPlaceholder(attrs.size()); for (int i = 0; i < rows.size(); i++) { sql.append(INTO).append(tableNameWrap); From 57ed09411bbc11cf27f9951035d7de63f8e2d9ba Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Sat, 8 Jul 2023 23:01:03 +0800 Subject: [PATCH 4/4] =?UTF-8?q?doc:=20=E8=A1=A5=E5=85=85=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E7=9A=84=E6=96=B9=E6=B3=95=E6=96=87=E6=A1=A3=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/zh/base/add-delete-update.md | 58 ++++++++++++++++++---------- docs/zh/base/query.md | 63 +++++++++++++++++++++++++------ 2 files changed, 91 insertions(+), 30 deletions(-) diff --git a/docs/zh/base/add-delete-update.md b/docs/zh/base/add-delete-update.md index 4236c3bb..efdb2758 100644 --- a/docs/zh/base/add-delete-update.md +++ b/docs/zh/base/add-delete-update.md @@ -8,22 +8,30 @@ MyBatis-Flex 内置了一个名为 `BaseMapper` 的接口,它实现了基本 `BaseMapper` 的接口提供了 insert 和 insertBatch 方法,用于新增数据; -- **insert**: 新增 1 条数据 -- **insertSelective**: 新增 1 条数据,忽略 null 值的字段 -- **insertBatch**: 新增多条数据 -- **insertBatch(entities, int size)**: 批量插入 entity 数据,按 size 切分 +- **insert(entity)**:新增 1 条数据,不忽略 null 值的字段 +- **insertSelective(entity)**:新增 1 条数据,忽略 null 值的字段 +- **insert(entity, ignoreNulls)**:插入实体类数据,并设置是否忽略 null 值字段。 +- **insertWithPk(entity)**:插入带有主键的实体类,不忽略 null 值字段。 +- **insertSelectiveWithPk(entity)**:插入带有主键的实体类,忽略 null 值字段。 +- **insertWithPk(entity, ignoreNulls)**:带有主键的插入,并设置是否忽略 null 值字段。 +- **insertBatch(entities)**:新增多条数据 +- **insertBatch(entities, size)**:批量插入 entity 数据,按 size 切分 +- **insertOrUpdate(entity)**:插入或者更新,若主键有值,则更新,若没有主键值,则插入,插入或者更新都不会忽略 null 值字段。 +- **insertOrUpdateSelective(entity)**:插入或者更新,若主键有值,则更新,若没有主键值,则插入,插入或者更新都会忽略 null 值字段。 +- **insertOrUpdate(entity, ignoreNulls)**:插入或者更新,若主键有值,则更新,若没有主键值,则插入,并设置是否忽略 null 值字段。 ## 删除数据 `BaseMapper` 的接口提供了 deleteById、deleteBatchByIds、deleteByMap、deleteByQuery 方法,用于删除数据; -- **deleteById(id)** :根据主键 id 删除数据,复合主键需要传入一个数组,例如 [1,100] -- **deleteBatchByIds(idList)** :根据主键的 集合,批量删除多条数据 -- **deleteByMap(map)** :根据 `map<字段名,值>` 组成的条件删除数据,字段名和值的关系为相等的关系,同时,防止 "不小心" 全表 -删除数据,map 的值不允许为 null 或者 空数据。 -- **deleteByCondition(condition)**:根据 condition 构建的条件来删除数据 -- **deleteByQuery(queryWrapper)**:根据 queryWrapper 组成的条件删除数据。 +- **deleteById(id)**:根据主键 id 删除数据,复合主键需要传入一个数组,例如 [1,100] +- **deleteBatchByIds(ids)**:根据主键的 集合,批量删除多条数据 +- **deleteBatchByIds(ids, size)**:根据多个主键批量删除数据,并按 size 切分。 +- **deleteByMap(map)**:根据 `map<字段名,值>` 组成的条件删除数据,字段名和值的关系为相等的关系,同时,防止 "不小心" 全表 + 删除数据,map 的值不允许为 null 或者 空数据。 +- **deleteByCondition(condition)**:根据 QueryCondition 构建的条件来删除数据 +- **deleteByQuery(queryWrapper)**:根据 QueryWrapper 组成的条件删除数据。 **deleteByQuery(queryWrapper)** 方法示例: @@ -56,15 +64,27 @@ delete from tb_account where id >= 100; `BaseMapper` 的接口提供了 update、updateByMap、updateByQuery 方法,用于更新数据; -- **update(entity)**:根据主键更新到 entity 到数据库,要求主键值不能为空,否则会抛出异常。同时,数据为 null 的字段 **不会** 更新到数据库。 -- **update(entity, ignoreNulls)**:根据主键更新到 entity 到数据库,要求主键值不能为空。ignoreNulls 为是否忽略 null 字段,如果为 false,所有 null 字段都会更新到数据库。 -- **updateByMap(entity, map)**:根据 `map<字段名,值>` 组成的条件更新到 entity 到数据库,entity 可以没有主键(如果有也会被忽略), entity 的 null 属性,会自动被忽略。 -- **updateByCondition(entity, condition)**:根据 condition 构建的条件更新到 entity 到数据库,entity 可以没有主键(如果有也会被忽略), entity 的 null 属性,会自动被忽略。 -- **updateByCondition(entity, ignoreNulls, condition)**:ignoreNulls 是否忽略 null 值,默认为 true,如果为 false,所有 null 字段都会更新到数据库。 -- **updateByQuery(entity, queryWrapper)**:根据 queryWrapper 组成的条件更新到 entity 到数据库,entity 可以没有主键(如果有也会被忽略), entity 的 null 属性,会自动被忽略。 -- **updateByQuery(entity, ignoreNulls, queryWrapper)**:据 queryWrapper 组成的条件更新到 entity 到数据库,entity 可以没有主键(如果有也会被忽略)。 ignoreNulls 用于是否忽略 entity 的 null 属性 -, 若 ignoreNulls 为 false,entity 的所有 null 属性都会被更新到数据库。 -- **updateNumberAddByQuery(fieldName,value,queryWrapper)**:通过 `update table set field = field + 1 where ... ` 的这种方向更新数据库某个字段内容。 +- **update(entity)**:根据主键更新到 entity 到数据库,要求主键值不能为空,否则会抛出异常。同时,数据为 null 的字段 **不会** + 更新到数据库。 +- **update(entity, ignoreNulls)**:根据主键更新到 entity 到数据库,要求主键值不能为空。ignoreNulls 为是否忽略 null 字段,如果为 + false,所有 null 字段都会更新到数据库。 +- **updateByMap(entity, map)**:根据 `map<字段名,值>` 组成的条件更新到 entity 到数据库,entity 可以没有主键(如果有也会被忽略), + entity 的 null 属性,会自动被忽略。 +- **updateByCondition(entity, condition)**:根据 condition 构建的条件更新到 entity 到数据库,entity 可以没有主键(如果有也会被忽略), + entity 的 null 属性,会自动被忽略。 +- **updateByCondition(entity, ignoreNulls, condition)**:ignoreNulls 是否忽略 null 值,默认为 true,如果为 false,所有 null + 字段都会更新到数据库。 +- **updateByQuery(entity, queryWrapper)**:根据 queryWrapper 组成的条件更新到 entity 到数据库,entity 可以没有主键(如果有也会被忽略), + entity 的 null 属性,会自动被忽略。 +- **updateByQuery(entity, ignoreNulls, queryWrapper)**:据 queryWrapper 组成的条件更新到 entity 到数据库,entity + 可以没有主键(如果有也会被忽略)。 ignoreNulls 用于是否忽略 entity 的 null 属性 + , 若 ignoreNulls 为 false,entity 的所有 null 属性都会被更新到数据库。 +- **updateNumberAddByQuery(fieldName,value,queryWrapper)**:通过 `update table set field = field + 1 where ... ` + 的这种方向更新数据库某个字段内容。 +- **updateNumberAddByQuery(column,value,queryWrapper)**:通过 `update table set field = field + 1 where ... ` + 的这种方向更新数据库某个字段内容。 +- **updateNumberAddByQuery(fn,value,queryWrapper)**:通过 `update table set field = field + 1 where ... ` + 的这种方向更新数据库某个字段内容。 ## 部分字段更新 diff --git a/docs/zh/base/query.md b/docs/zh/base/query.md index f8de5715..640f0b29 100644 --- a/docs/zh/base/query.md +++ b/docs/zh/base/query.md @@ -8,22 +8,29 @@ - **selectOneByMap(map)**:根据 `map<字段名,值>` 组成的条件查询 1 条数据,若命中多条数据,则只返回第一条数据。 - **selectOneByCondition(condition)**:根据 condition 组成的条件查询 1 条数据,若命中多条数据,则只返回第一条数据。 - **selectOneByQuery(query)**:根据 QueryWrapper 组成的条件查询 1 条数据,若命中多条数据,**则抛出错误!!!**。一般情况下,用户可以主动添加 `limit(1)` 来阻止返回多条的情况。 -- **selectOneByQueryAs(query, asType)**:和 `selectOneByQuery` 方法类似,但是在某些场景下,`query` 可能包含了 `left join` 等多表查询,返回的数据和 entity 字段不一致时, -可以通过 `asType` 参数来指定接收的数据类型(通常是 dto、vo 等)。 +- **selectOneByQueryAs(query, asType)**:和 `selectOneByQuery` 方法类似,但是在某些场景下,`query` 可能包含了 `left join` + 等多表查询,返回的数据和 entity 字段不一致时, + 可以通过 `asType` 参数来指定接收的数据类型(通常是 dto、vo 等)。 - **selectListByIds(idList)**:根据多个 id 查询,返回多条数据 - **selectListByMap(map)**:根据 `map<字段名,值>` 组成的条件查询数据。 - **selectListByMap(map, count)**:根据 `map<字段名,值>` 组成的条件查询数据,只取前 count 条。 - **selectListByCondition(condition)**:根据 condition 组成的条件查询数据。 -- **selectListByCondition(condition, count)**:根据 condition 组成的条件查询数据,只取前 count 条。 -- **selectListByQuery(query)**: 根据 QueryWrapper 组成的条件查询数据。 -- **selectListByQueryAs(query, asType)**: 和 `selectListByQuery` 方法类似,但是在某些场景下,`query` 可能包含了 `left join` 等多表查询,返回的数据和 entity 字段不一致时, +- **selectListByCondition(condition, count)**:根据 condition 组成的条件查询数据,只取前 count 条。 +- **selectListByQuery(query)**: 根据 QueryWrapper 组成的条件查询数据。 +- **selectListByQuery(query, consumers)**: 根据 QueryWrapper 组成的条件查询数据。 +- **selectListByQueryAs(query, asType)**: 和 `selectListByQuery` 方法类似,但是在某些场景下,`query` + 可能包含了 `left join` 等多表查询,返回的数据和 entity 字段不一致时, + 可以通过 `asType` 参数来指定接收的数据类型(通常是 dto、vo 等)。 +- **selectListByQueryAs(query, asType, consumers)**: 和 `selectListByQuery` 方法类似,但是在某些场景下,`query` + 可能包含了 `left join` 等多表查询,返回的数据和 entity 字段不一致时, 可以通过 `asType` 参数来指定接收的数据类型(通常是 dto、vo 等)。 - **selectAll**:查询所有数据。 - **selectObjectByQuery(query)**:查询只返回 1 列,并只有 1 条数据的场景。 +- **selectObjectByQueryAs(query)**:查询只返回 1 列,并只有 1 条数据的场景。 - **selectObjectListByQuery(query)**:查询只返回 1 列场景,比如 `QueryWrapper.create().select(ACCOINT.ID).from(...)`。 - **selectObjectListByQueryAs(query, asType)**:对 `selectObjectListByQuery` 进行封装,并转换为特定的类型。 -- **selectCountByCondition**:根据 QueryWrapper 查询数据量。 -- **selectCountByQuery**:根据 QueryWrapper 查询数据量。 +- **selectCountByCondition(condition)**:根据 QueryWrapper 查询数据量。 +- **selectCountByQuery(queryWrapper)**:根据 QueryWrapper 查询数据量。 ## 游标查询 @@ -55,7 +62,32 @@ Db.tx(() -> { - 1、数据查询并写入到缓存 - 2、Excel 导出等 +## 查询 Map 集合 +```java +List selectRowsByQuery(QueryWrapper queryWrapper); +``` + +## Relations 注解查询 + +- **selectOneWithRelationsByMap(map)** +- **selectOneWithRelationsByCondition(condition)** +- **selectOneWithRelationsByQuery(queryWrapper)** +- **selectOneWithRelationsByQueryAs(queryWrapper, asType)** +- **selectListWithRelationsByQuery(queryWrapper)** +- **selectListWithRelationsByQueryAs(queryWrapper, asType)** +- **selectListWithRelationsByQueryAs(queryWrapper, asType, consumers)** +- **selectAllWithRelations()** +- **paginateWithRelations(pageNumber, pageSize, queryWrapper)** +- **paginateWithRelations(pageNumber, pageSize, condition)** +- **paginateWithRelations(pageNumber, pageSize, totalRow, queryWrapper)** +- **paginateWithRelations(pageNumber, pageSize, totalRow, condition)** +- **paginateWithRelations(page, queryWrapper)** +- **paginateWithRelations(page, queryWrapper, consumers)** +- **paginateWithRelationsAs(pageNumber, pageSize, queryWrapper, asType)** +- **paginateWithRelationsAs(pageNumber, pageSize, totalRow, queryWrapper, asType)** +- **paginateWithRelationsAs(page, queryWrapper, asType)** +- **paginateWithRelationsAs(page, queryWrapper, asType, consumers)** ## 多表查询(关联查询) @@ -201,11 +233,20 @@ System.out.println(results); 在 MyBatis-Flex 的 BaseMapper 中,提供了如下的分页查询功能: ```java -Page paginate(int pageNumber, int pageSize, QueryWrapper queryWrapper); -Page paginate(int pageNumber, int pageSize, int totalRow, QueryWrapper queryWrapper); +default Page paginate(int pageNumber,int pageSize,QueryWrapper queryWrapper); +default Page paginate(int pageNumber,int pageSize,QueryCondition whereConditions); -Page paginate(int pageNumber, int pageSize, QueryCondition condition); -Page paginate(int pageNumber, int pageSize, int totalRow, QueryCondition condition); +default Page paginate(int pageNumber,int pageSize,int totalRow,QueryWrapper queryWrapper); +default Page paginate(int pageNumber,int pageSize,int totalRow,QueryCondition whereConditions); + +default Page paginate(Page page,QueryWrapper queryWrapper); +default Page paginate(Page page,QueryWrapper queryWrapper,Consumer>...consumers); + +default Page paginateAs(int pageNumber,int pageSize,QueryWrapper queryWrapper,Class asType); +default Page paginateAs(int pageNumber,int pageSize,int totalRow,QueryWrapper queryWrapper,Class asType) + +default Page paginateAs(Page page,QueryWrapper queryWrapper,Class asType) +default Page paginateAs(Page page,QueryWrapper queryWrapper,Class asType,Consumer>...consumers) ``` - pageNumber: 当前页码,从 1 开始 - pageSize: 每 1 页的数据量