Invalid property 'driver' of bean class [org.apache.commons.dbcp.BasicDataSource]

Spring整合MyBatis!

main方法测试,出现异常:

一堆错,很懵逼!

别慌,慢慢分析,也许错误很简单;

先分析一下applicationContext.xml文件

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

    <bean id="config" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
        <property name="locations">
            <array>
                <value>classpath:db.properties</value>
            </array>
        </property>
    </bean>

     <bean id="studentService" class="com.guor.service.impl.StudentServiceImpl">
        <property name="studentMapper" ref="studentMapper"></property>
    </bean>
    
    <!-- 第一种方式 extends SqlSessionDaoSupport 
    <bean id="studentMapper" class="com.guor.dao.impl.StudentDaoImpl">
         将spring配置的 sqlSessionFactory 交给mapper(dao层)
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> 
    </bean>  -->
    
    
    <!-- 第二种方式  直接使用MapperFactoryBean,缺点:每个mapper都需要配置一次
    <bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="com.guor.mapper.StudentMapper"></property>
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>  
    </bean> -->
    
    <!-- 第三种方式  批量搞定,批量产生mapper对象在IOC中的id值默认就是接口名-->
    <bean id="mappers" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>  
        <!-- 指定批量产生哪个包的mapper对象 -->
        <property name="basePackage" value="com.guor.mapper"></property>  
    </bean>

    <!-- 配置数据库信息(替代mybatis的配置文件confx.ml)   -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driver" value="${driver}"></property>
        <property name="url" value="${url}"></property>
        <property name="username" value="${username}"></property>
        <property name="password" value="${password}"></property>
    </bean> 
    
     <!-- 在springIOC中创建mybatis的核心类SqlSessionFactoryBean  -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="mapperLocations" value="com/guor/mapper/*.xml"></property>
    </bean> 
</beans>

分析思路:

1、全部注掉;

2、放开对外层的那一部分;

dataSource

dataSource是org.apache.commons.dbcp.BasicDataSource中的

不难发现BasicDataSource中的驱动项不是普通的driver,而是driverClassName;

运行:

输出发现是用set方法注入的,并且数据库中数据增加成功,success!

评论

暂无评论