001/* 002 * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). 003 * <p> 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * <p> 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * <p> 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.mybatisflex.core.query; 018 019import com.mybatisflex.core.BaseMapper; 020import com.mybatisflex.core.paginate.Page; 021import com.mybatisflex.core.util.SqlUtil; 022 023import java.util.List; 024import java.util.Optional; 025 026/** 027 * <p>链式 {@link BaseMapper} 查询。 028 * 029 * <p>要求实现类除了包含有 {@link BaseMapper} 接口的引用外,还必须具有 {@link QueryWrapper} 030 * 的查询条件构建功能。在使用时: 031 * <ul> 032 * <li>通过 {@link #baseMapper()} 获取该实现类对应的 {@link BaseMapper} 引用。 033 * <li>通过 {@link #toQueryWrapper()} 将该实现类转换为 {@link QueryWrapper} 对象。 034 * </ul> 035 * 036 * @param <T> 实体类类型 037 * @author 王帅 038 * @since 2023-08-08 039 */ 040public interface MapperQueryChain<T> extends ChainQuery<T> { 041 042 /** 043 * 该实现类对应的 {@link BaseMapper} 对象。 044 * 045 * @return {@link BaseMapper} 046 */ 047 BaseMapper<T> baseMapper(); 048 049 /** 050 * 将该实现类转换为 {@link QueryWrapper} 对象。 051 * 052 * @return {@link QueryWrapper} 053 */ 054 QueryWrapper toQueryWrapper(); 055 056 /** 057 * 查询数据数量。 058 * 059 * @return 数据数量 060 */ 061 default long count() { 062 return baseMapper().selectCountByQuery(toQueryWrapper()); 063 } 064 065 /** 066 * 判断数据是否存在。 067 * 068 * @return {@code true} 数据存在,{@code false} 数据不存在 069 */ 070 default boolean exists() { 071 return SqlUtil.toBool(count()); 072 } 073 074 /** 075 * {@inheritDoc} 076 */ 077 @Override 078 default T one() { 079 return baseMapper().selectOneByQuery(toQueryWrapper()); 080 } 081 082 /** 083 * {@inheritDoc} 084 */ 085 @Override 086 default <R> R oneAs(Class<R> asType) { 087 return baseMapper().selectOneByQueryAs(toQueryWrapper(), asType); 088 } 089 090 /** 091 * 获取第一列,且第一条数据。 092 * 093 * @return 第一列数据 094 */ 095 default Object obj() { 096 return baseMapper().selectObjectByQuery(toQueryWrapper()); 097 } 098 099 /** 100 * 获取第一列,且第一条数据并转换为指定类型,比如 {@code Long}, {@code String} 等。 101 * 102 * @param asType 接收数据类型 103 * @param <R> 接收数据类型 104 * @return 第一列数据 105 */ 106 default <R> R objAs(Class<R> asType) { 107 return baseMapper().selectObjectByQueryAs(toQueryWrapper(), asType); 108 } 109 110 /** 111 * 获取第一列,且第一条数据,并封装为 {@link Optional} 返回。 112 * 113 * @return 第一列数据 114 */ 115 default Optional<Object> objOpt() { 116 return Optional.ofNullable(obj()); 117 } 118 119 /** 120 * 获取第一列,且第一条数据并转换为指定类型,比如 {@code Long}, {@code String} 121 * 等,封装为 {@link Optional} 返回。 122 * 123 * @param asType 接收数据类型 124 * @param <R> 接收数据类型 125 * @return 第一列数据 126 */ 127 default <R> Optional<R> objAsOpt(Class<R> asType) { 128 return Optional.ofNullable(objAs(asType)); 129 } 130 131 /** 132 * 获取第一列的所有数据。 133 * 134 * @return 第一列数据 135 */ 136 default List<Object> objList() { 137 return baseMapper().selectObjectListByQuery(toQueryWrapper()); 138 } 139 140 /** 141 * 获取第一列的所有数据,并转换为指定类型,比如 {@code Long}, {@code String} 等。 142 * 143 * @param asType 接收数据类型 144 * @param <R> 接收数据类型 145 * @return 第一列数据 146 */ 147 default <R> List<R> objListAs(Class<R> asType) { 148 return baseMapper().selectObjectListByQueryAs(toQueryWrapper(), asType); 149 } 150 151 /** 152 * {@inheritDoc} 153 */ 154 default List<T> list() { 155 return baseMapper().selectListByQuery(toQueryWrapper()); 156 } 157 158 /** 159 * {@inheritDoc} 160 */ 161 default <R> List<R> listAs(Class<R> asType) { 162 return baseMapper().selectListByQueryAs(toQueryWrapper(), asType); 163 } 164 165 /** 166 * {@inheritDoc} 167 */ 168 default Page<T> page(Page<T> page) { 169 return baseMapper().paginate(page, toQueryWrapper()); 170 } 171 172 /** 173 * {@inheritDoc} 174 */ 175 default <R> Page<R> pageAs(Page<R> page, Class<R> asType) { 176 return baseMapper().paginateAs(page, toQueryWrapper(), asType); 177 } 178 179 /** 180 * 使用 {@code Fields Query} 的方式进行关联查询。 181 * 182 * @return {@code Fields Query} 查询 183 */ 184 default FieldsBuilder<T> withFields() { 185 return new FieldsBuilder<>(this); 186 } 187 188 /** 189 * 使用 {@code Relations Query} 的方式进行关联查询。 190 * 191 * @return {@code Relations Query} 查询 192 */ 193 default RelationsBuilder<T> withRelations() { 194 return new RelationsBuilder<>(this); 195 } 196 197}