spring 和 mybatis整合后启动遇到的一个问题

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0': Invocation of init method failed; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.apache.commons.dbcp.BasicDataSource] for bean with name 'dataSource' defined in file [D:\apache-tomcat-8.0.18\wtpwebapps\Pipeline\WEB-INF\classes\config\spring-mybatis.xml]; nested exception is java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource

10个回答

jar少了好像是的吧,一般出现ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource大多是jar包问题,仔细检查一下吧

这个类的包是导入的,
Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0': Invocation of init method failed;
应该是这一句的问题

它说在spring-mybatis.xml里找不到定义为dataSource的bean类:org.apache.commons.dbcp.BasicDatasource.
你看看是不是这个类路径写错了。

jar包是导入了的,也能通过ctrl+鼠标左键找到

看看你的 映射文件中的路径写的对不对

”<?xml version="1.0" encoding="UTF-8"?>
xmlns:context="http://www.springframework.org/schema/context"
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">

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:config/mybatis.properties"/>
</bean>
   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${jdbc.mysql.driver}"/>
    <property name="url" value="${jdbc.mysql.url}"/>
    <property name="username" value="${jdbc.mysql.username}"/>
    <property name="password" value="${jdbc.mysql.password}"/>
    <property name="initialSize" value="${jdbc.initialSize}"/>
    <property name="minIdle" value="${jdbc.minIdle}"/>
    <property name="maxIdle" value="${jdbc.maxIdle}"/>
    <property name="maxActive" value="${jdbc.maxActive}"/>
    <property name="maxWait" value="${jdbc.maxWait}"/>
    <property name="defaultAutoCommit" value="${jdbc.defaultAutoCommit}"/>
    <property name="removeAbandoned" value="${jdbc.removeAbandoned}"/>
    <property name="removeAbandonedTimeout" value="${jdbc.removeAbandonedTimeout}"/>
    <property name="testWhileIdle" value="${jdbc.testWhileIdle}"/>
    <property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}"/>
    <property name="numTestsPerEvictionRun" value="${jdbc.numTestsPerEvictionRun}"/>
    <property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="mapperLocations" value="classpath:com/pipeline/dao/mappers/*Mapper.xml"></property>
</bean>

<!-- mybatis.spring自动映射 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.pipeline.dao"/>
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>

<!-- 自动扫描,多个包以 逗号分隔 -->
<context:component-scan base-package="com.pipeline.dao"/>
<aop:aspectj-autoproxy/>

sqlSessionFactory中bean没有引入mybastic的配置

需添加<!-- 指定mybastic配置文件 -->

我这边是(ssm整合型框架项目),我贴下我这边配置Spring.xml

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.3.xsd 
        http://www.springframework.org/schema/tx   
        http://www.springframework.org/schema/tx/spring-tx-4.3.xsd   
         http://www.springframework.org/schema/aop    
         http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
    <!-- 加载db.proerties -->
    <context:property-placeholder location="classpath:config/db.properties" />
    <!-- 采用DBCP连接池 -->
    <bean id="dbcpDataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="maxActive" value="30"></property>
        <property name="maxIdle" value="5"></property>
    </bean>
    <!-- 事务管理器,对mybatis操作事务控制,Spring 采用jdbc的事务控制器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dbcpDataSource"></property>
    </bean>
   <!-- 配置 sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 指定要用的连接池 -->
        <property name="dataSource" ref="dbcpDataSource"></property>
        <!-- 指定mybastic配置文件 -->
        <property name="configLocation" value="classpath:config/mybastic_config.xml"></property>
        <!-- 指定配置的mapper -->
       <property name="mapperLocations"
            value="classpath*:com/szmj/myself/dao/mapper/*.xml"></property>  
    </bean>
    <!-- 配置Mapper扫描器 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
         <!-- 扫描的路径:若是扫描多个包,需要用半角逗号隔开 -->
        <property name="basePackage" value="com.szmj.myself.dao.mapper" />
        <!-- 这里能使用ref="sqlSessionFactory",会导致sqlSessionFactory来不及配置 -->
         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> 
    </bean>

     <bean  id="operation" class="org.mybatis.spring.mapper.MapperFactoryBean" >
        <property name="mapperInterface" value="com.szmj.myself.dao.inter.PersonOperation"></property>
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
    </bean>




</beans> 

springmvc你的是哪个版本的?高版本的注解功能不是这个类,是这个org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter

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