mybatis expected at least 1 bean which qualifies as autowire candidate for this dependency

你说的曾经没有我的故事 提交于 2020-03-05 00:35:28

错误原因:没有引入相应mapper接口,导致spring没有找到依赖

解决方法一:使用注解的方法:

首先在spring配置文件中添加

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.forum" />
        <property name="annotationClass" value="org.springframework.stereotype.Repository" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    </bean>

然后在mapper接口中添加注解

@Transactional
@Repository
public interface RoleMapper

解决方法二:使用xml配置定义mapper的bean

<!— mapper bean -->  
<bean id="roleMapper" class="org.mybatis.spring.MapperFactoryBean">  
    <property name="mapperInterface" value="com.forum.dao.RoleMapper" />  
    <property name="sqlSessionFactory" ref="sqlSessionFactory" />  
</bean>

不过第二种方法有点繁琐,假如有多个mapper接口的时候,可能要配置多个bean,那就太多了,能把文件撑爆。


 

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