MyBatis Plus:No qualifying bean of type 'com.baomidou.mybatisplus.mapper.BaseMapper<?>' available: expected single matching bean but found 4
Posted on 2018-10-25 18:36 冰碟 閱讀(13882) 評(píng)論(2) 收藏 舉報(bào)場(chǎng)景:
應(yīng)用MyBatis Plus 和通用Mapper
繼承自ServiceImpl實(shí)現(xiàn)對(duì)Service里的方法進(jìn)行包裝再處理。
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; } }
錯(cuò)誤:
啟動(dòng)項(xiàng)目報(bào)錯(cuò):No qualifying bean of type 'com.baomidou.mybatisplus.mapper.BaseMapper<?>' available: expected single matching bean but found 4
解決:
將ServiceBaseImpl 更改為抽象類(lèi)
public abstract class ServiceBaseImpl2<M extends BaseMapper<P>,P extends Model<P>,D extends AbstractDTO> extends ServiceImpl<M,P> implements IServiceBase2<D> { }
浙公網(wǎng)安備 33010602011771號(hào)