java - springmvc+mybatis 无法装载dao,这是怎么回事?
黄舟
黄舟 2017-04-18 10:30:49
[Java讨论组]

项目结构:

spring-mybatis的配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        ">
    <!-- 自动扫描 -->
    <context:component-scan base-package="com.ymf.whoisquery.service.Impl" />

    <!-- 引入配置文件 -->
    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:jdbc.properties" />
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
        <!-- 初始化连接大小 -->
        <property name="initialSize" value="${initialSize}"/>
        <!-- 连接池最大数量 -->
        <property name="maxActive" value="${maxActive}"/>
        <!-- 连接池最大空闲 -->
        <property name="maxIdle" value="${maxIdle}"/>
        <!-- 连接池最小空闲 -->
        <property name="minIdle" value="${minIdle}"/>
        <!-- 获取连接最大等待时间 -->
        <property name="maxWait" value="${maxWait}"/>
    </bean>


    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>


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

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

</beans>

我估计是mapper文件没找到的缘故

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

全部回复(8)
阿神

UserDAO 你加上注解了吗? @Repository

巴扎黑
    <!-- 自动扫描 -->
    <context:component-scan base-package="com.ymf.whoisquery.service.Impl" />

这里只能扫到service.Impl包下面的类,就算你dao加注解也没用,Controller也没扫到,只扫了service
建议改成

    <!-- 自动扫描 -->
    <context:component-scan base-package="com.ymf.whoisquery" />

我又看了一下,你说加了注解就能用了
我建议你不改扫描范围,再单独写个Controller用@Controller看看前端能不能访问,正常来说没扫到的注解spring是不会去托管,你这个是因为他托管Service的Bean时发现有注入,就会去拿被注入的Bean,继而尝试创建被注入的Bean,应该和扫描没关系
我比较担心你后续会有别的问题

伊谢尔伦

是不是自动扫描配置错了<context:component-scan base-package="com.ymf.whoisquery.service.Impl" />

高洛峰

你扫描到dao了吗

阿神

应该是没有扫描的dao

迷茫

感觉好像要把自动扫描的最后两级去掉

阿神

实际你这是ide报的 一个警告吧,你运行起来应该没问题

ringa_lee

这是IDEA Spring 插件的问题,该插件对MyBatis的支持还不够完善,你可以安装mybatis 插件来取消这个警告

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号