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 49a22333..7a8919d7 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 @@ -16,7 +16,7 @@ package com.mybatisflex.core; import com.mybatisflex.core.constant.FuncName; -import com.mybatisflex.core.exception.FlexExceptions; +import com.mybatisflex.core.exception.FlexAssert; import com.mybatisflex.core.field.FieldQueryBuilder; import com.mybatisflex.core.mybatis.MappedStatementTypes; import com.mybatisflex.core.paginate.Page; @@ -36,63 +36,78 @@ import java.util.function.Consumer; import static com.mybatisflex.core.query.QueryMethods.count; +/** + * 通用 Mapper 接口。 + * + * @param 实体类类型 + * @author 开源海哥 + * @author 庄佳彬 + * @author 闵柳华 + * @author 王帅 + * @author yangs + * @author lhzsdnu + */ +@SuppressWarnings({"varargs", "unchecked", "unused"}) public interface BaseMapper { /** - * 插入 entity 数据 + * 插入实体类数据。 * * @param entity 实体类 - * @return 返回影响的行数 + * @return 受影响的行数 */ default int insert(T entity) { return insert(entity, false); } - /** - * 插入 entity 数据,但是忽略 null 的数据,只对有值的内容进行插入 - * 这样的好处是数据库已经配置了一些默认值,这些默认值才会生效 + * 插入实体类数据,但是忽略 {@code null} 的数据,只对有值的内容进行插入。 + * 这样的好处是数据库已经配置了一些默认值,这些默认值才会生效。 * * @param entity 实体类 - * @return 返回影响的行数 + * @return 受影响的行数 */ default int insertSelective(T entity) { return insert(entity, true); } - /** - * 插入 entity 数据 + * 插入实体类数据。 * - * @param entity 实体类 - * @return 返回影响的行数 + * @param entity 实体类 + * @param ignoreNulls 是否忽略 {@code null} 值 + * @return 受影响的行数 * @see com.mybatisflex.core.provider.EntitySqlProvider#insert(Map, ProviderContext) */ @InsertProvider(type = EntitySqlProvider.class, method = "insert") int insert(@Param(FlexConsts.ENTITY) T entity, @Param(FlexConsts.IGNORE_NULLS) boolean ignoreNulls); - + /** + * 插入带有主键的实体类。 + * + * @param entity 实体类 + * @return 受影响的行数 + */ default int insertWithPk(T entity) { return insertWithPk(entity, true); } /** - * 带有主键的插入,此时 entity 不会经过主键生成器生成主键 + * 带有主键的插入,此时实体类不会经过主键生成器生成主键。 * * @param entity 带有主键的实体类 - * @param ignoreNulls 是否忽略 null 值 - * @return 返回影响的行数 + * @param ignoreNulls 是否忽略 {@code null} 值 + * @return 受影响的行数 * @see com.mybatisflex.core.provider.EntitySqlProvider#insertWithPk(Map, ProviderContext) */ @InsertProvider(type = EntitySqlProvider.class, method = "insertWithPk") int insertWithPk(@Param(FlexConsts.ENTITY) T entity, @Param(FlexConsts.IGNORE_NULLS) boolean ignoreNulls); - /** - * 批量插入 entity 数据,只会根据第一条数据来构建插入的字段内容 + * 批量插入实体类数据,只会根据第一条数据来构建插入的字段内容。 * * @param entities 插入的数据列表 - * @return 返回影响的行数 + * @return 受影响的行数 * @see com.mybatisflex.core.provider.EntitySqlProvider#insertBatch(Map, ProviderContext) * @see com.mybatisflex.core.FlexConsts#METHOD_INSERT_BATCH */ @@ -100,15 +115,16 @@ public interface BaseMapper { int insertBatch(@Param(FlexConsts.ENTITIES) List entities); /** - * 批量插入 entity 数据,按 size 切分 + * 批量插入实体类数据,按 size 切分。 * * @param entities 插入的数据列表 * @param size 切分大小 - * @return 影响行数 + * @return 受影响的行数 */ default int insertBatch(List entities, int size) { if (size <= 0) { - size = 1000;//默认1000 + // 默认 1000 + size = 1000; } int sum = 0; int entitiesSize = entities.size(); @@ -121,22 +137,21 @@ public interface BaseMapper { } /** - * 新增 或者 更新,若主键有值,则更新,若没有主键值,则插入 + * 新增 或者 更新,若主键有值,则更新,若没有主键值,则插入。 * * @param entity 实体类 - * @return 返回影响的行数 + * @return 受影响的行数 */ default int insertOrUpdate(T entity) { return insertOrUpdate(entity, false); } - /** - * 新增 或者 更新,若主键有值,则更新,若没有主键值,则插入 + * 新增 或者 更新,若主键有值,则更新,若没有主键值,则插入。 * * @param entity 实体类 - * @param ignoreNulls 是否忽略 null 值 - * @return 返回影响的行数 + * @param ignoreNulls 是否忽略 {@code null} 值 + * @return 受影响的行数 */ default int insertOrUpdate(T entity, boolean ignoreNulls) { TableInfo tableInfo = TableInfoFactory.ofEntityClass(entity.getClass()); @@ -149,33 +164,31 @@ public interface BaseMapper { } /** - * 根据 id 删除数据 - * 如果是多个主键的情况下,需要传入数组 new Object[]{100,101} + * 根据主键删除数据。如果是多个主键的情况下,需要传入数组,例如:{@code new Integer[]{100,101}}。 * * @param id 主键数据 - * @return 返回影响的行数 + * @return 受影响的行数 * @see com.mybatisflex.core.provider.EntitySqlProvider#deleteById(Map, ProviderContext) */ @DeleteProvider(type = EntitySqlProvider.class, method = "deleteById") int deleteById(@Param(FlexConsts.PRIMARY_VALUE) Serializable id); - /** - * 根据多个 id 批量删除数据 + * 根据多个主键批量删除数据。 * - * @param ids ids 列表 - * @return 返回影响的行数 + * @param ids 主键列表 + * @return 受影响的行数 * @see com.mybatisflex.core.provider.EntitySqlProvider#deleteBatchByIds(Map, ProviderContext) */ @DeleteProvider(type = EntitySqlProvider.class, method = "deleteBatchByIds") int deleteBatchByIds(@Param(FlexConsts.PRIMARY_VALUE) Collection ids); /** - * 根据多个 id 批量删除数据 + * 根据多个主键批量删除数据。 * - * @param ids ids 列表 + * @param ids 主键列表 * @param size 切分大小 - * @return 返回影响的行数 + * @return 受影响的行数 * @see com.mybatisflex.core.provider.EntitySqlProvider#deleteBatchByIds(Map, ProviderContext) */ default int deleteBatchByIds(List ids, int size) { @@ -192,139 +205,123 @@ public interface BaseMapper { return sum; } - /** - * 根据 map 构建的条件来删除数据 + * 根据 Map 构建的条件来删除数据。 * * @param whereConditions 条件 - * @return 返回影响的行数 + * @return 受影响的行数 */ default int deleteByMap(Map whereConditions) { - if (whereConditions == null || whereConditions.isEmpty()) { - throw FlexExceptions.wrap("deleteByMap is not allow empty map."); - } + FlexAssert.notEmpty(whereConditions, "deleteByMap is not allow empty map."); return deleteByQuery(QueryWrapper.create().where(whereConditions)); } /** - * 根据条件来删除数据 + * 根据查询条件来删除数据。 * - * @param condition 条件 - * @return 返回影响的行数 + * @param whereConditions 条件 + * @return 受影响的行数 */ - default int deleteByCondition(QueryCondition condition) { - if (condition == null) { - throw FlexExceptions.wrap("condition can not be null."); - } - return deleteByQuery(QueryWrapper.create().where(condition)); + default int deleteByCondition(QueryCondition whereConditions) { + FlexAssert.notNull(whereConditions, "whereConditions can not be null."); + return deleteByQuery(QueryWrapper.create().where(whereConditions)); } /** - * 根据 query 构建的条件来数据吗 + * 根据查询条件来删除数据。 * - * @param queryWrapper query 条件 - * @return 返回影响的行数 + * @param queryWrapper 条件 + * @return 受影响的行数 * @see com.mybatisflex.core.provider.EntitySqlProvider#deleteByQuery(Map, ProviderContext) */ @DeleteProvider(type = EntitySqlProvider.class, method = "deleteByQuery") int deleteByQuery(@Param(FlexConsts.QUERY) QueryWrapper queryWrapper); - /** - * 根据主键来更新数据,若 entity 属性数据为 null,该属性不会新到数据库 + * 根据主键来更新数据,若实体类属性数据为 {@code null},该属性不会新到数据库。 * * @param entity 数据内容,必须包含有主键 - * @return 返回影响的行数 + * @return 受影响的行数 */ default int update(T entity) { return update(entity, true); } /** - * 根据主键来更新数据到数据库 + * 根据主键来更新数据到数据库。 * - * @param entity 数据内容 + * @param entity 数据内容,必须包含有主键 * @param ignoreNulls 是否忽略空内容字段 - * @return 返回影响的行数 + * @return 受影响的行数 * @see com.mybatisflex.core.provider.EntitySqlProvider#update(Map, ProviderContext) */ @UpdateProvider(type = EntitySqlProvider.class, method = "update") int update(@Param(FlexConsts.ENTITY) T entity, @Param(FlexConsts.IGNORE_NULLS) boolean ignoreNulls); - /** - * 根据 map 构建的条件来更新数据 + * 根据 Map 构建的条件来更新数据。 * - * @param entity 数据 - * @param map where 条件内容 - * @return 返回影响的行数 + * @param entity 实体类 + * @param map 条件 + * @return 受影响的行数 */ default int updateByMap(T entity, Map map) { - if (map == null || map.isEmpty()) { - throw FlexExceptions.wrap("updateByMap is not allow empty map."); - } + FlexAssert.notEmpty(map, "updateByMap is not allow empty map."); return updateByQuery(entity, QueryWrapper.create().where(map)); } /** - * 根据 condition 来更新数据 + * 根据查询条件来更新数据。 * - * @param entity 数据 - * @param condition 条件 - * @return 返回影响的行数 + * @param entity 实体类 + * @param whereConditions 条件 + * @return 受影响的行数 */ - default int updateByCondition(T entity, QueryCondition condition) { - if (condition == null) { - throw FlexExceptions.wrap("condition can not be null."); - } - return updateByQuery(entity, QueryWrapper.create().where(condition)); + default int updateByCondition(T entity, QueryCondition whereConditions) { + FlexAssert.notNull(whereConditions, "whereConditions can not be null."); + return updateByQuery(entity, QueryWrapper.create().where(whereConditions)); } /** - * 根据 condition 来更新数据 + * 根据查询条件来更新数据。 * - * @param entity 数据 - * @param ignoreNulls 是否忽略 null 数据,默认为 true - * @param condition 条件 - * @return 返回影响的行数 + * @param entity 实体类 + * @param ignoreNulls 是否忽略 {@code null} 数据 + * @param whereConditions 条件 + * @return 受影响的行数 */ - default int updateByCondition(T entity, boolean ignoreNulls, QueryCondition condition) { - if (condition == null) { - throw FlexExceptions.wrap("condition can not be null."); - } - return updateByQuery(entity, ignoreNulls, QueryWrapper.create().where(condition)); + default int updateByCondition(T entity, boolean ignoreNulls, QueryCondition whereConditions) { + FlexAssert.notNull(whereConditions, "whereConditions can not be null."); + return updateByQuery(entity, ignoreNulls, QueryWrapper.create().where(whereConditions)); } - /** - * 根据 query 构建的条件来更新数据 + * 根据查询条件来更新数据。 * - * @param entity 数据内容 - * @param queryWrapper query 条件 - * @return 返回影响的行数 + * @param entity 实体类 + * @param queryWrapper 条件 + * @return 受影响的行数 */ - default int updateByQuery(T entity, QueryWrapper queryWrapper) { return updateByQuery(entity, true, queryWrapper); } /** - * 根据 query 构建的条件来更新数据 + * 根据查询条件来更新数据。 * - * @param entity 数据内容 + * @param entity 实体类 * @param ignoreNulls 是否忽略空值 - * @param queryWrapper query 条件 + * @param queryWrapper 条件 * @see com.mybatisflex.core.provider.EntitySqlProvider#updateByQuery(Map, ProviderContext) */ @UpdateProvider(type = EntitySqlProvider.class, method = "updateByQuery") int updateByQuery(@Param(FlexConsts.ENTITY) T entity, @Param(FlexConsts.IGNORE_NULLS) boolean ignoreNulls, @Param(FlexConsts.QUERY) QueryWrapper queryWrapper); - /** - * 执行类似 update table set field=field+1 where ... 的场景 + * 执行类似 {@code update table set field = field + 1 where ... } 的场景。 * * @param fieldName 字段名 - * @param value 值( >=0 加,小于 0 减) + * @param value 值(大于等于 0 加,小于 0 减) * @param queryWrapper 条件 * @see EntitySqlProvider#updateNumberAddByQuery(Map, ProviderContext) */ @@ -332,154 +329,131 @@ public interface BaseMapper { int updateNumberAddByQuery(@Param(FlexConsts.FIELD_NAME) String fieldName, @Param(FlexConsts.VALUE) Number value, @Param(FlexConsts.QUERY) QueryWrapper queryWrapper); /** - * 执行类似 update table set field=field+1 where ... 的场景 + * 执行类似 {@code update table set field = field + 1 where ... } 的场景。 * * @param column 字段名 - * @param value 值( >=0 加,小于 0 减) + * @param value 值(大于等于 0 加,小于 0 减) * @param queryWrapper 条件 * @see EntitySqlProvider#updateNumberAddByQuery(Map, ProviderContext) */ default int updateNumberAddByQuery(QueryColumn column, Number value, QueryWrapper queryWrapper) { - if (value == null) { - throw FlexExceptions.wrap("value can not be null."); - } + FlexAssert.notNull(value, "add value can not be null."); return updateNumberAddByQuery(column.getName(), value, queryWrapper); } /** - * 执行类似 update table set field=field+1 where ... 的场景 + * 执行类似 {@code update table set field = field + 1 where ... } 的场景。 * * @param fn 字段名 - * @param value 值( >=0 加,小于 0 减) + * @param value 值(大于等于 0 加,小于 0 减) * @param queryWrapper 条件 * @see EntitySqlProvider#updateNumberAddByQuery(Map, ProviderContext) */ default int updateNumberAddByQuery(LambdaGetter fn, Number value, QueryWrapper queryWrapper) { - if (value == null) { - throw FlexExceptions.wrap("value can not be null."); - } - + FlexAssert.notNull(value, "add value can not be null."); TableInfo tableInfo = TableInfoFactory.ofMapperClass(ClassUtil.getUsefulClass(getClass())); String column = tableInfo.getColumnByProperty(LambdaUtil.getFieldName(fn)); return updateNumberAddByQuery(column, value, queryWrapper); } - /** - * 根据主键来选择数据 + * 根据主键查询数据。 * - * @param id 多个主键 - * @return entity + * @param id 主键 + * @return 实体类数据 * @see com.mybatisflex.core.provider.EntitySqlProvider#selectOneById(Map, ProviderContext) */ @SelectProvider(type = EntitySqlProvider.class, method = "selectOneById") T selectOneById(@Param(FlexConsts.PRIMARY_VALUE) Serializable id); - /** - * 根据 map 构建的条件来查询数据 + * 根据 Map 构建的条件来查询数据。 * - * @param map where 条件 - * @return entity 数据 + * @param whereConditions 条件 + * @return 实体类数据 */ - default T selectOneByMap(Map map) { - if (map == null || map.isEmpty()) { - throw FlexExceptions.wrap("map can not be null or empty."); - } - return selectOneByQuery(QueryWrapper.create().where(map).limit(1)); + default T selectOneByMap(Map whereConditions) { + FlexAssert.notEmpty(whereConditions, "whereConditions map can not be null or empty."); + return selectOneByQuery(QueryWrapper.create().where(whereConditions).limit(1)); } - /** - * 根据 condition 来查询数据 + * 根据查询条件查询数据。 * - * @param condition 条件 - * @return 1 条数据 + * @param whereConditions 条件 + * @return 实体类数据 */ - default T selectOneByCondition(QueryCondition condition) { - if (condition == null) { - throw FlexExceptions.wrap("condition can not be null."); - } - return selectOneByQuery(QueryWrapper.create().where(condition).limit(1)); + default T selectOneByCondition(QueryCondition whereConditions) { + FlexAssert.notNull(whereConditions, "whereConditions can not be null."); + return selectOneByQuery(QueryWrapper.create().where(whereConditions).limit(1)); } - /** - * 根据 queryWrapper 构建的条件来查询 1 条数据 + * 根据查询条件来查询 1 条数据。 * - * @param queryWrapper query 条件 - * @return entity 数据 + * @param queryWrapper 条件 + * @return 实体类数据 */ default T selectOneByQuery(QueryWrapper queryWrapper) { return MapperUtil.getSelectOneResult(selectListByQuery(queryWrapper)); } - /** - * 根据 queryWrapper 构建的条件来查询 1 条数据 + * 根据查询条件来查询 1 条数据。 * - * @param queryWrapper query 条件 - * @param asType 接收类型 + * @param queryWrapper 条件 + * @param asType 接收数据类型 * @return 数据内容 */ default R selectOneByQueryAs(QueryWrapper queryWrapper, Class asType) { return MapperUtil.getSelectOneResult(selectListByQueryAs(queryWrapper, asType)); } - /** - * 根据 map 构建的条件来查询数据 + * 根据 Map 构建的条件来查询数据。 * - * @param map where 条件 - * @return entity 数据 + * @param whereConditions 条件 + * @return 实体类数据 */ - default T selectOneWithRelationsByMap(Map map) { - if (map == null || map.isEmpty()) { - throw FlexExceptions.wrap("map can not be null or empty."); - } - return selectOneWithRelationsByQuery(QueryWrapper.create().where(map).limit(1)); + default T selectOneWithRelationsByMap(Map whereConditions) { + FlexAssert.notEmpty(whereConditions, "whereConditions map can not be null or empty."); + return selectOneWithRelationsByQuery(QueryWrapper.create().where(whereConditions).limit(1)); } - /** - * 根据 condition 来查询数据 + * 根据查询条件查询数据。 * - * @param condition 条件 - * @return 1 条数据 + * @param whereConditions 条件 + * @return 实体类数据 */ - default T selectOneWithRelationsByCondition(QueryCondition condition) { - if (condition == null) { - throw FlexExceptions.wrap("condition can not be null."); - } - return selectOneWithRelationsByQuery(QueryWrapper.create().where(condition).limit(1)); + default T selectOneWithRelationsByCondition(QueryCondition whereConditions) { + FlexAssert.notNull(whereConditions, "whereConditions can not be null."); + return selectOneWithRelationsByQuery(QueryWrapper.create().where(whereConditions).limit(1)); } - /** - * 根据 queryWrapper 构建的条件来查询 1 条数据 + * 根据查询条件来查询 1 条数据。 * - * @param queryWrapper query 条件 - * @return entity 数据 + * @param queryWrapper 条件 + * @return 实体类数据 */ default T selectOneWithRelationsByQuery(QueryWrapper queryWrapper) { return MapperUtil.queryRelations(this, MapperUtil.getSelectOneResult(selectListByQuery(queryWrapper))); } - /** - * 根据 queryWrapper 构建的条件来查询 1 条数据 + * 根据查询条件来查询 1 条数据。 * - * @param queryWrapper query 条件 - * @param asType 接收类型 + * @param queryWrapper 条件 + * @param asType 接收数据类型 * @return 数据内容 */ default R selectOneWithRelationsByQueryAs(QueryWrapper queryWrapper, Class asType) { return MapperUtil.queryRelations(this, MapperUtil.getSelectOneResult(selectListByQueryAs(queryWrapper, asType))); } - /** - * 根据多个主键来查询多条数据 + * 根据多个主键来查询多条数据。 * * @param ids 主键列表 * @return 数据列表 @@ -488,113 +462,101 @@ public interface BaseMapper { @SelectProvider(type = EntitySqlProvider.class, method = "selectListByIds") List selectListByIds(@Param(FlexConsts.PRIMARY_VALUE) Collection ids); - /** - * 根据 map 来构建查询条件,查询多条数据 + * 根据 Map 来构建查询条件,查询多条数据。 * - * @param map 条件列表 + * @param whereConditions 条件 * @return 数据列表 */ - default List selectListByMap(Map map) { - if (map == null || map.isEmpty()) { - throw FlexExceptions.wrap("map can not be null or empty."); - } - return selectListByQuery(QueryWrapper.create().where(map)); + default List selectListByMap(Map whereConditions) { + FlexAssert.notEmpty(whereConditions, "whereConditions map can not be null or empty."); + return selectListByQuery(QueryWrapper.create().where(whereConditions)); } - /** - * 根据 map 来构建查询条件,查询多条数据 + * 根据 Map 来构建查询条件,查询多条数据。 * - * @param map 条件列表 + * @param whereConditions 条件 + * @param count 数据量 * @return 数据列表 */ - default List selectListByMap(Map map, int count) { - if (map == null || map.isEmpty()) { - throw FlexExceptions.wrap("map can not be null or empty."); - } - return selectListByQuery(QueryWrapper.create().where(map).limit(count)); + default List selectListByMap(Map whereConditions, int count) { + FlexAssert.notEmpty(whereConditions, "whereConditions map can not be null or empty."); + return selectListByQuery(QueryWrapper.create().where(whereConditions).limit(count)); } - /** - * 根据 condition 来查询数据 + * 根据查询条件查询数据。 * - * @param condition condition 条件 + * @param whereConditions 条件 * @return 数据列表 */ - default List selectListByCondition(QueryCondition condition) { - if (condition == null) { - throw FlexExceptions.wrap("condition can not be null."); - } - return selectListByQuery(QueryWrapper.create().where(condition)); + default List selectListByCondition(QueryCondition whereConditions) { + FlexAssert.notNull(whereConditions, "whereConditions can not be null."); + return selectListByQuery(QueryWrapper.create().where(whereConditions)); } - /** - * 根据 condition 来查询数据 + * 根据查询条件查询数据。 * - * @param condition condition 条件 - * @param count 数据量 + * @param whereConditions 条件 + * @param count 数据量 * @return 数据列表 */ - default List selectListByCondition(QueryCondition condition, int count) { - if (condition == null) { - throw FlexExceptions.wrap("condition can not be null."); - } - return selectListByQuery(QueryWrapper.create().where(condition).limit(count)); + default List selectListByCondition(QueryCondition whereConditions, int count) { + FlexAssert.notNull(whereConditions, "whereConditions can not be null."); + return selectListByQuery(QueryWrapper.create().where(whereConditions).limit(count)); } - /** - * 根据 query 来构建条件查询数据列表 + * 根据查询条件查询数据列表。 * - * @param queryWrapper 查询条件 + * @param queryWrapper 条件 * @return 数据列表 * @see com.mybatisflex.core.provider.EntitySqlProvider#selectListByQuery(Map, ProviderContext) */ @SelectProvider(type = EntitySqlProvider.class, method = "selectListByQuery") List selectListByQuery(@Param(FlexConsts.QUERY) QueryWrapper queryWrapper); - - default List selectListByQuery(QueryWrapper queryWrapper - , Consumer>... consumers) { - + /** + * 根据查询条件查询数据列表。 + * + * @param queryWrapper 条件 + * @param consumers 字段查询 + * @return 数据列表 + */ + default List selectListByQuery(QueryWrapper queryWrapper, Consumer>... consumers) { List list = selectListByQuery(queryWrapper); if (list == null || list.isEmpty()) { return Collections.emptyList(); } - MapperUtil.queryFields(this, list, consumers); - return list; } /** - * 根据 query 来构建条件查询游标数据 Cursor - * 该方法必须在事务中才能正常使用,非事务下无法获取数据 + * 根据查询条件查询游标数据,该方法必须在事务中才能正常使用,非事务下无法获取数据。 * - * @param queryWrapper 查询条件 - * @return 游标数据 Cursor + * @param queryWrapper 条件 + * @return 游标数据 */ @SelectProvider(type = EntitySqlProvider.class, method = "selectListByQuery") Cursor selectCursorByQuery(@Param(FlexConsts.QUERY) QueryWrapper queryWrapper); /** - * 根据 query 来构建条件查询 Row + * 根据查询条件查询 Row 数据。 * - * @param queryWrapper 查询条件 - * @return Row + * @param queryWrapper 条件 + * @return 行数据 */ @SelectProvider(type = EntitySqlProvider.class, method = "selectListByQuery") List selectRowsByQuery(@Param(FlexConsts.QUERY) QueryWrapper queryWrapper); - /** - * 根据 query 来构建条件查询数据列表,要求返回的数据为 asType - * 这种场景一般用在 left join 时,有多出了 entity 本身的字段内容,可以转换为 dto、vo 等场景时 + * 根据查询条件查询数据列表,要求返回的数据为 asType。这种场景一般用在 left join 时, + * 有多出了实体类本身的字段内容,可以转换为 dto、vo 等场景时。 * - * @param queryWrapper 查询条件 + * @param queryWrapper 条件 * @param asType 接收数据类型 * @return 数据列表 */ @@ -616,17 +578,15 @@ public interface BaseMapper { } } - /** - * 根据 query 来构建条件查询数据列表,要求返回的数据为 asType + * 根据查询条件查询数据列表,要求返回的数据为 asType 类型。 * - * @param queryWrapper 查询条件 + * @param queryWrapper 条件 * @param asType 接收的数据类型 * @param consumers 字段查询 * @return 数据列表 */ - default List selectListByQueryAs(QueryWrapper queryWrapper, Class asType - , Consumer>... consumers) { + default List selectListByQueryAs(QueryWrapper queryWrapper, Class asType, Consumer>... consumers) { List list = selectListByQueryAs(queryWrapper, asType); if (list == null || list.isEmpty()) { return Collections.emptyList(); @@ -636,21 +596,19 @@ public interface BaseMapper { } } - /** - * 查询 entity 及其 relation 注解字段 + * 查询实体类及其 relation 注解字段。 * - * @param queryWrapper 查询条件 + * @param queryWrapper 条件 */ default List selectListWithRelationsByQuery(QueryWrapper queryWrapper) { return MapperUtil.queryRelations(this, selectListByQuery(queryWrapper)); } - /** - * 查询 entity 及其 relation 注解字段 + * 查询实体类及其 relation 注解字段。 * - * @param queryWrapper 查询条件 + * @param queryWrapper 条件 * @param asType 要求返回的数据类型 * @return 数据列表 */ @@ -672,17 +630,15 @@ public interface BaseMapper { } } - /** - * 查询 entity 及其 relation 注解字段 + * 查询实体类及其 relation 注解字段。 * - * @param queryWrapper 查询条件 + * @param queryWrapper 条件 * @param asType 返回的类型 * @param consumers 字段查询 * @return 数据列表 */ - default List selectListWithRelationsByQueryAs(QueryWrapper queryWrapper, Class asType - , Consumer>... consumers) { + default List selectListWithRelationsByQueryAs(QueryWrapper queryWrapper, Class asType, Consumer>... consumers) { List list = selectListByQueryAs(queryWrapper, asType); if (list == null || list.isEmpty()) { return Collections.emptyList(); @@ -693,9 +649,8 @@ public interface BaseMapper { } } - /** - * 查询全部数据 + * 查询全部数据。 * * @return 数据列表 */ @@ -703,9 +658,8 @@ public interface BaseMapper { return selectListByQuery(new QueryWrapper()); } - /** - * 查询全部数据,及其 relation 字段内容 + * 查询全部数据,及其 relation 字段内容。 * * @return 数据列表 */ @@ -713,10 +667,9 @@ public interface BaseMapper { return MapperUtil.queryRelations(this, selectListByQuery(new QueryWrapper())); } - /** - * 根据 queryWrapper 1 条数据 - * queryWrapper 执行的结果应该只有 1 列,例如 QueryWrapper.create().select(ACCOUNT.id).where... + * 查询第一列返回的数据,QueryWrapper 执行的结果应该只有 1 列,例如:
+ * {@code QueryWrapper.create().select(ACCOUNT.id).where(...);} * * @param queryWrapper 查询包装器 * @return 数据量 @@ -726,8 +679,8 @@ public interface BaseMapper { } /** - * 根据 queryWrapper 1 条数据 - * queryWrapper 执行的结果应该只有 1 列,例如 QueryWrapper.create().select(ACCOUNT.id).where... + * 查询第一列返回的数据,QueryWrapper 执行的结果应该只有 1 列,例如:
+ * {@code QueryWrapper.create().select(ACCOUNT.id).where(...);} * * @param queryWrapper 查询包装器 * @param asType 转换成的数据类型 @@ -737,10 +690,9 @@ public interface BaseMapper { return MapperUtil.getSelectOneResult(selectObjectListByQueryAs(queryWrapper, asType)); } - /** - * 根据 queryWrapper 来查询数据列表 - * queryWrapper 执行的结果应该只有 1 列,例如 QueryWrapper.create().select(ACCOUNT.id).where... + * 查询第一列返回的数据集合,QueryWrapper 执行的结果应该只有 1 列,例如:
+ * {@code QueryWrapper.create().select(ACCOUNT.id).where(...);} * * @param queryWrapper 查询包装器 * @return 数据列表 @@ -749,11 +701,9 @@ public interface BaseMapper { @SelectProvider(type = EntitySqlProvider.class, method = "selectObjectByQuery") List selectObjectListByQuery(@Param(FlexConsts.QUERY) QueryWrapper queryWrapper); - /** - * 对 {@link #selectObjectListByQuery(QueryWrapper)} 进行数据转换 - * 根据 queryWrapper 来查询数据列表 - * queryWrapper 执行的结果应该只有 1 列,例如 QueryWrapper.create().select(ACCOUNT.id).where... + * 查询第一列返回的数据集合,QueryWrapper 执行的结果应该只有 1 列,例如:
+ * {@code QueryWrapper.create().select(ACCOUNT.id).where(...);} * * @param queryWrapper 查询包装器 * @param asType 转换成的数据类型 @@ -771,11 +721,10 @@ public interface BaseMapper { return results; } - /** - * 查询数据量 + * 查询数据量。 * - * @param queryWrapper 查询包装器 + * @param queryWrapper 条件 * @return 数据量 */ default long selectCountByQuery(QueryWrapper queryWrapper) { @@ -815,175 +764,175 @@ public interface BaseMapper { } } - /** - * 根据条件查询数据总量 + * 根据条件查询数据总量。 * - * @param condition 条件 + * @param whereConditions 条件 * @return 数据量 */ - default long selectCountByCondition(QueryCondition condition) { - if (condition == null) { - throw FlexExceptions.wrap("condition can not be null."); - } - return selectCountByQuery(QueryWrapper.create().where(condition)); + default long selectCountByCondition(QueryCondition whereConditions) { + FlexAssert.notNull(whereConditions, "whereConditions can not be null."); + return selectCountByQuery(QueryWrapper.create().where(whereConditions)); } - /** - * 分页查询 + * 分页查询。 * * @param pageNumber 当前页码 * @param pageSize 每页的数据量 - * @param queryWrapper 查询条件 - * @return 返回 Page 数据 + * @param queryWrapper 条件 + * @return 分页数据 */ default Page paginate(int pageNumber, int pageSize, QueryWrapper queryWrapper) { Page page = new Page<>(pageNumber, pageSize); return paginate(page, queryWrapper); } + /** + * 分页查询,及其 relation 字段内容。 + * + * @param pageNumber 当前页码 + * @param pageSize 每页的数据量 + * @param queryWrapper 条件 + * @return 分页数据 + */ default Page paginateWithRelations(int pageNumber, int pageSize, QueryWrapper queryWrapper) { Page page = new Page<>(pageNumber, pageSize); return paginateWithRelations(page, queryWrapper); } - /** - * 根据条件分页查询 + * 分页查询。 * - * @param pageNumber 当前页面 - * @param pageSize 每页的数据量 - * @param condition 查询条件 - * @return 返回 Page 数据 + * @param pageNumber 当前页码 + * @param pageSize 每页的数据量 + * @param whereConditions 条件 + * @return 分页数据 */ - default Page paginate(int pageNumber, int pageSize, QueryCondition condition) { + default Page paginate(int pageNumber, int pageSize, QueryCondition whereConditions) { Page page = new Page<>(pageNumber, pageSize); - return paginate(page, new QueryWrapper().where(condition)); - } - - default Page paginateWithRelations(int pageNumber, int pageSize, QueryCondition condition) { - Page page = new Page<>(pageNumber, pageSize); - return paginateWithRelations(page, new QueryWrapper().where(condition)); + return paginate(page, new QueryWrapper().where(whereConditions)); } /** - * 分页查询 + * 分页查询,及其 relation 字段内容。 + * + * @param pageNumber 当前页码 + * @param pageSize 每页的数据量 + * @param whereConditions 条件 + * @return 分页数据 + */ + default Page paginateWithRelations(int pageNumber, int pageSize, QueryCondition whereConditions) { + Page page = new Page<>(pageNumber, pageSize); + return paginateWithRelations(page, new QueryWrapper().where(whereConditions)); + } + + /** + * 分页查询。 * * @param pageNumber 当前页码 * @param pageSize 每页的数据量 * @param totalRow 数据总量 - * @param queryWrapper 查询条件 - * @return 返回 Page 数据 + * @param queryWrapper 条件 + * @return 分页数据 */ default Page paginate(int pageNumber, int pageSize, int totalRow, QueryWrapper queryWrapper) { Page page = new Page<>(pageNumber, pageSize, totalRow); return paginate(page, queryWrapper); } + /** + * 分页查询,及其 relation 字段内容。 + * + * @param pageNumber 当前页码 + * @param pageSize 每页的数据量 + * @param totalRow 数据总量 + * @param queryWrapper 条件 + * @return 分页数据 + */ default Page paginateWithRelations(int pageNumber, int pageSize, int totalRow, QueryWrapper queryWrapper) { Page page = new Page<>(pageNumber, pageSize, totalRow); return paginateWithRelations(page, queryWrapper); } - /** - * 根据条件分页查询 + * 分页查询。 * - * @param pageNumber 当前页面 - * @param pageSize 每页的数据量 - * @param totalRow 数据总量 - * @param condition 查询条件 - * @return 返回 Page 数据 + * @param pageNumber 当前页码 + * @param pageSize 每页的数据量 + * @param totalRow 数据总量 + * @param whereConditions 条件 + * @return 分页数据 */ - default Page paginate(int pageNumber, int pageSize, int totalRow, QueryCondition condition) { - if (condition == null) { - throw FlexExceptions.wrap("condition can not be null."); - } + default Page paginate(int pageNumber, int pageSize, int totalRow, QueryCondition whereConditions) { + FlexAssert.notNull(whereConditions, "whereConditions can not be null."); Page page = new Page<>(pageNumber, pageSize, totalRow); - return paginate(page, new QueryWrapper().where(condition)); + return paginate(page, new QueryWrapper().where(whereConditions)); } - - default Page paginateWithRelations(int pageNumber, int pageSize, int totalRow, QueryCondition condition) { - if (condition == null) { - throw FlexExceptions.wrap("condition can not be null."); - } - Page page = new Page<>(pageNumber, pageSize, totalRow); - return paginateWithRelations(page, new QueryWrapper().where(condition)); - } - - /** - * 分页查询 + * 分页查询。 * - * @param page page,其包含了页码、每页的数据量,可能包含数据总量 - * @param queryWrapper 查询条件 + * @param pageNumber 当前页码 + * @param pageSize 每页的数据量 + * @param totalRow 数据总量 + * @param whereConditions 条件 + * @return 分页数据 + */ + default Page paginateWithRelations(int pageNumber, int pageSize, int totalRow, QueryCondition whereConditions) { + FlexAssert.notNull(whereConditions, "whereConditions can not be null."); + Page page = new Page<>(pageNumber, pageSize, totalRow); + return paginateWithRelations(page, new QueryWrapper().where(whereConditions)); + } + + /** + * 分页查询。 + * + * @param page 包含了页码、每页的数据量,可能包含数据总量 + * @param queryWrapper 条件 + * @param consumers 字段查询 * @return page 数据 */ - @SuppressWarnings("unchecked") default Page paginate(Page page, QueryWrapper queryWrapper, Consumer>... consumers) { return paginateAs(page, queryWrapper, null, consumers); } + /** + * 分页查询。 + * + * @param page 包含了页码、每页的数据量,可能包含数据总量 + * @param queryWrapper 条件 + * @param consumers 字段查询 + * @return 分页数据 + */ default Page paginateWithRelations(Page page, QueryWrapper queryWrapper, Consumer>... consumers) { - return doPaginate(page, queryWrapper, null, true, consumers); + return paginateWithRelationsAs(page, queryWrapper, null, consumers); } - + /** + * 分页查询。 + * + * @param page 包含了页码、每页的数据量,可能包含数据总量 + * @param queryWrapper 条件 + * @param asType 接收数据类型 + * @param consumers 字段查询 + * @return 分页数据 + */ default Page paginateAs(Page page, QueryWrapper queryWrapper, Class asType, Consumer>... consumers) { - return doPaginate(page, queryWrapper, asType, false, consumers); + return MapperUtil.doPaginate(this, page, queryWrapper, asType, false, consumers); } + /** + * 分页查询。 + * + * @param page 包含了页码、每页的数据量,可能包含数据总量 + * @param queryWrapper 条件 + * @param asType 接收数据类型 + * @param consumers 字段查询 + * @return 分页数据 + */ default Page paginateWithRelationsAs(Page page, QueryWrapper queryWrapper, Class asType, Consumer>... consumers) { - return doPaginate(page, queryWrapper, asType, true, consumers); + return MapperUtil.doPaginate(this, page, queryWrapper, asType, true, consumers); } - - default Page doPaginate(Page page, QueryWrapper queryWrapper, Class asType, boolean withRelations, Consumer>... consumers) { - try { - // 只有 totalRow 小于 0 的时候才会去查询总量 - // 这样方便用户做总数缓存,而非每次都要去查询总量 - // 一般的分页场景中,只有第一页的时候有必要去查询总量,第二页以后是不需要的 - if (page.getTotalRow() < 0) { - QueryWrapper countQueryWrapper; - if (page.needOptimizeCountQuery()) { - countQueryWrapper = MapperUtil.optimizeCountQueryWrapper(queryWrapper); - } else { - countQueryWrapper = MapperUtil.rawCountQueryWrapper(queryWrapper); - } - page.setTotalRow(selectCountByQuery(countQueryWrapper)); - } - - if (page.isEmpty()) { - return page; - } - - queryWrapper.limit(page.offset(), page.getPageSize()); - - List records; - if (asType != null) { - records = selectListByQueryAs(queryWrapper, asType); - } else { - records = (List) selectListByQuery(queryWrapper); - } - - if (withRelations) { - MapperUtil.queryRelations(this, records); - } - - MapperUtil.queryFields(this, records, consumers); - page.setRecords(records); - - return page; - - } finally { - // 将之前设置的 limit 清除掉 - // 保险起见把重置代码放到 finally 代码块中 - CPI.setLimitRows(queryWrapper, null); - CPI.setLimitOffset(queryWrapper, null); - } - } - - }