这里说的开源连接池指的是DBCP和C3P0连接池。
配置起来都很简单,继续在上一篇文章十六、Spring通过IOC管理JDBC模板的基础上操作
整合DBCP连接池
先引入DBCP的2个jar包
com.springsource.org.apache.commons.dbcp-1.2.2.osgi.jar
com.springsource.org.apache.commons.pool-1.5.3.jar
对应的查找路径:
\spring-framework-3.0.2.RELEASE-dependencies\org.apache.commons\com.springsource.org.apache.commons.dbcp\1.2.2.osgi
和
\spring-framework-3.0.2.RELEASE-dependencies\org.apache.commons\com.springsource.org.apache.commons.pool\1.5.3\
然后,只需要修改下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" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 注册Spring管理内置的连接池
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql:///spring_jdbc" />
<property name="username" value="root" />
<property name="password" value="1234" />
</bean>
-->
<!-- 注册dbcp连接池,只需替换class的值即可 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql:///spring_jdbc" />
<property name="username" value="root" />
<property name="password" value="1234" />
</bean>
<!-- 注册Spring管理模板类 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!-- 注入属性值,引用类型属性用ref -->
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
测试
package blog.csdn.net.mchenys.test;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo1 {
// 通过java注解注入模板
@Resource(name = "jdbcTemplate")
private JdbcTemplate JdbcTemplate;
@Test
public void test1() {
JdbcTemplate.update("update t_account set money=? where name=?", 99, "胜哥");
}
@Test
public void test2() {
JdbcTemplate.update("update t_account set name=? where money=?", "dbcp连接池", 99);
}
}
运行test2方法,查看数据库,如果显示如下,说明dbcp连接池配置成功了
整合C3P0连接池
先引入C3P0的jar包
com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar
具体路径在
\spring-framework-3.0.2.RELEASE-dependencies\com.mchange.c3p0\com.springsource.com.mchange.v2.c3p0\0.9.1.2\
关于spring-framework-3.0.2.RELEASE-dependencies.zip
的包在哪里下载,第一篇文章有介绍过跳转第一篇文章
然后,还是只需要修改下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" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 注册Spring管理内置的连接池
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql:///spring_jdbc" />
<property name="username" value="root" />
<property name="password" value="1234" />
</bean>
-->
<!-- 注册dbcp连接池,只需替换class的值即可
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql:///spring_jdbc" />
<property name="username" value="root" />
<property name="password" value="1234" />
</bean>
-->
<!-- 注册c3p0连接池,除了class值不同,属性名称也有所不同 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- c3p0的前3个连接属性的名称有所不同,这里需要注意下 -->
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql:///spring_jdbc" />
<property name="user" value="root" />
<property name="password" value="1234" />
</bean>
<!-- 注册Spring管理模板类 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!-- 注入属性值,引用类型属性用ref -->
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
测试
package blog.csdn.net.mchenys.test;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo1 {
// 通过java注解注入模板
@Resource(name = "jdbcTemplate")
private JdbcTemplate JdbcTemplate;
@Test
public void test1() {
JdbcTemplate.update("update t_account set money=? where name=?", 99, "胜哥");
}
@Test
public void test2() {
JdbcTemplate.update("update t_account set name=? where money=?", "dbcp连接池", 99);
}
@Test
public void test3() {
JdbcTemplate.update("insert into t_account values(null,?,?)", "c3p0连接池", 100);
}
}
运行test3方法,查看数据库,显示如下,这说明c3p0连接池配置成功了。