Spring2.5+Struts2+ Hibernate3整合之二

转载请注明:来自http://blog.csdn.net/M_ChangGong/ 作者:张燕广

该篇主要编写Spring整合Hibernate相关的配置文件。

src下创建spring配置文件applicationContext.xml,其代码如下:

  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:context="http://www.springframework.org/schema/context"  
  5.        xmlns:aop="http://www.springframework.org/schema/aop"  
  6.        xmlns:tx="http://www.springframework.org/schema/tx"  
  7.        xsi:schemaLocation="http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  8.            http://www.springframework.org/schema/context  
  9. http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  10.            http://www.springframework.org/schema/aop  
  11.            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  12.            http://www.springframework.org/schema/tx  
  13.            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  14.      <context:annotation-config/>  
  15.           
  16.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
  17.         <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>  
  18.         <property name="url" value="jdbc:mysql://localhost:3306/vote?characterEncoding=utf-8"/>  
  19.         <property name="username" value="root"/>  
  20.         <property name="password" value="zyg"/>  
  21.         <!-- 连接池启动时的初始值 -->  
  22.         <property name="initialSize" value="1"/>  
  23.         <!-- 连接池的最大值 -->  
  24.         <property name="maxActive" value="500"/>  
  25.         <!-- 最大空闲值,当经过一个高峰期后,连接池可以慢慢将已经用不到的连接进行释放,一直达到maxIdel为止 -->  
  26.         <property name="maxIdle" value="2"/>  
  27.         <!-- 最小空闲值,当空闲的连接小于阀值时,连接池会去预审一些连接,以免洪峰来时来不及去申请 -->  
  28.         <property name="minIdle" value="1"/>  
  29.     </bean>  
  30.       
  31.     <!-- 配置Hibernate -->  
  32.     <bean id="sessionFactory"  
  33.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  34.         <property name="dataSource">  
  35.             <ref local="dataSource" />  
  36.         </property>  
  37.         <property name="mappingResources">  
  38.             <list>  
  39.                 <value>com/zyg/ssh/bean/Student.hbm.xml</value>  
  40.             </list>  
  41.         </property>  
  42.         <property name="hibernateProperties">  
  43.             <value>  
  44.                 hibernate.dialect=org.hibernate.dialect.MySQLDialect  
  45.                 hibernate.hbm2ddl.auto=update  
  46.                 hibernate.show_sql=true  
  47.                 hibernate.format_sql=true  
  48.                 <!--   
  49.                     hibernate.cache.use_second_level_cache=true  
  50.                     hibernate.cache.use_query_level_cache=false  
  51.                     hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider  
  52.                  -->  
  53.             </value>  
  54.         </property>  
  55.     </bean>  
  56.     <bean id="txManger" class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  57.         <property name="sessionFactory" ref="sessionFactory"/>  
  58.     </bean>  
  59.     <tx:annotation-driven transaction-manager="txManger"/>  
  60.       
  61.     <bean id="studentDao" class="com.zyg.ssh.dao.hibernate.StudentDaoImpl">  
  62.         <property name="sessionFactory" ref="sessionFactory"/>  
  63.     </bean>  
  64.       
  65.     <bean id="studentService" class="com.zyg.ssh.service.impl.StudentServiceImpl">  
  66.         <property name="studentDao" ref="studentDao"></property>  
  67.     </bean>  
  68. </beans>  

通过上面spring配置文件的代码可以看出,在spring整合hibernate时并没有单独编写hibernate的配置文件hibernate.cfg.xml,而是将hibernate sessionFactorymappingResources相关的配置直接在spring中以bean的形式进行配置。

至此,该篇完成了Spring与Hibernate整合相关配置文件的编写。下一篇开始编写bean和bean的hibernate配置文件。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值