hibernate在Spring下的配置

<?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">
           
    <!--加载我的资源文件:dbconn/prpperties -->      

 
 <bean id="propertyConfigurer"
  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations">
   <list>
    <value>classpath:dbconn.properties</value>
   </list>
  </property>
 </bean>

<!--资源文件如下:

#oracle10g
#jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
#jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
#jdbc.username=scott
#jdbc.password=zitadmin
#hibernate.dialect=org.hibernate.dialect.Oracle10gDialect

#mysql
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=root
hibernate.dialect=org.hibernate.dialect.MySQLDialect

这个文件要重新建立并且我给它命名为:dbconn.properties

-->

<!--  ${ } 是获取资源文件里的值-->
 <!--第一种是: dbcp配置
 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
  destroy-method="close">
  <property name="driverClassName">
   <value>${jdbc.driverClassName}</value>
  </property>
  <property name="url">
   <value>${jdbc.url}</value>
  </property>
  <property name="username">
   <value>${jdbc.username}</value>
  </property>
  <property name="password">
   <value>${jdbc.password}</value>
  </property>
  <property name="maxActive" value="20" />
  <property name="minIdle" value="1" />

  <property name="initialSize" value="1" />
  <property name="maxWait" value="20" />
 </bean>
  -->
<!-- 第二种:c3p0配置(首选:因为hibernate官方的人说dbcp有bug) -->
 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
 <property name="driverClass" value="org.gjt.mm.mysql.Driver"/>
 <property name="jdbcUrl" value="${jdbc.url}" />
 <property name="user" value="${jdbc.username}" />
 <property name="password" value="${jdbc.password}" />
 <property name="initialPoolSize" value="1" />
 <property name="minPoolSize" value="1" />
 <property name="maxPoolSize" value="300" />
 <!-- 最大空闲时间,60表示60秒内无使用,则连接被丢弃,如果为0,表示始终保持连接 Default:0-->
 <property name="maxIdleTime" value="60" />
 <!-- 当连接池中的连接耗尽时,c3p0一次同时获取的连接数 Default:3 -->
 <property name="acquireIncrement" value="5" />
 <!-- 每60秒检查所有连接池中的空闲连接 Default:0-->
 <property name="idleConnectionTestPeriod" value="60" />
</bean>
  
 <!-- 得到sessionFactory -->


 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  <property name="dataSource">
   <ref bean="dataSource" />
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">${hibernate.dialect}</prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.format_sql">true</prop>
    <!-- 
    <prop key="hibernate.current_session_context_class">thread</prop>
    -->
    <prop key="hibernate.hbm2ddl.auto">update</prop>
   </props>
  </property>
  <!-- 注解的包或者类(此类是用 hibernate注解方式的)   annotatedClasses:加载指定类    packagesToScan:加载指定包-->
  <property name="packagesToScan">
   <list>
    <value>com.ad.model</value>
   </list>

  </property>

</property>

<!--非注解方式
 <property name="mappingResources">
  <list>
  <value>com/ad/model/User.hbm.xml</value>
  </list>
 </property>

-->
 </bean>
 
</beans>

posted @ 2012-08-03 15:33  紫韵轩  阅读(199)  评论(0编辑  收藏  举报