在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 内容如下:
- <?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>
<?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
- connection.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
- connection.username=system
- connection.password=system
- 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
- dialect=org.hibernate.dialect.OracleDialect
- format_sql=true
- show_sql=true
- connection.pool_size=3
dialect=org.hibernate.dialect.OracleDialect
format_sql=true
show_sql=true
connection.pool_size=3
大家注意到了,
- <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>
<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配置不用关心了。
本文详细介绍了Spring框架中五种不同的数据源配置方法,包括jdbc、dbcp、c3p0及结合PropertyPlaceholderConfigurer的方式,并提供了完整的XML配置示例。

被折叠的 条评论
为什么被折叠?



