场景:
应用MyBatis Plus 和通用Mapper
继承自ServiceImpl实现对Service里的方法进行包装再处理。
public interface IServiceBase2<T extends AbstractDTO> { }
public class ServiceBaseImpl2<M extends BaseMapper<P>,P extends Model<P>,D extends AbstractDTO> extends ServiceImpl<M,P> implements IServiceBase2<D> { private Class<P> poClazz; private Class<D> dtoClazz; public ServiceBaseImpl2(){ Type superClass = getClass().getGenericSuperclass(); if (superClass instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) superClass; Type[] types = parameterizedType.getActualTypeArguments(); if (types != null && types.length == 3) { if (types[1] instanceof Class) { poClazz = (Class<P>) types[1]; } if (types[2] instanceof Class) { dtoClazz = (Class<D>) types[2]; } } } } @Override public D selectByIdDTO(Serializable var1) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException { P p = super.selectById(var1); String classPath = poClazz.getName() + "POJOMapperImpl";// "com.api.modular.clinicalcase.dto.mapper.ClinicalcasePOJOMapperImpl"; classPath = classPath.replace("model","dto.mapper"); Class mapperClazz = Class.forName(classPath); Object newInstance = mapperClazz.newInstance(); Method[] methods = mapperClazz.getMethods(); Method method = mapperClazz.getMethod("doToDto", poClazz); D result = (D) method.invoke(newInstance, p); return result; } }
错误:
启动项目报错:No qualifying bean of type 'com.baomidou.mybatisplus.mapper.BaseMapper<?>' available: expected single matching bean but found 4
解决:
将ServiceBaseImpl 更改为抽象类
public abstract class ServiceBaseImpl2<M extends BaseMapper<P>,P extends Model<P>,D extends AbstractDTO> extends ServiceImpl<M,P> implements IServiceBase2<D> { }
来源:https://www.cnblogs.com/icebutterfly/p/9851853.html