Spring學習-24:Spring中的JDBC Template(JDBC模板):預設連線池、DBCP連線池、C3P0池的配置
阿新 • • 發佈:2018-12-30
上一講中,我們編寫的測試類如下所示:
我們一般不會在程式中來寫這些引數,而是寫在配置檔案中。而從這個示例中已經看出,Spring JDBC模板的使用,必須依賴於DataSource資料庫連線池。在實際開發中,通過Spring配置檔案來配置JDBC Template。下面我們主要介紹幾種常用的連線池(DataSource)的配置:package com.js.demo1; import org.junit.Test; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DriverManagerDataSource; public class SpringTest1 { @Test public void demo1(){ //建立連線池,這裡使用的是Spring自帶的連線池 DriverManagerDataSource dataSource = new DriverManagerDataSource(); //設定引數 dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql:///spring3_day02"); dataSource.setUsername("root"); dataSource.setPassword("root"); //使用JDBC的模板,傳入DataSource,帶參構造器或者setter方法均可 JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); jdbcTemplate.execute("create table user (id int primary key auto_increment,name varchar(20))"); } }
1、Spring資料來源實現類 DriverManagerDataSource
2、DBCP資料來源 BasicDataSource
3、C3P0資料來源 ComboPooledDataSource
重點掌握第3種,下面我們依次介紹。
對於預設連線池:
1、配置Spring預設連線池以及JDBC模板類
<?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"> <!-- 配置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="root"></property> </bean> <!-- 定義JDBC的模板類 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> </beans>
2、編寫測試類:
package com.js.demo1; /** * 預設連線池配置測試 */ 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.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class SpringTest2 { @Autowired @Qualifier("jdbcTemplate") private JdbcTemplate jdbcTemplate; @Test public void demo2(){ jdbcTemplate.execute("create table user2 (id int primary key auto_increment,name varchar(20))"); } }
執行測試,表格user2建立成功。
對於DBCP連線池:
1、預設連線池的使用無需匯入其他的jar包,但使用DBCP連線池需要匯入jar包:依賴包中org.apache.commons資料夾中:
(1)com.springsource.org.apache.commons.dbcp-1.2.2.osgi.jar
(2)com.springsource.org.apache.commons.pool-1.5.3.jar
2、配置DBCP連線池
<?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">
<!-- 配置DBCP連線池 -->
<bean id="dataSource" 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="root"></property>
</bean>
<!-- 定義JDBC的模板類 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
3、編寫測試類:
package com.js.demo1;
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.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* DBCP連線池配置測試
* @author JiangShuai
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext2.xml")
public class SpringTest3 {
@Autowired
@Qualifier("jdbcTemplate")
private JdbcTemplate jdbcTemplate;
@Test
public void demo2(){
jdbcTemplate.execute("create table user3 (id int primary key auto_increment,name varchar(20))");
}
}
執行測試,表格user3成功建立。
對於C3P0連線池:
1、匯入jar包:依賴包中com.mchange.c3p0資料夾下com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar
2、配置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">
<!-- 配置C3P0連線池 -->
<bean id="dataSource" 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="root"></property>
</bean>
<!-- 定義JDBC的模板類 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
3、編寫測試類:
package com.js.demo1;
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.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* C3P0連線池配置測試
* @author JiangShuai
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext3.xml")
public class SpringTest4 {
@Autowired
@Qualifier("jdbcTemplate")
private JdbcTemplate jdbcTemplate;
@Test
public void demo2(){
jdbcTemplate.execute("create table user4 (id int primary key auto_increment,name varchar(20))");
}
}
執行測試,表格user4成功建立。