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.activerecord.query; 018 019import com.mybatisflex.core.activerecord.Model; 020import com.mybatisflex.core.query.RelationsBuilder; 021import com.mybatisflex.core.util.LambdaGetter; 022 023import java.io.Serializable; 024 025/** 026 * 使用 {@code Relations Query} 的方式进行关联查询。 027 * 028 * @author 王帅 029 * @since 2023-07-30 030 */ 031public class RelationsQuery<T extends Model<T>> extends RelationsBuilder<T> { 032 033 public RelationsQuery(Model<T> model) { 034 super(model); 035 } 036 037 @Override 038 public RelationsQuery<T> ignoreRelations(String... fields) { 039 super.ignoreRelations(fields); 040 return this; 041 } 042 043 @Override 044 public <L> RelationsQuery<T> ignoreRelations(LambdaGetter<L>... fields) { 045 super.ignoreRelations(fields); 046 return this; 047 } 048 049 @Override 050 public RelationsQuery<T> maxDepth(int maxDepth) { 051 super.maxDepth(maxDepth); 052 return this; 053 } 054 055 @Override 056 public RelationsQuery<T> extraConditionParam(String key, Object value) { 057 super.extraConditionParam(key, value); 058 return this; 059 } 060 061 protected Object pkValue() { 062 // 懒加载,实际用到的时候才会生成 主键值 063 return ((Model<T>) delegate).pkValue(); 064 } 065 066 /** 067 * 根据主键查询一条数据。 068 * 069 * @return 一条数据 070 */ 071 public T oneById() { 072 return baseMapper().selectOneWithRelationsById((Serializable) pkValue()); 073 } 074 075 /** 076 * 根据主键查询一条数据,返回的数据为 asType 类型。 077 * 078 * @param asType 接收数据类型 079 * @param <R> 接收数据类型 080 * @return 一条数据 081 */ 082 public <R> R oneByIdAs(Class<R> asType) { 083 return baseMapper().selectOneWithRelationsByIdAs((Serializable) pkValue(), asType); 084 } 085 086}