Spring中数据源配置(全)

2017-06-03  本文已影响0人  LinkedIn

DBCP数据源

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"       
        destroy-method="close">       
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />      
    <property name="url" value="jdbc:mysql://localhost:3309/sampledb" />      
    <property name="username" value="root" />      
    <property name="password" value="1234" />      
</bean>

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:@localhost:1521:ora9i "/>      
    <property name="user" value="admin"/>      
    <property name="password" value="1234"/>      
</bean>  
<bean id="propertyConfigurer"     
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">      
    <property name="location" value="/WEB-INF/jdbc.properties"/>      
</bean>      
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"       
        destroy-method="close">      
    <property name="driverClassName" value="${jdbc.driverClassName}" />      
    <property name="url" value="${jdbc.url}" />      
    <property name="username" value="${jdbc.username}" />      
    <property name="password" value="${jdbc.password}" />      
</bean>   
在jdbc.properties属性文件中定义属性值: 
    jdbc.driverClassName= com.mysql.jdbc.Driver 
    jdbc.url= jdbc:mysql://localhost:3309/sampledb 
    jdbc.username=root 
    jdbc.password=1234 
    提示 经常有开发者在${xxx}的前后不小心键入一些空格,这些空格字符将和变量合并后作为属性的值。如: <property name="username" value=" ${jdbc.username} "></property> 的属性配置项,在前后都有空格,被解析后,username的值为“ 1234 ”,这将造成最终的错误,因此需要特别小心。

J NDI数据源基于WEB容器

xml 代码
<beans xmlns=http://www.springframework.org/schema/beans    
xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance    
xmlns:jee=http://www.springframework.org/schema/jee    
xsi:schemaLocation="http://www.springframework.org/schema/beans     
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd     
http://www.springframework.org/schema/jee    
http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">      
<jee:jndi-lookup id="dataSource" jndi-name=" java:comp/env/jdbc/bbt"/>      
</beans>  
上一篇下一篇

猜你喜欢

热点阅读