SpringMVC中DAO层bean无法创建的问题

来源:互联网 发布:阿里妈妈淘宝客怎么做 编辑:程序博客网 时间:2024/06/09 23:16

初次使用SpringMVC+Mybatis。各层之间以接口和接口实现类的方式相连,在普通配置的配置完成后,启动项目一直报错

No qualifying bean of type 'com.softwise1.dao.UserDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

因为使用的Spring的自动注入

@Servicepublic class UserServiceImp extends BaseServiceImp implements UserService {    @Autowired    private UserDao userDao;        private RoleService roleService;    *    *    *}

在这里@Autowired时出现错误,其原因是在xml文件中,整合Spring+Mybatis的配置

<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <property name="dataSource" ref="dataSource" />        <!-- <property name="configLocation"  value="classpath:mybatis-config.xml"/> -->        <!-- 自动扫描mapping.xml文件,**表示迭代查找,也可在sqlMapConfig.xml中单独指定xml文件-->        <property name="mapperLocations" value="classpath:com/softwise1/mappers/*.xml" />    </bean>

缺少了dao层和mapper之间的连接。

解决方法:

<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <property name="dataSource" ref="dataSource" />        <!-- <property name="configLocation"  value="classpath:mybatis-config.xml"/> -->        <!-- 自动扫描mapping.xml文件,**表示迭代查找,也可在sqlMapConfig.xml中单独指定xml文件-->        <property name="mapperLocations" value="classpath:com/softwise1/mappers/*.xml" />    </bean>    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <!-- 注入sqlSessionFactory -->        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />        <!-- 给出需要扫描Dao接口包 -->        <property name="basePackage" value="com.softwise1.dao" />    </bean>

指定需要扫面的Dao层接口包的位置。
启动,解决问题。

0 0
原创粉丝点击