首先,要明确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的更多相关文章

  1. (六)Spring4 整合Hibernate4,Struts2

    第一节:S2SH 整合所需Jar 包 Struts2.3.16,Spring4.0.6,Hibernate4.3.5 整合所需jar 包: Struts2.3.16 jar 包 Spring4.0.6 ...

  2. Spring4整合Hibernate4详细示例

    1. Spring整合Hibernate,主要是解决什么问题? a.让Spring提供的IOC容器来管理Hibernate的SessionFactory b.让Hibernate使用Spring提供的 ...

  3. spring4 整合hibernate4时遇到的问题以及解决办法

    配置hibernate时出现了如下错误: Java.lang.NoClassDefFoundError: org/hibernate/util/DTDEntityResolver 错误原因:hiber ...

  4. 【j2ee spring】27、巴巴荆楚网-整合hibernate4+spring4(2)

    巴巴荆楚网-整合hibernate4+spring4(2) 1.图文项目 2.首先我们引入对应的jar包 这里用的是oracle 11g,所以我们使用的数据库连接jar包是ojdbc6, 的区别就是支 ...

  5. 【Spring实战-2】Spring4.0.4整合Hibernate4.3.6

    作者:ssslinppp      源程序下载:http://download.csdn.net/detail/ssslinppp/8751185  1. 摘要 本文主要讲解如何在Spring4.0. ...

  6. Spring4 MVC Hibernate4集成 Annotation

    Spring4 MVC Hibernate4集成 Annotation 一.本文所用环境 二.工程目录 三.Maven添加依赖 四.新建数据库表 五.配置文件 六.Model层 七.DAO层 八.Se ...

  7. Spring4 MVC Hibernate4集成

      Spring4 MVC Hibernate4集成 一.    本文所用环境 Spring4.0.3.RELEASE Hibernate4.3.5.Final Mysql 二.    工程目录 三. ...

  8. 基于Struts2,Spring4,Hibernate4框架的系统架构设计与示例系统实现

    笔者在大学中迷迷糊糊地度过了四年的光景,心中有那么一点目标,但总感觉找不到发力的方向. 在四年间,尝试写过代码结构糟糕,没有意义的课程设计,尝试捣鼓过Android开发,尝试探索过软件工程在实际开发中 ...

  9. 基于全注解的SpringMVC+Spring4.2+hibernate4.3框架搭建

    概述 从0到1教你搭建spring+springMVC+hibernate整合框架,基于注解. 本教程框架为基于全注解的SpringMVC+Spring4.2+hibernate4.3,开发工具为my ...

随机推荐

  1. CentOS下安装Redmine 2.5.2

    Redmine是用Ruby开发的基于web的项目管理软件,所以先要下载安装Ruby,再下载对 Ruby组件进行打包的 Ruby 打包系统RubyGems. 第一步:下载必要的软件 1.依赖包安装 # ...

  2. Candy

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

  3. 【leetcode】Find Peak Element ☆

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  4. who is in front of me 解题报告

    题目描述:N(1<=N<=50005)个学生站成一个纵队,每个人只能看到前面身高比他高(严格大于)的人 求所有人中能看到的最大人数 分析:对于某个人A,设前面第一个身高比他高的人是B.如果 ...

  5. POJ 3440 Coin Toss(求概率)

    题目链接 题意 :把硬币往棋盘上扔,分别求出硬币占1,2,3,4个格子的时候的概率. 思路 : 求出公式输出,不过要注意输出格式,我还因为输入的时候用了int类型错了好几次..... #include ...

  6. excel公式应用大全

    excel公式应用大全 1.ABS函数 函数名称:ABS 主要功能:求出相应数字的绝对值. 使用格式:ABS(number) 参数说明:number代表需要求绝对值的数值或引用的单元格. 应用举例:如 ...

  7. HTML5入门1---Canvas画布

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  8. C++Builder和VC的比较

    C++Builder和VC的比较 其实很久以前我就想写这篇文章,其原因一方面是因为笔者深深感觉到C++ Builder的确是一个先进与强大的程式开发工具,但更最重要的一点是,我深信C++ Builde ...

  9. jQuery 表单验证插件 jQuery Validation Engine 使用

    jQuery 表单验证插件 jQuery Validation Engine 使用方式如下: 1.引入头文件(注意一定要把jQuery放在前面),指定使用 jQuery Validation Engi ...

  10. android中最先被执行的activity

    像C.C++.JAVA都有一个主函数作为程序的入口点,但是Android中并没有一个明确的主窗口,那么在有多个Activity的情况下,最先被执行的是哪个呢?这完全取决于配置文件AndroidMain ...