利用Spring配置文件实现jdbc操作数据库(C3P0)

<?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="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--注入四个数据库原属性-->
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/springdemo"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>

<!--通过queryRunner注入datasource对象-->
<bean id="QueryRunner" class="org.apache.commons.dbutils.QueryRunner">
<constructor-arg name="ds" ref="datasource"></constructor-arg>
</bean>

<!--注入queryRunner-->
<bean id="AccountDao" class="com.itheima.dao.impl.AccountDaoImpl">
<property name="queryRunner" ref="QueryRunner"></property>
</bean>
<!--注入accountDao-->
<bean id="AccountService" class="com.itheima.service.impl.AccountServiceImpl">
<property name="accountDao" ref="AccountDao"></property>
</bean>

</beans>
posted @ 2019-10-17 11:40  KongChan  阅读(246)  评论(0)    收藏  举报