Spring4整合Hibernate4
首先,要明确Spring整合Hibernate可以做什么?
答案是:
1.由IOC容器来管理Hibernate的SessionFactory
2.让Hibernate使用上Spring的声明式事务
整合的过程以一个实例来说明。
在整合的中,hibernate的一些配置都可以放在spring的配置文件中。但是为了使配置文件看起啦比较清晰,建议还是分开存放。
比如在db.properties中存放数据库信息,hibernate.cfg.xml中存放hibernate的基本信息。其余的配置信息可以放在spring的配置文件(applicationContext.xml)中。
db.properties
jdbc.user=yulei
jdbc.password=yulei
jdbc.driverClass=oracle.jdbc.driver.OracleDriver
jdbc.jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl jdbc.initialPoolSize=5
jdbc.maxPoolSize=10
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 配置连接数据库的基本信息 -->
<!--
1.数据源配置到IOC容器中,所以此处不再需要配置数据源
2.关联的.hbm.xml也在IOC容器配置SessionFactory实例时再进行配置
3.配置Hibernate的基本属性:方言、SQL显式及格式化、生成数据表的策略以及二级缓存等
--> <!-- 配置hibernate基本信息 -->
<!-- hibernate所使用的数据库方言 -->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> <!-- 执行操作时是否在控制台打印sql -->
<property name="show_sql">true</property> <!-- 是否对SQL进行格式化 -->
<property name="format_sql">true</property> <!-- 指定自动生成数据表的策略 -->
<property name="hbm2ddl.auto">update</property> </session-factory>
</hibernate-configuration>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <!-- 配置自动扫描的包 -->
<context:component-scan base-package="com.yl.spring.hibernate"></context:component-scan> <!-- 配置数据源 -->
<!-- 导入资源文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置c3p0数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="initialPoolSize" value="${jdbc.initialPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean> <!-- 配置 Hibernate 的 SessionFactory 实例 : 通过Spring 提供的LocalSessionFactoryBean配置-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 配置数据源属性 -->
<property name="dataSource" ref="dataSource"></property> <!-- 配置 hibernate 配置文件的名称及位置 -->
<!-- <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> -->
<!-- 使用hibernateProperties属性来配置Hibernate原生的属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property> <!-- 配置 hibernate 映射文件的位置及名称 , 可以使用通配符-->
<property name="mappingLocations" value="classpath:com/yl/spring/hibernate/entities/*.hbm.xml"></property>
</bean> <!-- 配置 Spring 的声明式事务 -->
<!-- 1.配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 2.配置事务属性 , 需要事务管理器 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<!-- 添加下面此行后改变了 事务的隔离级别 -->
<!-- <tx:method name="purchase" propagation="REQUIRES_NEW"/> -->
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- 3.配置事务切点, 并把切点和事务属性联系起来 -->
<aop:config>
<aop:pointcut expression="execution(* com.yl.spring.hibernate.service.*.*(..))"
id="txPointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config> </beans>
以上三个配置文件基本上就是Spring整合Hibernate所需的配置。
Spring4整合Hibernate4的更多相关文章
- (六)Spring4 整合Hibernate4,Struts2
第一节:S2SH 整合所需Jar 包 Struts2.3.16,Spring4.0.6,Hibernate4.3.5 整合所需jar 包: Struts2.3.16 jar 包 Spring4.0.6 ...
- Spring4整合Hibernate4详细示例
1. Spring整合Hibernate,主要是解决什么问题? a.让Spring提供的IOC容器来管理Hibernate的SessionFactory b.让Hibernate使用Spring提供的 ...
- spring4 整合hibernate4时遇到的问题以及解决办法
配置hibernate时出现了如下错误: Java.lang.NoClassDefFoundError: org/hibernate/util/DTDEntityResolver 错误原因:hiber ...
- 【j2ee spring】27、巴巴荆楚网-整合hibernate4+spring4(2)
巴巴荆楚网-整合hibernate4+spring4(2) 1.图文项目 2.首先我们引入对应的jar包 这里用的是oracle 11g,所以我们使用的数据库连接jar包是ojdbc6, 的区别就是支 ...
- 【Spring实战-2】Spring4.0.4整合Hibernate4.3.6
作者:ssslinppp 源程序下载:http://download.csdn.net/detail/ssslinppp/8751185 1. 摘要 本文主要讲解如何在Spring4.0. ...
- Spring4 MVC Hibernate4集成 Annotation
Spring4 MVC Hibernate4集成 Annotation 一.本文所用环境 二.工程目录 三.Maven添加依赖 四.新建数据库表 五.配置文件 六.Model层 七.DAO层 八.Se ...
- Spring4 MVC Hibernate4集成
Spring4 MVC Hibernate4集成 一. 本文所用环境 Spring4.0.3.RELEASE Hibernate4.3.5.Final Mysql 二. 工程目录 三. ...
- 基于Struts2,Spring4,Hibernate4框架的系统架构设计与示例系统实现
笔者在大学中迷迷糊糊地度过了四年的光景,心中有那么一点目标,但总感觉找不到发力的方向. 在四年间,尝试写过代码结构糟糕,没有意义的课程设计,尝试捣鼓过Android开发,尝试探索过软件工程在实际开发中 ...
- 基于全注解的SpringMVC+Spring4.2+hibernate4.3框架搭建
概述 从0到1教你搭建spring+springMVC+hibernate整合框架,基于注解. 本教程框架为基于全注解的SpringMVC+Spring4.2+hibernate4.3,开发工具为my ...
随机推荐
- c# 4.0新特性一览
原文:http://www.cnblogs.com/palo/archive/2009/03/01/1400949.html 终于静下心来仔细听了一遍Anders Hejlsberg(Visual S ...
- How to use Mac Terminal
Mac OS X 启用超级用户的方法Root user,又名超级用户,是一个权力最高的Unix 账户,Root 的账户能在整个系统里任何部份进行任何“操作”,包括:拷贝档案.移动/移除档案.执行程序等 ...
- 典型重构3 (Try/Catch)
Try/Catch 块过多 public Customer GetCustomer(string customerId) { try { var command = new SqlCommand(); ...
- lintcode:落单的数
题目: 落单的数 给出2*n + 1 个的数字,除其中一个数字之外其他每个数字均出现两次,找到这个数字. 样例 给出 [1,2,2,1,3,4,3],返回 4 挑战 一次遍历,常数级的额外空间复杂度 ...
- lintcode: 把排序数组转换为高度最小的二叉搜索树
题目: 把排序数组转换为高度最小的二叉搜索树 给一个排序数组(从小到大),将其转换为一棵高度最小的排序二叉树. 样例 给出数组 [1,2,3,4,5,6,7], 返回 4 / \ 2 6 / \ / ...
- jvm调优具体参数配置
3.JVM参数 在JVM启动参数中,可以设置跟内存.垃圾回收相关的一些参数设置,默认情况不做任何设置JVM会工作的很好,但对一些配置很好的Server和具体的应用必须仔细调优才能获得最佳性能.通过设置 ...
- Photoshop:模拟钢笔压力
预期效果: 方法: 按B选择画笔工具,设置"钢笔压力" 路径描边:选择路径,右击路径,选择“描边路径” 或在路径面板,选择: 即可得到预期效果!
- spring 定时任务的 执行时间设置规则(转)
spring 定时任务的 执行时间设置规则 单纯针对时间的设置规则org.springframework.scheduling.quartz.CronTriggerBean允许你更精确地控制任务的运 ...
- (转)SSI开发环境搭建
本文转自:http://blog.csdn.net/lifuxiangcaohui/article/details/7187869 先来点文字性的描述: MVC对于我们来说,已经不陌生了,它起源于20 ...
- 255. Verify Preorder Sequence in Binary Search Tree
题目: Given an array of numbers, verify whether it is the correct preorder traversal sequence of a bin ...