Spring与Hibernate两种组合方式

Spring与Hibernate大致有两种组合方式,主要区别是一种是在Hibernate中的hibernate.cfg.xml中配置数据源,一种是借助Spring的jdbc方式在Spring的applicationContext.xml文件中配置数据源,然后在Spring配置sessionFactory的bean有些区别

下面大致的说明一下

第一种

1.hibernate.cfg.xml文件

  1. xml version='1.0' encoding='utf-8'?> 
  2.         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  3.         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"
  4.  
  5. <hibernate-configuration
  6.  
  7.     <session-factory
  8.  
  9.          
  10.         <property name="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriverproperty
  11.         <property name="connection.url">jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=acegi;SelectMethod=cursorproperty
  12.         <property name="connection.username">saproperty
  13.         <property name="connection.password">serverproperty
  14.         <property name="show_sql">trueproperty
  15.  
  16.          <mapping resource=""/> 
  17.  
  18.     session-factory
  19.  
  20. hibernate-configuration

2.在spring配置sessionFactory

  1. <bean id="sessionFactory"    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
  2.    <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> 
  3.  </bean> 
  4.  <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
  5.     <property name="sessionFactory" ref="sessionFactory"></property> 
  6.  </bean> 
  7.  <tx:annotation-driven transaction-manager="txManager"/> 

或者实现hibernate零配置

  1. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
  2.    <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> 
  3.     <property name="packagesToScan" value="com/wch/entity/"></property> 
  4.  </bean> 
  5.  <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
  6.     <property name="sessionFactory" ref="sessionFactory"></property> 
  7.  </bean> 
  8.  <tx:annotation-driven transaction-manager="txManager"/> 

第二种

  1. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 
  2.          <property name="driverClassName" value="com.microsoft.jdbc.sqlserver.SQLServerDriver"/> 
  3.          <property name="url" value="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=acegi;SelectMethod=cursor"/> 
  4.          <property name="username" value="sa"/> 
  5.          <property name="password" value="server"/> 
  6.  
  7.  
  8.    </bean> 
  9.     
  10.  <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
  11.    <property name="dataSource" value="dataSource"></property> 
  12.  
  13. <property name="mappingResources"> 
  14.      <list> 
  15.               <value>user.hbm.xml</value> 
  16.      </list> 
  17.  </property> 
  18.  <property name="hibernateProperties"> 
  19.       <props> 
  20.         <prop key="hibernate.show_sql">true</prop> 
  21.    </props> 
  22.   </property> 
  23.  </bean>    
  24.  
  25.     </bean> 
  26.  <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
  27.     <property name="sessionFactory" ref="sessionFactory"></property> 
  28.  </bean> 
  29.  <tx:annotation-driven transaction-manager="txManager"/> 

或者实现hibernate的零配置 

  1. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 
  2.         <property name="driverClassName" value="com.microsoft.jdbc.sqlserver.SQLServerDriver"/> 
  3.         <property name="url" value="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=acegi;SelectMethod=cursor"/> 
  4.         <property name="username" value="sa"/> 
  5.         <property name="password" value="server"/> 
  6.   </bean> 
  7.    
  8. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
  9.   <property name="dataSource" value="dataSource"></property> 
  10.    <property name="packagesToScan" value="com/wch/entity/"></property> 
  11. </bean> 
  12. <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
  13.    <property name="sessionFactory" ref="sessionFactory"></property> 
  14. </bean> 
  15. <tx:annotation-driven transaction-manager="txManager"/>  
上一篇:MVC拦截器加密解密


下一篇:HDU 1256 画8