Spring配置数据源
DBCP连接池
DBCP配置依赖与2个jar包:commons-dbcp.jar和commons-pool.jar。
笔者亲测结果报错(类org.apache.commons.dbcp.BasicDataSource找不到)
网上很多文章说还需要另一个包commons-dbutils.jar
添加这个包之后完美解决,这里推荐三个包都添加
commons-dbcp-1.4.jar,commons-dbutils-1.6.jar,commons-pool-1.5.6.jar下载链接
application.xml配置:
<!--application.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"
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/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 添加扫描 -->
<context:component-scan base-package="com.sxt.zxdz"></context:component-scan>
<!-- DBCP连接池配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
<property name="username" value="spring"></property>
<property name="password" value="spring"></property>
<property name="initialSize" value="10"></property>
</bean>
</beans>
test测试类:
//test.java
package com.sxt.zxdz.test;
import java.sql.SQLException;
import javax.sql.DataSource