下面大致的说明一下
***种
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.SQLServerDriver< SPAN>property>
- <property name="connection.url">jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=acegi;SelectMethod=cursor< SPAN>property>
- <property name="connection.username">sa< SPAN>property>
- <property name="connection.password">server< SPAN>property>
- <property name="show_sql">true< SPAN>property>
- <mapping resource=""/>
- < SPAN>session-factory>
- < SPAN>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"/>
【编辑推荐】