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>
- 另外,在将映射器注入到业务层时,提示: 困惑,求大神帮忙解答,谢谢!