Spring与Hibernate两种组合方式

第一种

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"/>  

posted on 2013-10-14 18:41  我竟然比我还帅  阅读(275)  评论(0编辑  收藏  举报

导航