Spring与Hibernate两种组合方式[转]
http://developer.51cto.com/art/200906/131127.htm
Spring与Hibernate大致有两种组合方式,主要区别是一种是在Hibernate中的hibernate.cfg.xml中配置数据源,一种是借助Spring的jdbc方式在Spring的applicationContext.xml文件中配置数据源,然后在Spring配置sessionFactory的bean有些区别:
下面大致的说明一下
第一种
1.hibernate.cfg.xml文件
xmlversion='1.0'encoding='utf-8'?>
"-//Hibernate/HibernateConfigurationDTD3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<propertyname="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriverproperty>
<propertyname="connection.url">jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=acegi;SelectMethod=cursorproperty>
<propertyname="connection.username">saproperty>
<propertyname="connection.password">serverproperty>
<propertyname="show_sql">trueproperty>
<mappingresource=""/>
session-factory>
hibernate-configuration>
2.在spring配置sessionFactory
<beanid="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<propertyname="configLocation"value="classpath:hibernate.cfg.xml"></property>
</bean>
<beanid="txManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<propertyname="sessionFactory"ref="sessionFactory"></property>
</bean>
<tx:annotation-driventransaction-manager="txManager"/>
或者实现hibernate零配置
<beanid="sessionFactory"class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<propertyname="configLocation"value="classpath:hibernate.cfg.xml"></property>
<propertyname="packagesToScan"value="com/wch/entity/"></property>
</bean>
<beanid="txManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<propertyname="sessionFactory"ref="sessionFactory"></property>
</bean>
<tx:annotation-driventransaction-manager="txManager"/>
第二种
<beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource">
<propertyname="driverClassName"value="com.microsoft.jdbc.sqlserver.SQLServerDriver"/>
<propertyname="url"value="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=acegi;SelectMethod=cursor"/>
<propertyname="username"value="sa"/>
<propertyname="password"value="server"/>
</bean>
<beanid="sessionFactory"class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<propertyname="dataSource"value="dataSource"></property>
<propertyname="mappingResources">
<list>
<value>user.hbm.xml</value>
</list>
</property>
<propertyname="hibernateProperties">
<props>
<propkey="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
</bean>
<beanid="txManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<propertyname="sessionFactory"ref="sessionFactory"></property>
</bean>
<tx:annotation-driventransaction-manager="txManager"/>
或者实现hibernate的零配置
<beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource">
<propertyname="driverClassName"value="com.microsoft.jdbc.sqlserver.SQLServerDriver"/>
<propertyname="url"value="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=acegi;SelectMethod=cursor"/>
<propertyname="username"value="sa"/>
<propertyname="password"value="server"/>
</bean>
<beanid="sessionFactory"class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<propertyname="dataSource"value="dataSource"></property>
<propertyname="packagesToScan"value="com/wch/entity/"></property>
</bean>
<beanid="txManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<propertyname="sessionFactory"ref="sessionFactory"></property>
</bean>
<tx:annotation-driventransaction-manager="txManager"/>