dbcp2、c3p0、druid连接池的简单配置
引入Maven依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>08-spring</artifactId> <groupId>com.taotao</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>day37_jdbc</artifactId> <properties> <junit.version>4.12</junit.version> <spring.version>4.2.4.RELEASE</spring.version> <slf4j.version>1.6.4</slf4j.version> <mysql.version>5.1.8</mysql.version> <dbcp.version>2.1.1</dbcp.version> <c3p0.version>0.9.1.2</c3p0.version> <druid.version>1.0.9</druid.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-dbcp2</artifactId> </dependency> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> </dependencies> </project>
配置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 class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate0"> <constructor-arg ref="dataSource" name="dataSource"/> </bean> <bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate1"> <constructor-arg ref="basicDataSource" name="dataSource"/> </bean> <bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate2"> <constructor-arg ref="comboPooledDataSource" name="dataSource"/> </bean> <bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate3"> <constructor-arg ref="druidDataSource" name="dataSource"/> </bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="url" value="jdbc:mysql://172.28.128.5:3306/spring_day03"/> <property name="username" value="ttsc"/> <property name="password" value="redhat"/> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="connectionProperties"> <props> <prop key="useUnicode">yes</prop> <prop key="characterEncoding">utf8</prop> </props> </property> </bean> <bean class="org.apache.commons.dbcp2.BasicDataSource" id="basicDataSource"> <property name="url" value="jdbc:mysql://172.28.128.5:3306/spring_day03"/> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="username" value="ttsc"/> <property name="password" value="redhat"/> <property name="connectionProperties" value="useUnicode=yes;characterEncoding=utf8"/> </bean> <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="comboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="user" value="ttsc"/> <property name="password" value="redhat"/> <property name="jdbcUrl" value="jdbc:mysql://172.28.128.5:3306/spring_day03?useUnicode=true&characterEncoding=utf-8"/> </bean> <bean class="com.alibaba.druid.pool.DruidDataSource" id="druidDataSource" destroy-method="close"> <property name="url" value="jdbc:mysql://172.28.128.5:3306/spring_day03"/> <property name="username" value="ttsc"/> <property name="password" value="redhat"/> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="connectionProperties" value="useUnicode=yes;characterEncoding=utf8"/> </bean> </beans>
编写测试代码
package com.itheima.demo1; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DriverManagerDataSource; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.annotation.Resource; import java.util.Properties; /** * Created by Eric on 3/9/17. */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(value = "classpath:applicationContext.xml") public class demo1 { @Resource(name = "jdbcTemplate0") private JdbcTemplate template0; @Resource(name = "jdbcTemplate1") private JdbcTemplate template1; @Resource(name = "jdbcTemplate2") private JdbcTemplate template2; @Resource(name = "jdbcTemplate3") private JdbcTemplate template3; @Test public void run1() throws Exception { Properties properties = new Properties(); properties.put("useUnicode", "yes"); properties.put("characterEncoding", "utf8"); DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://172.28.128.5:3306/spring_day03"); dataSource.setUsername("ttsc"); dataSource.setPassword("redhat"); dataSource.setConnectionProperties(properties); JdbcTemplate template = new JdbcTemplate(dataSource); template.update("INSERT INTO t_account VALUES (NULL ,?,?)", "小明", 1000); } @Test public void run2() throws Exception { template0.update("INSERT INTO t_account VALUES (NULL ,?,?)", "冠希", 1000); } @Test public void run3() throws Exception { template1.update("INSERT INTO t_account VALUES (NULL ,?,?)", "小强", 1000); } @Test public void run4() throws Exception { template2.update("INSERT INTO t_account VALUES (NULL ,?,?)", "小美", 1000); } @Test public void run5() throws Exception { template3.update("INSERT INTO t_account VALUES (NULL ,?,?)", "小六", 1000); } }