chemitry 2017-11-24 17:38 采纳率: 50%
浏览 1277
已采纳

Spring集成mybaties,用配置文件将映射器接口注入业务层,无法代理生成实现类

Spring集成mybaties,用配置文件将映射器接口注入业务层,做单元测试时,控制层可以正常返回数据,但是部署到tomcat上,启动就报错,信息如下。
1. 单元测试时正常返回数据
图片说明
2.部署到tomcat 启动报错
严重: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping': Invocation of init method failed; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.alibaba.druid.pool.DruidDataSource] for bean with name 'dataSource' defined in file [G:\IdeaProjects\firstSpring\target\firstSpring\WEB-INF\classes\spring-mybatis.xml]; nested exception is java.lang.ClassNotFoundException: com.alibaba.druid.pool.DruidDataSource
Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.alibaba.druid.pool.DruidDataSource] for bean with name 'dataSource' defined in file [G:\IdeaProjects\firstSpring\target\firstSpring\WEB-INF\classes\spring-mybatis.xml]; nested exception is java.lang.ClassNotFoundException: com.alibaba.druid.pool.DruidDataSource
Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.mybatis.spring.SqlSessionFactoryBean] for bean with name 'sqlSessionFactory' defined in file [G:\IdeaProjects\firstSpring\target\firstSpring\WEB-INF\classes\spring-mybatis.xml]; nested exception is java.lang.ClassNotFoundException: org.mybatis.spring.SqlSessionFactoryBean
Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.mybatis.spring.mapper.MapperScannerConfigurer] for bean with name 'org.mybatis.spring.mapper.MapperScannerConfigurer#0' defined in file [G:\IdeaProjects\firstSpring\target\firstSpring\WEB-INF\classes\spring-mybatis.xml]; nested exception is java.lang.ClassNotFoundException: org.mybatis.spring.mapper.MapperScannerConfigurer

Caused by: java.lang.ClassNotFoundException: org.mybatis.spring.mapper.MapperScannerConfigurer
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1892)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1735)
at org.springframework.util.ClassUtils.forName(ClassUtils.java:250)
at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:401)
at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1432)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1377)
... 81 more
3. 配置文件
spring-mvc
图片说明
Spring-mybatis

 <util:properties id="cfg"    location="classpath:config.properties"/>  
   <!--配置DruidDataSource连接池 -->
   <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
        destroy-method="close" init-method="init" lazy-init="true">
        <property name="driverClassName" value="#{cfg.driver}" />
        <property name="url" value="#{cfg.url}" />
        <property name="username" value="#{cfg.username}" />
        <property name="password" value="#{cfg.password}" />
        <!-- 初始化连接大小 -->
        <property name="initialSize" value="#{cfg.initialSize}" />
        <!-- 连接池最大数量 -->
        <property name="maxActive" value="#{cfg.maxActive}" />
        <!-- 连接池最小空闲 -->
        <property name="minIdle" value="#{cfg.minIdle}" />
        <!-- 获取连接最大等待时间 -->
        <property name="maxWait" value="#{cfg.maxWait}" />
     </bean>
     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
       <property name="dataSource" ref="dataSource"></property>
       <property name="configLocation" value="classpath:mybatis-config.xml"></property>
       <!-- 自动扫描mapping.xml文件 -->
       <property name="mapperLocations" >
            <list>
             <value>classpath:mapper/*.xml</value>
            </list>
        </property>
     </bean>
    <!-- Mapper接口所在包,Spring会自动查找其下的Mapper -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
        <property name="basePackage" value="rush.dao"/>
    </bean>
  1. 另外,在将映射器注入到业务层时,提示: 图片说明 困惑,求大神帮忙解答,谢谢!
  • 写回答

2条回答 默认 最新

  • COCO_AS 2017-11-24 23:01
    关注

    是你的com.alibaba.druid.pool.DruidDataSource这个类找不到啊, 检查一下jar包在不, 如果是用maven构建, 需要有下面的依赖

    <dependency>
         <groupId>com.alibaba</groupId>
         <artifactId>druid</artifactId>
         <version>1.1.5</version>
    </dependency>
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥500 高有偿提问!求优化设计微信小程序
  • ¥15 matlab在安装时报错 无法找到入口 无法定位程序输入点
  • ¥15 收益高的广告联盟有哪些
  • ¥15 Android Studio webview 的使用问题, 播放器横屏全屏
  • ¥15 删掉jdk后重新下载,Java web所需要的eclipse无法使用
  • ¥15 uniapp正式环境中通过webapi将本地数据推送到设备出现的跨域问题
  • ¥15 xui建立节点,显示错误
  • ¥15 关于#单片机#的问题:开始、复位、十进制的功能可以实现,但是切换八进制的功能无法实现(按下按键也没有效果),把初始状态调成八进制,也是八进制可以实现但是切换到十进制不行(相关搜索:汇编语言|计数器)
  • ¥15 VINS-Mono或Fusion中feature_manager中estimated_depth是特征的深度还是逆深度?
  • ¥15 谷歌浏览器如何备份抖音网页数据