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

Stella981
• 阅读 491

Spring整合MyBatis!

main方法测试,出现异常:

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

一堆错,很懵逼!

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

先分析一下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中的

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

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

运行:

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

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

点赞
收藏
评论区
推荐文章
Easter79 Easter79
2年前
spring注解
随着越来越多地使用Springboot敏捷开发,更多地使用注解配置Spring,而不是Spring的applicationContext.xml文件。Configuration注解:Spring解析为配置类,相当于spring配置文件Bean注解:容器注册Bean组件,默认id为方法名@Configurat
Stella981 Stella981
2年前
MyBatis接口(Bean)与配置信息(Mapper)绑定
目的MyBatis的XML配置文件解析成JAVA类并在内存中存储,但是在程序运行时需要对应的类去调用,而相应的调用类还没有实例化,现在流行的都是使用Spring去管理需要的对象,Spring提供2种方式,分别为XML与注解。下面来分析调用类的实例化及与配置绑定。1XML方式<bean id"menuMapper" cl
Easter79 Easter79
2年前
SpringCloud 启动时报No active profile set, falling back to default profiles default
这在Spring程序启动时没有找到默认的配置文件所引发的错误,默认文件application.yml如下图: !这里写图片描述(https://oscimg.oschina.net/oscnet/4a822ce35ff8ed227a2c57f496787d95e5a.png)一般在项目中都会有多个,如有正式环境、测试环境等。如下
Stella981 Stella981
2年前
SpringBoot整合Redis乱码原因及解决方案
问题描述:springboot使用springdataredis存储数据时乱码rediskey/value出现\\xAC\\xED\\x00\\x05t\\x00\\x05问题分析:查看RedisTemplate类!(https://oscimg.oschina.net/oscnet/0a85565fa
Stella981 Stella981
2年前
MyBatis整合Spring
目的MyBatis在执行SQL语句时,都需要创建一个SqlSession,但是这里还需要与Spring的事务进行整合,那么SqlSession是怎么创建的呢?下面就来分析一下。上一章节已经分析MapperProxy代理类中,具体执行代码,实际MyBatis调用就是使用的SqlSession入口,就来看看SqlSession具体是哪个类。!
Stella981 Stella981
2年前
Android So动态加载 优雅实现与原理分析
背景:漫品Android客户端集成适配转换功能(基于目标识别(So库35M)和人脸识别库(5M)),导致apk体积50M左右,为优化客户端体验,决定实现So文件动态加载.!(https://oscimg.oschina.net/oscnet/00d1ff90e4b34869664fef59e3ec3fdd20b.png)点击上方“蓝字”关注我
Wesley13 Wesley13
2年前
APPLICATIONCONTEXT.XML与SPRING
之前使用的ssm框架时,配置的是springmybatis.xml这个文件,今天在网上搜ssm框架整合时,发现了applicationContext.xml这个文件(对于Spring和mybatis两个框架的整合),然后又查了下springmybatis.xml的配置,发现applicationContext.xml与springmybatis.x
Easter79 Easter79
2年前
SpringBoot整合Redis乱码原因及解决方案
问题描述:springboot使用springdataredis存储数据时乱码rediskey/value出现\\xAC\\xED\\x00\\x05t\\x00\\x05问题分析:查看RedisTemplate类!(https://oscimg.oschina.net/oscnet/0a85565fa
Stella981 Stella981
2年前
CAS 实现站内单点登录及实现第三方 OAuth、OpenId 登录(四)
一、OAuth配置1.配置OAuth提供商<bean id"weibo" class"com.buession.oauth.provider.impl.WeiboProvider"    <property name"key" value"the_key_for_
Wesley13 Wesley13
2年前
MYSQL查询用户下多个角色信息
<resultMapid"baseBeanUser"type"com...vo.system.TUserVoOut"<idcolumn"id"property"id"/<resultcolumn"name"property"name"/