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

本文详细解析了Spring与MyBatis整合过程中遇到的常见问题及解决方案,通过分析applicationContext.xml配置文件,逐步解决异常,实现数据库操作的成功案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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!

查看代码是否有问题<!-- 修改后的Spring配置文件 --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 加载数据库配置文件 --> <context:property-placeholder location="classpath:config/db.properties" /> <!-- 修改后的数据源配置(关键修改点) --> <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"> <!-- 1. 更新驱动类名 --> <property name="driverClassName" value="${jdbc.driver}" /> <!-- 2. 确保URL包含时区参数和时区支持 --> <property name="url" value="${jdbc.url}?serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- 连接池配置建议调整 --> <property name="maxTotal" value="${jdbc.maxTotal}" /> <property name="maxIdle" value="${jdbc.maxIdle}" /> <property name="initialSize" value="${jdbc.initialSize}" /> </bean> <!-- 其他保持不变的配置 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:annotation-driven transaction-manager="txManager" /> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:config/mybatis-config.xml" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.mybatis.mapper" /> <property name="sqlSess
03-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

哪 吒

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

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

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

打赏作者

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

抵扣说明:

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

余额充值