001/* 002 * Copyright (c) 2022-2025, 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 */ 016package com.mybatisflex.core.field; 017 018import com.mybatisflex.core.exception.FlexAssert; 019import com.mybatisflex.core.util.LambdaGetter; 020import com.mybatisflex.core.util.LambdaUtil; 021 022import java.io.Serializable; 023 024/** 025 * 属性查询构建者。 026 * 027 * @param <T> 属性所在实体类类型 028 * @author 开源海哥 029 * @author 王帅 030 */ 031public class FieldQueryBuilder<T> implements Serializable { 032 033 private FieldQuery.Builder<?> builder; 034 035 /** 036 * 为指定属性创建查询。 037 * 038 * @param fn Lambda 引用 039 * @return {@link FieldQuery.Builder} 构建者 040 */ 041 public FieldQuery.Builder<T> field(LambdaGetter<T> fn) { 042 return createBuilder(fn); 043 } 044 045 /** 046 * 为指定嵌套属性创建查询。 047 * 048 * @param <N> 嵌套属性类型 049 * @param fn Lambda 引用 050 * @return {@link FieldQuery.Builder} 构建者 051 */ 052 public <N> FieldQuery.Builder<N> nestedField(LambdaGetter<N> fn) { 053 return createBuilder(fn); 054 } 055 056 /** 057 * <p>创建 {@link FieldQuery.Builder} 用于构建属性类型与 {@code QueryWrapper} 用于查询。 058 * 059 * <p>该方法主要用作于创建构建者对象,以及泛型的转换。 060 */ 061 @SuppressWarnings({"rawtypes", "unchecked"}) 062 private <R> FieldQuery.Builder<R> createBuilder(LambdaGetter fn) { 063 FlexAssert.notNull(fn, "Field can not be null."); 064 Class<?> entityClass = LambdaUtil.getImplClass(fn); 065 String fieldName = LambdaUtil.getFieldName(fn); 066 builder = new FieldQuery.Builder<>(entityClass, fieldName); 067 return (FieldQuery.Builder<R>) builder; 068 } 069 070 public FieldQuery build() { 071 return builder.build(); 072 } 073 074}