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!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哪 吒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值