spring配置数据源的4种方式--简介

本文详细介绍了Spring框架中五种不同的数据源配置方法,包括jdbc、dbcp、c3p0及结合PropertyPlaceholderConfigurer的方式,并提供了完整的XML配置示例。

在spring中配置数据源这是做项目不可避免的,今天我把了解到的配置方式在这里做个总结。

本人目前知道4种方式。

1.jdbc

org.springframework.jdbc.datasource.DriverManagerDataSource

 

2.dbcp

org.apache.commons.dbcp.BasicDataSource

 

3.c3p0

com.mchange.v2.c3p0.ComboPooledDataSource

 

4.jndi

org.springframework.jndi.JndiObjectFactoryBean

 

首先,jdbc建立连接是只要有连接就新建一个connection,根本没有连接池的作用。不常用。

其次,jndi需要在web server中配置数据源,不方便于部署。不推荐。

常用的还是dbcp和c3p0.

 

这里我给一个有jdbc,  dbcp,  c3p0  配置数据源的例子。(经过本人测试)。

 

applicationContext.xml 内容如下:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans  
  3.     xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xmlns:p="http://www.springframework.org/schema/p"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  7.   
  8.    
  9.     <bean id="propertyConfigurer"  
  10.        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  11.         <property name="locations">  
  12.             <list>  
  13.                 <value>/dataSource.properties</value>  
  14.                 <value>/hibernateProperties.properties</value>  
  15.             </list>  
  16.         </property>  
  17.     </bean>  
  18.   
  19.     <!-- spring配置数据源方式1,dbcp   -->  
  20.     <!--    
  21.     <bean id="dataSource"  
  22.         class="org.apache.commons.dbcp.BasicDataSource">  
  23.         <property name="driverClassName"  
  24.             value="oracle.jdbc.driver.OracleDriver">  
  25.         </property>  
  26.         <property name="url"  
  27.             value="jdbc:oracle:thin:@127.0.0.1:1521:orcl">  
  28.         </property>  
  29.         <property name="username" value="system"></property>  
  30.         <property name="password" value="system"></property>  
  31.     </bean>  
  32.     -->  
  33.       
  34.      <!-- spring配置数据源方式2,dbcp   propertyConfigurer-->  
  35.      <!--   
  36.     <bean id="dataSource"  
  37.         class="org.apache.commons.dbcp.BasicDataSource">  
  38.         <property name="driverClassName"  
  39.             value="${connection.driver_class}">  
  40.         </property>  
  41.         <property name="url"  
  42.             value="${connection.url}">  
  43.         </property>  
  44.         <property name="username" value="${connection.username}"></property>  
  45.         <property name="password" value="${connection.password}"></property>  
  46.     </bean>  
  47.      -->  
  48.        
  49.      <!-- spring配置数据源方式3, c3p0 -->  
  50.      <!--     
  51.     <bean id="dataSource"  
  52.         class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">  
  53.           
  54.         <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/>           
  55.         <property name="jdbcUrl" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/>           
  56.         <property name="user" value="system"/>           
  57.         <property name="password" value="system"/>   
  58.     </bean>  
  59.     -->  
  60.       
  61.      <!-- spring配置数据源方式4, c3p0  propertyConfigurer -->  
  62.      <!--     
  63.     <bean id="dataSource"  
  64.         class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">  
  65.           
  66.         <property name="driverClass" value="${connection.driver_class}"/>           
  67.         <property name="jdbcUrl" value="${connection.url}"/>           
  68.         <property name="user" value="${connection.username}"/>           
  69.         <property name="password" value="${connection.password}"/>   
  70.     </bean>  
  71.     -->  
  72.       
  73.     <!-- spring配置数据源方式5, jdbc   -->  
  74.     <!--    
  75.     <bean id="dataSource"   
  76.          class="org.springframework.jdbc.datasource.DriverManagerDataSource">    
  77.          <property name="driverClassName">    
  78.             <value>oracle.jdbc.driver.OracleDriver</value>    
  79.          </property>    
  80.         <property name="url">    
  81.              <value>jdbc:oracle:thin:@127.0.0.1:1521:orcl</value>    
  82.          </property>    
  83.          <property name="username">    
  84.              <value>system</value>    
  85.          </property>    
  86.          <property name="password">    
  87.             <value>system</value>    
  88.          </property>    
  89.      </bean>   
  90.     -->  
  91.     <!-- spring配置数据源方式6, jdbc  propertyConfigurer -->  
  92.     <bean id="dataSource"   
  93.          class="org.springframework.jdbc.datasource.DriverManagerDataSource">    
  94.          <property name="driverClassName">    
  95.             <value>${connection.driver_class}</value>    
  96.          </property>    
  97.         <property name="url">    
  98.              <value>${connection.url}</value>    
  99.          </property>    
  100.          <property name="username">    
  101.              <value>${connection.username}</value>    
  102.          </property>    
  103.          <property name="password">    
  104.             <value>${connection.password}</value>    
  105.          </property>    
  106.      </bean>   
  107.       
  108.     <bean id="sessionFactory"  
  109.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  110.           
  111.         <property name="dataSource">  
  112.             <ref bean="dataSource" />  
  113.         </property>  
  114.           
  115.         <!-- spring配置hibernate属性,方式一,使用hibernate.cfg.xml文件 -->  
  116.         <!--   
  117.         <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>  
  118.          -->  
  119.           
  120.         <!-- spring配置hibernate属性,方式二,直接配置属性值 -->  
  121.         <!--    
  122.         <property name="hibernateProperties">  
  123.             <props>  
  124.                 <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>  
  125.                 <prop key="hibernate.format_sql">true</prop>  
  126.                 <prop key="hibernate.show_sql">true</prop>  
  127.                 <prop key="hibernate.connection.pool_size">3</prop>  
  128.             </props>  
  129.         </property>  
  130.           
  131.         <property name="mappingResources">  
  132.             <list>  
  133.                <value>event.hbm.xml</value>  
  134.                <value>person.hbm.xml</value>  
  135.             </list>  
  136.         </property>  
  137.           
  138.         -->  
  139.           
  140.         <!-- spring配置hibernate属性,方式三,配置助手propertyConfigurer使用properties文件 -->  
  141.         <property name="hibernateProperties">  
  142.             <props>  
  143.                 <prop key="hibernate.dialect">${dialect}</prop>  
  144.                 <prop key="hibernate.format_sql">${format_sql}</prop>  
  145.                 <prop key="hibernate.show_sql">${show_sql}</prop>  
  146.                 <prop key="hibernate.connection.pool_size">${connection.pool_size}</prop>  
  147.             </props>  
  148.         </property>  
  149.           
  150.         <property name="mappingResources">  
  151.             <list>  
  152.                <value>event.hbm.xml</value>  
  153.                <value>person.hbm.xml</value>  
  154.             </list>  
  155.         </property>  
  156.           
  157.     </bean>  
  158.       
  159.     </beans>  
<?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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

 
    <bean id="propertyConfigurer"
       class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
	    <property name="locations">
	    	<list>
	    		<value>/dataSource.properties</value>
	    		<value>/hibernateProperties.properties</value>
	    	</list>
	    </property>
    </bean>

    <!-- spring配置数据源方式1,dbcp   -->
    <!--  
	<bean id="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName"
			value="oracle.jdbc.driver.OracleDriver">
		</property>
		<property name="url"
			value="jdbc:oracle:thin:@127.0.0.1:1521:orcl">
		</property>
		<property name="username" value="system"></property>
		<property name="password" value="system"></property>
	</bean>
	-->
	
	 <!-- spring配置数据源方式2,dbcp   propertyConfigurer-->
	 <!-- 
	<bean id="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName"
			value="${connection.driver_class}">
		</property>
		<property name="url"
			value="${connection.url}">
		</property>
		<property name="username" value="${connection.username}"></property>
		<property name="password" value="${connection.password}"></property>
	</bean>
	 -->
	 
	 <!-- spring配置数据源方式3, c3p0 -->
	 <!--   
	<bean id="dataSource"
		class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
		
	    <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/>         
        <property name="jdbcUrl" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/>         
        <property name="user" value="system"/>         
        <property name="password" value="system"/> 
	</bean>
	-->
	
	 <!-- spring配置数据源方式4, c3p0  propertyConfigurer -->
	 <!--   
	<bean id="dataSource"
		class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
		
	    <property name="driverClass" value="${connection.driver_class}"/>         
        <property name="jdbcUrl" value="${connection.url}"/>         
        <property name="user" value="${connection.username}"/>         
        <property name="password" value="${connection.password}"/> 
	</bean>
	-->
	
	<!-- spring配置数据源方式5, jdbc   -->
	<!--  
	<bean id="dataSource" 
         class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
         <property name="driverClassName">  
            <value>oracle.jdbc.driver.OracleDriver</value>  
         </property>  
        <property name="url">  
             <value>jdbc:oracle:thin:@127.0.0.1:1521:orcl</value>  
         </property>  
         <property name="username">  
             <value>system</value>  
         </property>  
         <property name="password">  
            <value>system</value>  
         </property>  
     </bean> 
	-->
	<!-- spring配置数据源方式6, jdbc  propertyConfigurer -->
	<bean id="dataSource" 
         class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
         <property name="driverClassName">  
            <value>${connection.driver_class}</value>  
         </property>  
        <property name="url">  
             <value>${connection.url}</value>  
         </property>  
         <property name="username">  
             <value>${connection.username}</value>  
         </property>  
         <property name="password">  
            <value>${connection.password}</value>  
         </property>  
     </bean> 
	
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		
		<!-- spring配置hibernate属性,方式一,使用hibernate.cfg.xml文件 -->
		<!--  
		<property name="configLocation" value="classpath:hibernate.cfg.xml"></property> 
		 -->
		
		<!-- spring配置hibernate属性,方式二,直接配置属性值 -->
		<!--  
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.connection.pool_size">3</prop>
			</props>
		</property>
		
		<property name="mappingResources">
            <list>
               <value>event.hbm.xml</value>
               <value>person.hbm.xml</value>
            </list>
        </property>
		
		-->
		
		<!-- spring配置hibernate属性,方式三,配置助手propertyConfigurer使用properties文件 -->
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">${dialect}</prop>
				<prop key="hibernate.format_sql">${format_sql}</prop>
				<prop key="hibernate.show_sql">${show_sql}</prop>
				<prop key="hibernate.connection.pool_size">${connection.pool_size}</prop>
			</props>
		</property>
		
		<property name="mappingResources">
            <list>
               <value>event.hbm.xml</value>
               <value>person.hbm.xml</value>
            </list>
        </property>
        
	</bean>
	
	</beans>


 

 

其中涉及到的properties文件有:

 

dataSource.properties

 

  1.   
  2. connection.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl  
  3. connection.username=system  
  4. connection.password=system  
  5.   
  6. connection.driver_class=oracle.jdbc.driver.OracleDriver  
connection.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
connection.username=system
connection.password=system

connection.driver_class=oracle.jdbc.driver.OracleDriver


hibernateProperties.properties

 

  1.   
  2. dialect=org.hibernate.dialect.OracleDialect  
  3.   
  4. format_sql=true  
  5. show_sql=true  
  6. connection.pool_size=3  
dialect=org.hibernate.dialect.OracleDialect

format_sql=true
show_sql=true
connection.pool_size=3


 

 

大家注意到了,

  1. <bean id="propertyConfigurer"  
  2.        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  3.         <property name="locations">  
  4.             <list>  
  5.                 <value>/dataSource.properties</value>  
  6.                 <value>/hibernateProperties.properties</value>  
  7.             </list>  
  8.         </property>  
  9.     </bean>  
<bean id="propertyConfigurer"
       class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
	    <property name="locations">
	    	<list>
	    		<value>/dataSource.properties</value>
	    		<value>/hibernateProperties.properties</value>
	    	</list>
	    </property>
    </bean>


这个只是帮助你把有些需要写在xml文件外的属性值从某个properties文件读取,你用${属性名} 就

可以取到了。这其实并没有改变连接池的本质,还属于同一种连接方法。

 

本例主要讲解dataSource的配置。

至于本例中的sessionFactory配置不用关心了。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值