spring与hibernate整合
2016-09-28 12:27
169 查看
spring2.5+hibernate3.3整合:
jar包:
hibsrnate核心包下:
hibernate3.jar
lib/required/*.jar
lib/optional/ehcache-1.2.3.jar
hibernate注解包下的lib/test/slf4j12.jar
spring安装包下的
dist/spring.jar
dist/modules/spring-webmvc-struts.jar
lib/jakarta-commons/commons-logging.jar
lib/aspectj/aspectjweaver.jar/aspectjrt.jar
lib/cglib/cglib-nodep_2.1.3.jar
lib/j2ee/common-annotations.jar
lib/log4j/log4j-1.2.15.jar
第一步:
导入jar包,spring有关包,hibernate有关包,数据库有关包。
第二步:
编写beans.xml配置文件
装载数据源:
注册sessionFactory:
如果spring需要使用二级缓存的话,将二级缓存的cfg.xml中的信息也放在hibernateProperties的value属性中。
springframework还提供了OpenSessionInView过滤器,我们可以自行配置用于hibernate开发。例子:
注册事务管理器(使用的是spring的hibernate事务管理器):
beans.xml全代码:
jdbc.properties
jar:
jar不敢保证,但是其他配置大概就是这样了。
jar包:
hibsrnate核心包下:
hibernate3.jar
lib/required/*.jar
lib/optional/ehcache-1.2.3.jar
hibernate注解包下的lib/test/slf4j12.jar
spring安装包下的
dist/spring.jar
dist/modules/spring-webmvc-struts.jar
lib/jakarta-commons/commons-logging.jar
lib/aspectj/aspectjweaver.jar/aspectjrt.jar
lib/cglib/cglib-nodep_2.1.3.jar
lib/j2ee/common-annotations.jar
lib/log4j/log4j-1.2.15.jar
第一步:
导入jar包,spring有关包,hibernate有关包,数据库有关包。
第二步:
编写beans.xml配置文件
装载数据源:
<context:property-placeholder location="classpath:jdbc.properties"/> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClassName}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="maxPoolSize" value="${jdbc.maxPoolSize}"/> <property name="minPoolSize" value="${jdbc.minPoolSize}"/> <property name="initialPoolSize" value="${jdbc.initialPoolSize}"/> <property name="maxIdleTime" value="${jdbc.maxIdleTime}"/> <property name="acquireIncrement" value="${jdbc.acquireIncrement}"/> </bean>
注册sessionFactory:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mappingResources"> <list> <value>cn/itcast/domain/Person.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.hbm2ddl.auto=update hibernate.show_sql=true </value> </property> </bean> sessionFactory中mappingResources加载映射文件,hibernateProperties属性配置hibernate一些配置属性,dataSource属性配置数据源
如果spring需要使用二级缓存的话,将二级缓存的cfg.xml中的信息也放在hibernateProperties的value属性中。
springframework还提供了OpenSessionInView过滤器,我们可以自行配置用于hibernate开发。例子:
<!-- spring的openSessionInView过滤器 --> <filter> <filter-name>openSessionInView</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>openSessionInView</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
注册事务管理器(使用的是spring的hibernate事务管理器):
<!-- 注册事务管理器 --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean>
beans.xml全代码:
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc\:mysql\://localhost\:3306/customers?useUnicode\=true&characterEncoding\=UTF-8 jdbc.username=guodaxia jdbc.password=961012gz jdbc.maxPoolSize=100 jdbc.minPoolSize=10 jdbc.initialPoolSize=20 jdbc.maxIdleTime=600 jdbc.acquireIncrement=5
jdbc.properties
jar:
jar不敢保证,但是其他配置大概就是这样了。
相关文章推荐
- 整合 struts spring hibernate
- Tapestry+spring+hibernate整合使用时HQL语句问题!
- 12月1日struts、spring、hibernate等框架的整合培训日记
- 成功整合spring+hibernate+jbpm
- 快速整合struts+spring+hibernate
- Tapestry+Spring+Hibernate整合工作小结[转]
- spring hibernate struts整合开发实例
- spring+hibernate+jbpm整合成功
- 10月31日spring与hibernate整合培训日记
- spring hibernate struts整合开发实例
- Tapestry+Spring+Hibernate整合工作小结
- Tapestry + Spring + Hibernate 整合工作及相关问题
- Hibernate3与spring的整合应用
- 12月1日struts、spring、hibernate等框架的整合培训日记
- Tapestry Spring Hibernate整合工作小结
- Tapestry+Spring+Hibernate整合工作小结
- Tapestry Spring Hibernate整合工作
- spring、hibernate、tapestry整合篇
- 12月1日struts、spring、hibernate等框架的整合培训日记
- spring hibernate struts整合开发实例