3、 整合hibernate a)
Web.xml openonesessionInView
<!-- Hibernate Open Session in View Filter-->
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
b) Spring 中配置
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://localhost:3306/test_szoa?characterEncoding=utf-8"></property>
<property name="username" value="root"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLInnoDBDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingDirectoryLocations">
<list>
<value>classpath:icom/szoa/</value>
</list>
</property>
<!-- 自动扫描hibernate 注解 -->
<property name="packagesToScan">
<list>
<value>icom.**.domain</value>
</list>
</property>
</bean>

本文介绍如何将Hibernate与Spring框架进行整合。具体包括在Web.xml中配置OpenSessionInView过滤器,确保每个请求期间都有一个活跃的Hibernate会话,以及在Spring中配置数据源和SessionFactory,通过使用注解的方式实现ORM映射。
1万+

被折叠的 条评论
为什么被折叠?



