From 2f26b713897ca3270432eacc3a6f3b886bb142df Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Mon, 31 Jul 2023 16:13:43 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=85=B3=E8=81=94=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E6=9E=84=E5=BB=BA=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../activerecord/query/AbstractQuery.java | 160 ++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 mybatis-flex-core/src/main/java/com/mybatisflex/core/activerecord/query/AbstractQuery.java diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/activerecord/query/AbstractQuery.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/activerecord/query/AbstractQuery.java new file mode 100644 index 00000000..3610dc87 --- /dev/null +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/activerecord/query/AbstractQuery.java @@ -0,0 +1,160 @@ +/* + * 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.activerecord.query; + +import com.mybatisflex.core.BaseMapper; +import com.mybatisflex.core.activerecord.Model; +import com.mybatisflex.core.paginate.Page; +import com.mybatisflex.core.query.QueryWrapper; + +import java.util.List; +import java.util.Optional; + +/** + * 抽象关联查询。 + * + * @author 王帅 + * @since 2023-07-30 + */ +public abstract class AbstractQuery> { + + protected final Model model; + + protected AbstractQuery(Model model) { + this.model = model; + } + + /** + * @return 主键 + */ + protected Object[] pkValues() { + return model.pkValues(); + } + + /** + * @return BaseMapper + */ + protected BaseMapper baseMapper() { + return model.baseMapper(); + } + + /** + * @return QueryWrapper + */ + protected QueryWrapper queryWrapper() { + return model.queryWrapper(); + } + + /** + * 根据实体类主键获取一条数据。 + * + * @return 数据 + */ + public abstract T oneById(); + + /** + * 根据实体类主键获取一条数据,并封装为 {@link Optional} 返回。 + * + * @return 数据 + */ + public Optional oneByIdOpt() { + return Optional.ofNullable(oneById()); + } + + /** + * 根据实体类主键获取一条数据。 + * + * @return 数据 + */ + public abstract R oneByIdAs(Class asType); + + /** + * 根据实体类主键获取一条数据,并封装为 {@link Optional} 返回。 + * + * @return 数据 + */ + public Optional oneByIdAsOpt(Class asType) { + return Optional.ofNullable(oneByIdAs(asType)); + } + + /** + * 根据实体类构建的条件获取一条数据。 + * + * @return 数据 + */ + public abstract T one(); + + /** + * 根据实体类构建的条件获取一条数据,返回的数据为 asType 类型。 + * + * @param asType 接收数据类型 + * @return 数据 + */ + public abstract R oneAs(Class asType); + + /** + * 根据实体类构建的条件获取一条数据,并封装为 {@link Optional} 返回。 + * + * @return 数据 + */ + public Optional oneOpt() { + return Optional.ofNullable(one()); + } + + /** + * 根据实体类构建的条件获取一条数据,返回的数据为 asType 类型,并封装为 {@link Optional} 返回。 + * + * @param asType 接收数据类型 + * @return 数据 + */ + public Optional oneOptAs(Class asType) { + return Optional.ofNullable(oneAs(asType)); + } + + /** + * 根据实体类构建的条件获取多条数据。 + * + * @return 数据列表 + */ + public abstract List list(); + + /** + * 根据实体类构建的条件获取多条数据,返回的数据为 asType 类型。 + * + * @param asType 接收数据类型 + * @return 数据列表 + */ + public abstract List listAs(Class asType); + + /** + * 根据实体类构建的条件获取分页数据。 + * + * @param page 分页对象 + * @return 分页数据 + */ + public abstract Page page(Page page); + + /** + * 根据实体类构建的条件获取分页数据,返回的数据为 asType 类型。 + * + * @param page 分页对象 + * @param asType 接收数据类型 + * @return 分页数据 + */ + public abstract Page pageAs(Page page, Class asType); + +}