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

springMVC mybatis dao接口(mapper接口)注入失败

2017-12-20 17:43 2621 查看
报的错误是:

No qualifying bean of
type [com.*.*.*.*.*.dao.UserDao] found
for


dependency: expected at least
1 bean which qualifies as autowire


candidate
for
this dependency. Dependency annotations:


{@javax.annotation.Resource(shareable=true,
mappedName=, description=, name=, type=class
java.lang.Object,
authenticationType=CONTAINER)}


错误描述:dao层的UserDao接口的对象不能注入

我的service是这样的

@Service("xxxService")

public class Hl7sysServiceImlp implements Hl7sysService {

 @Resource

 private Hl7SystemDao hl7sysdao;
@Override
public List<Hl7sys> searchHl7sys() throws Exception{
return hl7sysdao.selectAllSys();

}

}

可能的原因:

1、没有在上一层调用中加注解,如@Resource,可是我加了

2、配置文件不正确(路径要写对),但是我的是正确的

<!-- myBatis文件 -->  

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  

        <property name="dataSource" ref="dataSource" />         

         <property name="configLocation" value="classpath:spring/mybatis-config.xml"/>

         <!-- 因为映射接口文件不放在同一个包下,所以需要配置xml的位置 --> 

        <property name="mapperLocations" value="classpath:mapping/*.xml" />  

    </bean>   

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  

        <!-- 配置接口包的位置 -->

        <property name="basePackage"  value="com.spring.dao"/>  

        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />         

    </bean>  

3、没有写实现接口的xml文件,我写了

4、根本就木有加载spring-mybatis.xml文件!这个原因比较隐蔽!需要

在web.xml中添加上下文监听器

 <context-param>  

        <param-name>contextConfigLocation</param-name>  

        <param-value>classpath:spring/spring-mybatis.xml</param-value>  

 </context-param>


<!-- 上下文监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

至此,问题解决!!感谢这篇博客

http://blog.csdn.net/u012385190/article/details/53186552
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: