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条)

报告相同问题?

悬赏问题

  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色
  • ¥15 求一份华为esight平台V300R009C00SPC200这个型号的api接口文档
  • ¥15 就很莫名其妙,本来正常的Excel,突然变成了这种一格一页
  • ¥15 字符串比较代码的漏洞
  • ¥15 欧拉系统opt目录空间使用100%