优化 BaseController

v1.4.1
Parker 4 years ago
parent 6485197c1c
commit 2230591f9e

@ -20,7 +20,6 @@ import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval; import cn.hutool.core.date.TimeInterval;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.TypeUtil;
import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.util.CollectionUtils; import com.alibaba.excel.util.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
@ -59,7 +58,6 @@ import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@ -82,12 +80,9 @@ public abstract class BaseRestController <T extends BaseEntity, E extends ApiWra
/** Entity Clazz 类 */ /** Entity Clazz 类 */
protected Class<T> entityClazz; protected Class<T> entityClazz;
/** Entity 泛型游标 */
private static final int ENTITY_INDEX = 0;
/** Model Clazz 类 */ /** Model Clazz 类 */
protected Class<E> modelClazz; protected Class<E> modelClazz;
/** Model 泛型游标 */
private static final int MODEL_INDEX = 1;
/** 配置类 */ /** 配置类 */
@Autowired @Autowired
@ -366,8 +361,8 @@ public abstract class BaseRestController <T extends BaseEntity, E extends ApiWra
@PostConstruct @PostConstruct
public void init(){ public void init(){
try { try {
this.modelClazz = this.getModelClass(); this.modelClazz = IService.getModelClazz();
this.entityClazz = this.getEntityClass(); this.entityClazz = IService.getEntityClazz();
Class<?> serviceClazz = IService.getServiceClazz(); Class<?> serviceClazz = IService.getServiceClazz();
// 判断是否开启热点数据 // 判断是否开启热点数据
if(serviceClazz != null){ if(serviceClazz != null){
@ -397,31 +392,4 @@ public abstract class BaseRestController <T extends BaseEntity, E extends ApiWra
return null; return null;
} }
/**
* Clazz
* @return Class<E>
*/
private Class<E> getModelClass(){
Class<E> tClass = null;
Type typeArgument = TypeUtil.getTypeArgument(getClass().getGenericSuperclass(), MODEL_INDEX);
if(typeArgument != null){
tClass = (Class<E>) typeArgument;
}
return tClass;
}
/**
* Clazz
* @return Class<T>
*/
private Class<T> getEntityClass(){
Class<T> tClass = null;
Type typeArgument = TypeUtil.getTypeArgument(getClass().getGenericSuperclass(), ENTITY_INDEX);
if(typeArgument != null){
tClass = (Class<T>) typeArgument;
}
return tClass;
}
} }

@ -17,6 +17,7 @@ package org.opsli.core.base.service.impl;
import cn.hutool.core.convert.Convert; import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.TypeUtil; import cn.hutool.core.util.TypeUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.github.pagehelper.PageInfo; import com.github.pagehelper.PageInfo;
@ -38,6 +39,7 @@ import javax.annotation.PostConstruct;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* @BelongsProject: opsli-boot * @BelongsProject: opsli-boot
@ -62,15 +64,12 @@ import java.util.List;
public abstract class CrudServiceImpl<M extends BaseMapper<T>, T extends BaseEntity, E extends ApiWrapper> public abstract class CrudServiceImpl<M extends BaseMapper<T>, T extends BaseEntity, E extends ApiWrapper>
extends BaseService<M, T> implements CrudServiceInterface<T,E> { extends BaseService<M, T> implements CrudServiceInterface<T,E> {
/** JSON tmp */
private static final String JSON_TMP = "{\"id\":\"1\"}";
/** Entity Clazz 类 */ /** Entity Clazz 类 */
protected Class<T> entityClazz; protected Class<T> entityClazz;
/** Entity 泛型游标 */
private static final int ENTITY_INDEX = 1;
/** Model Clazz 类 */ /** Model Clazz 类 */
protected Class<E> modelClazz; protected Class<E> modelClazz;
/** Model 泛型游标 */
private static final int MODEL_INDEX = 2;
@Override @Override
public E get(String id) { public E get(String id) {
@ -304,36 +303,75 @@ public abstract class CrudServiceImpl<M extends BaseMapper<T>, T extends BaseEnt
@PostConstruct @PostConstruct
public void init(){ public void init(){
try { try {
this.modelClazz = this.getModelClazz(); this.modelClazz = this.getInnerModelClazz();
this.entityClazz = this.getEntityClazz(); this.entityClazz = this.getInnerEntityClazz();
}catch (Exception e){ }catch (Exception e){
log.error(e.getMessage(),e); log.error(e.getMessage(),e);
} }
} }
@Override
public Class<T> getEntityClazz() {
return entityClazz;
}
@Override
public Class<E> getModelClazz() {
return modelClazz;
}
/** /**
* Clazz * Model Clazz
* @return * @return Class<E>
*/ */
private Class<E> getModelClazz(){ public Class<E> getInnerModelClazz(){
String typeName = "E";
Class<E> tClass = null; Class<E> tClass = null;
Type typeArgument = TypeUtil.getTypeArgument(getClass().getGenericSuperclass(), MODEL_INDEX); Map<Type, Type> typeMap = TypeUtil.getTypeMap(this.getClass());
if(typeArgument != null){ for (Map.Entry<Type, Type> typeTypeEntry : typeMap.entrySet()) {
tClass = (Class<E>) typeArgument; Type entryKey = typeTypeEntry.getKey();
Type entryValue = typeTypeEntry.getValue();
// 如果等于当前类型名称则进入
if(entryKey != null && entryValue != null){
if(StringUtils.equals(typeName, entryKey.getTypeName())){
// 小技巧 json 转换 obj 然后 转换成 type对象
Object convert = Convert.convert(entryValue, JSONObject.parse(JSON_TMP));
if(convert instanceof ApiWrapper) {
tClass = (Class<E>) entryValue;
break;
}
}
}
} }
return tClass; return tClass;
} }
/** /**
* Clazz * Entity Clazz
* @return * @return Class<T>
*/ */
private Class<T> getEntityClazz(){ private Class<T> getInnerEntityClazz(){
String typeName = "T";
Class<T> tClass = null; Class<T> tClass = null;
Type typeArgument = TypeUtil.getTypeArgument(getClass().getGenericSuperclass(), ENTITY_INDEX); Map<Type, Type> typeMap = TypeUtil.getTypeMap(this.getClass());
if(typeArgument != null){ for (Map.Entry<Type, Type> typeTypeEntry : typeMap.entrySet()) {
tClass = (Class<T>) typeArgument; Type entryKey = typeTypeEntry.getKey();
Type entryValue = typeTypeEntry.getValue();
// 如果等于当前类型名称则进入
if(entryKey != null && entryValue != null){
if(StringUtils.equals(typeName, entryKey.getTypeName())){
// 小技巧 json 转换 obj 然后 转换成 type对象
Object convert = Convert.convert(entryValue, JSONObject.parse(JSON_TMP));
if(convert instanceof ApiWrapper) {
tClass = (Class<T>) entryValue;
break;
}
}
}
} }
return tClass; return tClass;
} }

@ -38,7 +38,7 @@ import java.util.List;
* @param <T> * @param <T>
* @param <E> * @param <E>
*/ */
public interface CrudServiceInterface<T extends BaseEntity,E extends ApiWrapper> extends BaseServiceInterface<T> { public interface CrudServiceInterface<T extends BaseEntity, E extends ApiWrapper> extends BaseServiceInterface<T> {
/** /**
@ -200,6 +200,18 @@ public interface CrudServiceInterface<T extends BaseEntity,E extends ApiWrapper>
Page<T,E> findPageNotCount(Page<T,E> page); Page<T,E> findPageNotCount(Page<T,E> page);
/**
* Model Clazz
* @return Class
*/
Class<E> getModelClazz();
/**
* Entity Clazz
* @return Class
*/
Class<T> getEntityClazz();
} }

Loading…
Cancel
Save