perf(extension/crud): 优化 BaseServiceImpl 中获取各泛型参数类型的方式

This commit is contained in:
2024-01-03 21:56:45 +08:00
parent 81ed292840
commit 6fc0b51a57
2 changed files with 85 additions and 13 deletions

View File

@@ -0,0 +1,49 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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 top.charles7c.continew.starter.core.util;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.TypeUtil;
import java.lang.reflect.Type;
/**
* 类工具类
*
* @author Charles7c
* @since 1.1.1
*/
public class ClassUtils {
/**
* 获得给定类的所有泛型参数
*
* @param clazz 被检查的类,必须是已经确定泛型类型的类
* @return {@link Class}[]
*/
public static Class<?>[] getTypeArguments(Class<?> clazz) {
final Type[] typeArguments = TypeUtil.getTypeArguments(clazz);
if (ArrayUtil.isEmpty(typeArguments)) {
return new Class[0];
}
final Class<?>[] classes = new Class<?>[typeArguments.length];
for (int i = 0; i < typeArguments.length; i++) {
classes[i] = TypeUtil.getClass(typeArguments[i]);
}
return classes;
}
}

View File

@@ -33,6 +33,7 @@ import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.transaction.annotation.Transactional;
import top.charles7c.continew.starter.core.util.ClassUtils;
import top.charles7c.continew.starter.core.util.ExceptionUtils;
import top.charles7c.continew.starter.core.util.ReflectUtils;
import top.charles7c.continew.starter.core.util.validate.CheckUtils;
@@ -67,15 +68,10 @@ public abstract class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseDO,
@Autowired
protected M baseMapper;
private final Class<T> entityClass;
private final Class<L> listClass;
private final Class<D> detailClass;
protected BaseServiceImpl() {
this.entityClass = (Class<T>) ClassUtil.getTypeArgument(this.getClass(), 1);
this.listClass = (Class<L>) ClassUtil.getTypeArgument(this.getClass(), 2);
this.detailClass = (Class<D>) ClassUtil.getTypeArgument(this.getClass(), 3);
}
private final Class<?>[] typeArguments = ClassUtils.getTypeArguments(this.getClass());
protected final Class<T> entityClass = this.currentEntityClass();
protected final Class<L> listClass = this.currentListClass();
protected final Class<D> detailClass = this.currentDetailClass();
@Override
public PageResp<L> page(Q query, PageQuery pageQuery) {
@@ -226,14 +222,41 @@ public abstract class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseDO,
* @param detailObj 待填充详情信息
*/
public void fillDetail(Object detailObj) {
if (detailObj instanceof BaseDetailResp detail) {
this.fill(detail);
Long updateUser = detail.getUpdateUser();
if (detailObj instanceof BaseDetailResp detailResp) {
this.fill(detailResp);
Long updateUser = detailResp.getUpdateUser();
if (null == updateUser) {
return;
}
CommonUserService userService = SpringUtil.getBean(CommonUserService.class);
detail.setUpdateUserString(ExceptionUtils.exToNull(() -> userService.getNicknameById(updateUser)));
detailResp.setUpdateUserString(ExceptionUtils.exToNull(() -> userService.getNicknameById(updateUser)));
}
}
/**
* 获取当前实体类型
*
* @return 当前实体类型
*/
protected Class<T> currentEntityClass() {
return (Class<T>) this.typeArguments[1];
}
/**
* 获取当前列表信息类型
*
* @return 当前列表信息类型
*/
protected Class<L> currentListClass() {
return (Class<L>) this.typeArguments[2];
}
/**
* 获取当前详情信息类型
*
* @return 当前详情信息类型
*/
protected Class<D> currentDetailClass() {
return (Class<D>) this.typeArguments[3];
}
}