1. 程式人生 > >DBCP,C3P0,druid連線池配置使用

DBCP,C3P0,druid連線池配置使用

                                   Apache DBCP連線池配置

Apache commons-dbcp 需要匯入dbcp包和 pool包 ,可以

從spring-framework-3.0.2.RELEASE-dependencies包中找到。

  1. com.springsource.org.apache.commons.dbcp-1.2.2.osgi.jar
  2. com.springsource.org.apache.commons.pool-1.5.3.jar

找到org.apache.commons路徑

配置applicationContext.xml檔案

<!-- 配置apache的dbcp連線池 -->

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">

       <property name="driverClassName" value="com.mysql.jdbc.Driver"/>

       <property name="url" value="jdbc:mysql:///igeekspring"/>

       <property name="username" value="root"/>

       <property name="password" value="root"/>

    </bean>

   

    <!-- jdbctemplate物件 -->

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">

       <!-- 注入資料來源 -->

       <property name="dataSource" ref="dataSource"/>

    </bean>

測試類:

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations="classpath:applicationContext.xml")

public class SpringTest {

    //注入要測試bean

    @Autowired

    private JdbcTemplate jdbcTemplate;

   

    @Test

    public void testCreatetable(){

       jdbcTemplate.execute("create table test003(id int,name varchar(20))");

    }



}

                                          C3P0 連線池配置

匯入C3P0的jar,可以從spring-framework-3.0.2.RELEASE-dependencies包中找到。

路徑在com.mchange.c3p0中

  1. com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar

配置applicationContext.xml檔案

<!-- c3p0連線池 -->

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

       <property name="driverClass" value="com.mysql.jdbc.Driver"/>

       <property name="jdbcUrl" value="jdbc:mysql:///igeekspring"/>

       <property name="user" value="root"/>

       <property name="password" value="root"/>

    </bean>

    <!-- jdbctemplate物件 -->

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">

       <!-- 注入資料來源 -->

       <property name="dataSource" ref="dataSource"/>

    </bean>

測試類:

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations="classpath:applicationContext.xml")

public class SpringTest {

    //注入要測試bean

    @Autowired

    private JdbcTemplate jdbcTemplate;

   

    @Test

    public void testCreatetable(){

       jdbcTemplate.execute("create table test004(id int,name varchar(20))");

    }



}

                                      Druid 連線池配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop" 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

http://www.springframework.org/schema/aop 
                           http://www.springframework.org/schema/aop/spring-aop.xsd
">

	<!-- 載入資原始檔 -->
	<context:property-placeholder location="classpath:/db.properties"/>

	<!-- 開啟掃描 -->
	<context:component-scan base-package="com.igeek.crm"/>

	<!-- 配置DataSource -->
	<!-- 使用德魯伊連線池 -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
		<property name="url" value="${jdbc.url}"/>
		<property name="driverClassName" value="${jdbc.driverClass}"/>
		<property name="username" value="${jdbc.username}"/>
		<property name="password" value="${jdbc.password}"/>
        <property name="initialSize" value="${jdbc.initialSize}"/>
		<property name="maxActive" value="${jdbc.maxActive}"/>
		<property name="filters" value="stat,slf4j"/>
	</bean>
	
	<!-- jdbc的模板類 -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<!-- 注入資料來源 -->
		<property name="dataSource" ref="dataSource"/>
	</bean>
</beans>

外部屬性檔案的配置 db.properties

模擬需求:

現在資料來源的相關引數配置,是測試環境下的。

現在,要將工程搭建在正式的伺服器上,因為測試環境和正式環境的資料庫肯定不是一個,所以肯定首先要更改資料來源相關的配置。

缺點:必須手動修改applicationContext.xml檔案,容易造成誤操作。

解決方案:不修改。可以將資料來源相關配置引數,外接。

目的:可以將xml配置中可能要經常修改內容,抽取到一個properties檔案

應用:使用properties檔案配置引數,如資料庫連線引數等。

第一步: src新建db.properties

         將經常需要修改變數抽取出來

jdbc.driverClass=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql:///igeekspring

jdbc.username=root

jdbc.password=root

第二步: 配置applicationContext.xml檔案,在applicationContext.xml 通過

         <context:property-placeholder> 引入外部屬性檔案

         通過${key} 引用屬性的值  

   <!-- 引入外部屬性配置檔案-->

    <context:property-placeholder location="classpath:db.properties"/>

   

    <!-- 配置內建的資料來源bean,使用db.properties -->

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

       <property name="driverClassName" value="${jdbc.driverClass}"/>

       <property name="url" value="${jdbc.url}"/>

       <property name="username" value="${jdbc.username}"/>

       <property name="password" value="${jdbc.password}"/>

    </bean>

第三步:使用SpringTest.java進行測試

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations="classpath:applicationContext.xml")

public class SpringTest {

    //注入要測試bean

    @Autowired

    private JdbcTemplate jdbcTemplate;

   

    @Test

    public void testCreatetable(){

       jdbcTemplate.execute("create table test006(id int,name varchar(20))");

    }



}