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个回答

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

<dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>druid</artifactId>
     <version>1.1.5</version>
</dependency>

chemitry
chemitry 回复COCO_AS: 问题已解决,你说得对,没用maven依赖,没有自动在lib文件夹下生成
2 年多之前 回复
chemitry
chemitry durid不是依赖进来的,手动导入的。
2 年多之前 回复
yujiaao
COCO_AS 你的dependecy scope是不是设成test了啊? tomcat/webapps/yourproject/WEB-INF/lib下有这个druid.jar?
2 年多之前 回复
chemitry
chemitry 测试类能够通过,而且jar包也有
2 年多之前 回复

肯定是缺jar包。jar包发没有发布

chemitry
chemitry 说的对,谢谢,已解决
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问