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