您的位置:首页 > 移动开发

spring mybatis 整合后mapper接口注入失败问题

2016-04-26 18:10 1091 查看
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.fkhd.whiteshirt.daos.UserMapper] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.
Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

spring-mybatis:

<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="localdataSource" />
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>

<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.fkhd.whiteshirt.daos" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>


serviceImpl:

@Autowired
private UserMapper userMapper;


修改serviceImpl为:

@Autowired(required=false)
private UserMapper userMapper;

不报错了,但userMapper为null,也不注入了,这样解决虽然不报错了,但项目无法继续进行,所以另寻方法。

各种解决办法都试了后,没解决,所以就自己在重新看看自己的项目。

我的错误原因在:web.xml启动是没加载soring-mybatis.xml,加上以后问题解决。

我的这个原因在于web.xml没有加载,仅是问题一种。可以看看你的是否加载,如果加载了就去看看其他原因吧!

可以运行的配置:

spring-mybatis(未修改):

<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="localdataSource" />
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>

<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.fkhd.whiteshirt.daos" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>


serviceImpl(未修改):

@Autowired
private UserMapper userMapper;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: