标签:
01.Spring内置数据源配置
Class:DriverManagerDataSource
全限定名:org.springframework.jdbc.datasource.DriverManagerDataSource
不需要添加任何jar
02.apache的 dbcp数据源配置
Class:BasicDataSource
全限定名:org.apache.commons.dbcp.BasicDataSource
需要添加:com.springsource.org.apache.commons.dbcp-1.2.2.osgi.jar
com.springsource.org.apache.commons.pool-1.5.3.jar
03.c3p0的 数据源配置
Class:ComboPooledDataSource
全限定名:com.mchange.v2.c3p0.ComboPooledDataSource
需要添加:com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar
引用外部的数据源配置信息设置:${jdbc.*}
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
jdbc.properties
1 jdbc.driver=oracle.jdbc.driver.OracleDriver 2 jdbc.url=jdbc\:oracle\:thin\:@localhost\:1521\:orcl 3 jdbc.user=zym 4 jdbc.password=zymm
引用jdbc.properties 的两种方式书写:
<!-- 01.让Spring去寻找jdbc.properties -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"></property>
</bean>
<!-- 02.让Spring去寻找jdbc.properties -->
<context:property-placeholder location="classpath:jdb.properties"/>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xsi:schemaLocation=" 7 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 9 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 10 "> 11 <!--01. Database connection settings spring内置 数据库连接设置--> 12 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 13 14 <!-- 驱动类 --> 15 <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property> 16 <!-- url地址 --> 17 <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property> 18 <property name="username" value="zym"></property> 19 <property name="password" value="zymm"></property> 20 </bean> 21 22 23 24 <!-- 02.dbcp 配置数据源 --> 25 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 26 <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property> 27 <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property> 28 <property name="username" value="zym"></property> 29 <property name="password" value="zymm"></property> 30 </bean> 31 32 <!-- 03.c3p0 配置数据源 --> 33 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 34 <property name="driverClass" value="${jdbc.driver}"></property> 35 <property name="jdbcUrl" value="${jdbc.url}"></property> 36 <property name="user" value="${jdbc.user}"></property> 37 <property name="password" value="${jdbc.password}"></property> 38 </bean> 39 40 41 42 <bean id="MyjdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 43 <property name="dataSource" ref="dataSource"></property> 44 </bean> 45 <bean id="empDao" class="zym.jdbctemplate.dao.impl.EmpDaoImpl"> 46 <property name="jdbcTemplate" ref="MyjdbcTemplate"></property> 47 </bean> 48 49 <bean id="empService" class="zym.jdbctemplate.service.impl.IEmpServiceImpl"> 50 <property name="dao" ref="empDao"></property> 51 </bean> 52 53 <!-- 让Spring去寻找jdbc.properties --> 54 <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 55 <property name="location" value="classpath:jdbc.properties"></property> 56 </bean> 57 </beans>
三种方式配置code实例,带jar包版:链接:http://pan.baidu.com/s/1c2l2WwC 密码:d2mu
Spring —— 三种配置数据源的方式:spring内置、c3p0、dbcp
标签:
原文地址:http://www.cnblogs.com/john69-/p/5962848.html