MyBatis Plus:No qualifying bean of type 'com.baomidou.mybatisplus.mapper.BaseMapper<?>' available: expected single matching bean but found 4

别说谁变了你拦得住时间么 提交于 2019-12-09 10:20:02

场景:

应用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> {
}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!