Spring —— 三种配置数据源的方式:spring内置、c3p0、dbcp

时间:2023-03-08 16:24:28
Spring ——  三种配置数据源的方式:spring内置、c3p0、dbcp

  

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

Spring ——  三种配置数据源的方式:spring内置、c3p0、dbcp

 jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc\:oracle\:thin\:@localhost\:\:orcl
jdbc.user=zym
jdbc.password=zymm

jdbc.properties

引用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"/>

 <?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!--. Database connection settings spring内置 数据库连接设置-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <!-- 驱动类 -->
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<!-- url地址 -->
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
<property name="username" value="zym"></property>
<property name="password" value="zymm"></property>
</bean> <!-- .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:@localhost:1521:orcl"></property>
<property name="username" value="zym"></property>
<property name="password" value="zymm"></property>
</bean> <!-- .c3p0 配置数据源 -->
<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> <bean id="MyjdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="empDao" class="zym.jdbctemplate.dao.impl.EmpDaoImpl">
<property name="jdbcTemplate" ref="MyjdbcTemplate"></property>
</bean> <bean id="empService" class="zym.jdbctemplate.service.impl.IEmpServiceImpl">
<property name="dao" ref="empDao"></property>
</bean> <!-- 让Spring去寻找jdbc.properties -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"></property>
</bean>
</beans>

三种方式配置code

三种方式配置code实例,带jar包版:链接:http://pan.baidu.com/s/1c2l2WwC 密码:d2mu