各种数据源配置之Spring+Hibernate配置DBCP数据源

beans.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:context="http://www.springframework.org/schema/context"
       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-2.5.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
   
    <context:annotation-config/><!-- Spring依赖注入注解功能 -->
   
    <!-- Hibernate SessionFactory -->  
<!-- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" autowire="autodetect">  
    <property name="mappingResources">
    <list>
    <value>cn/itcast/bean/Person.hbm.xml</value    </list>
    </property>
    <property name="hibernateProperties">
        <props>  
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
            <prop key="hibernate.connection.provider_class">com.jolbox.bonecp.provider.BoneCPConnectionProvider</prop>  
            <prop key="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</prop>  
            <prop key="hibernate.connection.url">jdbc:mysql://localhost:3306/batch?useUnicode=true&amp;characterEncoding=UTF-8</prop>  
            <prop key="hibernate.connection.username">root</prop>  
            <prop key="hibernate.connection.password">root</prop>  
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="bonecp.idleMaxAge">240</prop>
            <prop key="bonecp.idleConnectionTestPeriod">60</prop>  
            <prop key="bonecp.partitionCount">3</prop> 
            <prop key="bonecp.acquireIncrement">10</prop>  
            <prop key="bonecp.maxConnectionsPerPartition">60</prop> 
            <prop key="bonecp.minConnectionsPerPartition">20</prop> 
            <prop key="bonecp.statementsCacheSize">50</prop> 
            <prop key="bonecp.releaseHelperThreads">3</prop>  
        </props>  
    </property>  
</bean>
-->
   
    <context:property-placeholder location="classpath:jdbc.properties"/>//采用占位符进行配置
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${driverClassName}"/>
    <property name="url" value="${url}"/>
    <property name="username" value="${username}"/>
    <property name="password" value="${password}"/>
    <property name="initialSize" value="${initialSize}"/>
    <property name="maxActive" value="${maxActive}"/>
    <property name="maxIdle" value="${maxIdle}"/>
    <property name="minIdle" value="${minIdle}"/>
    </bean>
   
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="mappingResources">
    <list>
    <value>com/huhui/bean/Person.hbm.xml</value>    </list>
    </property>
    <property name="hibernateProperties">    <value>
    hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
    hibernate.hbm2ddl.auto=update
    hibernate.show_sql=false
    hibernate.format_sql=false
    </value>
    </property>
    </bean>   
    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"><!-- 配置一个针对hibernate的事务管理器 -->
    <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
   
    <!-- 采用@Transactional注解方式使用事务 -->
    <tx:annotation-driven transaction-manager="txManager"/>
</beans>

web.xml代码:
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:beans.xml</param-value>
  </context-param>

jdbc.properties代码:
driverClassName=org.gjt.mm.mysql.Driver
url=jdbc\:mysql\://localhost\:3306/huhui?useUnicode\=true&characterEncoding\=UTF-8
username=root
password=root
maxIdle=2
minIdle=1
initialSize=1
maxActive=500

上面同时给出了在Spring里配置BoneCP数据源。在JPA中配置C3P0或BDCP数据源时可以参照在“ JPA里配置BoneCP数据源”。在此,常用的数据源配置都已给出。

猜你喜欢

转载自javahuhui.iteye.com/blog/1456981