<!-- 配置C3P0连接池 --> <bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///spring3_day02"></property> <property name="user" value="root"></property> <property name="password" value=""></property> </bean>
配置好连接池之后把它注入到JDBC模板.
21:55:24,695 INFO XmlBeanDefinitionReader:315 - Loading XML bean definitions from class path resource [applicationContext.xml]
21:55:24,969 INFO GenericApplicationContext:510 - Refreshing org.springframework.context.support.GenericApplicationContext@43401ab6: startup date [Fri May 05 21:55:24 CST 2017]; root of context hierarchy
21:55:25,127 INFO DefaultListableBeanFactory:577 - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@cf7e5b2: defining beans [dataSource,dataSource1,dataSource2,jdbcTemplate,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
21:55:25,170 INFO DriverManagerDataSource:153 - Loaded JDBC driver: com.mysql.jdbc.Driver
21:55:25,209 INFO MLog:80 - MLog clients using log4j logging.
21:55:25,394 INFO C3P0Registry:204 - Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]
21:55:25,564 INFO AbstractPoolBackedDataSource:462 - Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 2ygg1c9n1o6hot718sug49|4706c7b2, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 2ygg1c9n1o6hot718sug49|4706c7b2, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> jdbc:mysql:///spring3_day02, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 15, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ]
21:55:25,975 INFO GenericApplicationContext:1042 - Closing org.springframework.context.support.GenericApplicationContext@43401ab6: startup date [Fri May 05 21:55:24 CST 2017]; root of context hierarchy
21:55:25,975 INFO DefaultListableBeanFactory:444 - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@cf7e5b2: defining beans [dataSource,dataSource1,dataSource2,jdbcTemplate,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
初始化C3P0的连接池等等的一些信息.
<?xml version="1.0" encoding="UTF-8"?> <!-- 引入beans的头 --> <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"> <!-- 配置Spring默认的连接池 --> <!-- 这个类由Spring来帮我们创建,它默认情况下只创建一次,因为是单例的. --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql:///spring3_day02"></property> <property name="username" value="root"></property> <property name="password" value=""></property> </bean> <!-- 配置DBCP连接池 --> <bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql:///spring3_day02"></property> <property name="username" value="root"></property> <property name="password" value=""></property> </bean> <!-- 配置C3P0连接池 --> <bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///spring3_day02"></property> <property name="user" value="root"></property> <property name="password" value=""></property> </bean> <!-- 定义jdbctemplate --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource2"></property><!-- 把上面定义好的连接池注入进来了 --> </bean> </beans>
package cn.itcast.spring3.demo1; import java.sql.DriverManager; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; 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 org.springframework.transaction.jta.SpringJtaSynchronizationAdapter; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class SpringTest1 { @Autowired @Qualifier("jdbcTemplate") private JdbcTemplate jdbcTemplate;//注入Jdbc模板 @Test public void demo2(){ jdbcTemplate.execute("create table user (id int primary key auto_increment,name varchar(20))"); } @Test public void demo1(){ // 创建连接池: DriverManagerDataSource dataSource = new DriverManagerDataSource();//Spring自带的连接池 // 设置参数: dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql:///spring3_day02"); dataSource.setUsername("root"); dataSource.setPassword(""); //使用JDBC的模板: //JdbcTemplate jdbcTemplate = new JdbcTemplate(); //jdbcTemplate.setDataSource(dataSource); JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); jdbcTemplate.execute("create table user (id int primary key auto_increment,name varchar(20))"); } }