mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-11-10 02:57:14 +08:00
perf(extension/crud): 优化 BaseServiceImpl 中获取各泛型参数类型的方式
This commit is contained in:
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -33,6 +33,7 @@ import jakarta.servlet.http.HttpServletResponse;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.data.domain.Sort;
|
import org.springframework.data.domain.Sort;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
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.ExceptionUtils;
|
||||||
import top.charles7c.continew.starter.core.util.ReflectUtils;
|
import top.charles7c.continew.starter.core.util.ReflectUtils;
|
||||||
import top.charles7c.continew.starter.core.util.validate.CheckUtils;
|
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
|
@Autowired
|
||||||
protected M baseMapper;
|
protected M baseMapper;
|
||||||
|
|
||||||
private final Class<T> entityClass;
|
private final Class<?>[] typeArguments = ClassUtils.getTypeArguments(this.getClass());
|
||||||
private final Class<L> listClass;
|
protected final Class<T> entityClass = this.currentEntityClass();
|
||||||
private final Class<D> detailClass;
|
protected final Class<L> listClass = this.currentListClass();
|
||||||
|
protected final Class<D> detailClass = this.currentDetailClass();
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PageResp<L> page(Q query, PageQuery pageQuery) {
|
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 待填充详情信息
|
* @param detailObj 待填充详情信息
|
||||||
*/
|
*/
|
||||||
public void fillDetail(Object detailObj) {
|
public void fillDetail(Object detailObj) {
|
||||||
if (detailObj instanceof BaseDetailResp detail) {
|
if (detailObj instanceof BaseDetailResp detailResp) {
|
||||||
this.fill(detail);
|
this.fill(detailResp);
|
||||||
Long updateUser = detail.getUpdateUser();
|
Long updateUser = detailResp.getUpdateUser();
|
||||||
if (null == updateUser) {
|
if (null == updateUser) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
CommonUserService userService = SpringUtil.getBean(CommonUserService.class);
|
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];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user