spring集成hibernate由两种形式

1、继续使用Hibernate的映射文件*.hbm.xml

2、使用jpa形式的pojo对象, 去掉*.hbm.xml文件

一、继续使用Hibernate的映射文件*.hbm.xml

此时Spring的配置文件中的SeesionFactory需要使用org.springframework.orm.hibernate.LocalSessionFactoryBean

  1. <bean id="sessionFactory"
  2. class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
  3. <property name="dataSource" ref="dataSource" />
  4. <property name="hibernateProperties">
  5. <props>
  6. <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
  7. <prop key="hibernate.show_sql">true</prop>
  8. <prop key="hibernate.format_sql">true</prop>
  9. </props>
  10. </property>
  11. </bean>

LocalSessionFactoryBean可以用如下几个属性来寻找*hbm.xml文件

mappingResources、mappingLocations、mappingDirectoryLocations与mappingJarLocations

1、mappingResources:指定classpath下具体映射文件名

  1. /**
  2. * Set Hibernate mapping resources to be found in the class path,
  3. * like "example.hbm.xml" or "mypackage/example.hbm.xml".
  4. * Analogous to mapping entries in a Hibernate XML config file.
  5. * Alternative to the more generic setMappingLocations method.
  6. * <p>Can be used to add to mappings from a Hibernate XML config file,
  7. * or to specify all mappings locally.
  8. * @see #setMappingLocations
  9. * @see org.hibernate.cfg.Configuration#addResource
  10. */
  11. public void setMappingResources(String... mappingResources) {
  12. this.mappingResources = mappingResources;
  13. }

src根目录下的example.hbm.xml文件

  1. <property name="mappingResources">
  2. <value>example.hbm.xml </value>
  3. </property>

src/mypackage目录下的example.hbm.xml文件

  1. <property name="mappingResources">
  2. <value>mypackage/example.hbm.xml</value>
  3. </property>

2、mappingLocations,可以指定映射文件的路径,可以指定classpath路径下和其他文件夹下的映射文件

  1. /**
  2. * Set locations of Hibernate mapping files, for example as classpath
  3. * resource "classpath:example.hbm.xml". Supports any resource location
  4. * via Spring's resource abstraction, for example relative paths like
  5. * "WEB-INF/mappings/example.hbm.xml" when running in an application context.
  6. * <p>Can be used to add to mappings from a Hibernate XML config file,
  7. * or to specify all mappings locally.
  8. * @see org.hibernate.cfg.Configuration#addInputStream
  9. */
  10. public void setMappingLocations(Resource... mappingLocations) {
  11. this.mappingLocations = mappingLocations;
  12. }

指定WEB-INF/mappings目录下的example.hbm.xml映射文件文件

  1. <property name="mappingLocations">
  2. <value>WEB-INF/mappings/example.hbm.xml </value>
  3. </property>
  1. </pre><pre>

指定classpath下的example.hbm.xml映射文件

  1. <property name="mappingLocations">
  2. <value>classpath:example.hbm.xml </value>
  3. </property>

也可以使用*作为通配符

3、mappingDirectoryLocations,指定包含映射文件的文件夹的目录

  1. /**
  2. * Set locations of directories that contain Hibernate mapping resources,
  3. * like "WEB-INF/mappings".
  4. * <p>Can be used to add to mappings from a Hibernate XML config file,
  5. * or to specify all mappings locally.
  6. * @see org.hibernate.cfg.Configuration#addDirectory(java.io.File)
  7. */
  8. public void setMappingDirectoryLocations(Resource... mappingDirectoryLocations) {
  9. this.mappingDirectoryLocations = mappingDirectoryLocations;
  10. }

包含WEB-INF/mappings目录下的所有*hbm.xml映射文件

  1. <property name="mappingDirectoryLocations">
  2. <list>
  3. <value>WEB-INF/mappings</value>
  4. </list>
  5. </property>

也可以通过classpath来指出,此处包含classpath路径下的hbm包下的所有*.hbm.xml文件

  1. <property name="mappingDirectoryLocations">
  2. <list>
  3. <value>classpath:hbm/</value>
  4. </list>
  5. </property>

4、mappingJarLocations ,指定加载的映射文件在jar文件中

  1. /**
  2. * Set locations of jar files that contain Hibernate mapping resources,
  3. * like "WEB-INF/lib/example.hbm.jar".
  4. * <p>Can be used to add to mappings from a Hibernate XML config file,
  5. * or to specify all mappings locally.
  6. * @see org.hibernate.cfg.Configuration#addJar(java.io.File)
  7. */
  8. public void setMappingJarLocations(Resource... mappingJarLocations) {
  9. this.mappingJarLocations = mappingJarLocations;
  10. }

例如:

  1. <property name="mappingDirectoryLocations">
  2. <value>WEB-INF/lib/example.hbm.jar</value>
  3. </property>
  1. </pre><p></p><p><span style="line-height:30px"><span style="line-height:21px"><span style="font-family: FangSong_GB2312; font-size: 18px;"></span></span></span></p><p></p><p>二、使用jpa形式的pojo对象, 去掉*.hbm.xml文件</p><p>此时Spring的配置文件中的SeesionFactory需要使用<span style="color:rgb(255,0,0);">org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean</span></p><p>AnnotationSessionFactoryBean中查找jpa注解形式的pojo映射对象的属性有:annotatedClasses、packagesToScan</p><p>1、annotatedClasses:指定classpath下指定的注解映射实体类的类名</p><p><pre name="code" class="java">/**
  2. * Specify annotated classes, for which mappings will be read from
  3. * class-level annotation metadata.
  4. * @see org.hibernate.cfg.AnnotationConfiguration#addAnnotatedClass(Class)
  5. */
  6. public void setAnnotatedClasses(Class<?>... annotatedClasses) {
  7. this.annotatedClasses = annotatedClasses;
  8. }
  1. <property name="annotatedClasses"> <list> <value>com.anyview.entities.ClassTable</value> <value>com.anyview.entities.ClassStudentTable</value> </list> </property>

2、 packagesToScan指定映射文件的包名

  1. /**
  2. * Specify packages to search using Spring-based scanning for entity classes in
  3. * the classpath. This is an alternative to listing annotated classes explicitly.
  4. * <p>Default is none. Specify packages to search for autodetection of your entity
  5. * classes in the classpath. This is analogous to Spring's component-scan feature
  6. * ({@link org.springframework.context.annotation.ClassPathBeanDefinitionScanner}).
  7. */
  8. ublic void setPackagesToScan(String... packagesToScan) {
  9. this.packagesToScan = packagesToScan;
    1. <property name="packagesToScan" value="com/anyview/entities/"></property></strong></span>

Spring整合Hibernate之AnnotationSessionFactoryBean与LocalSessionFactoryBean的更多相关文章

  1. Spring框架学习(4)spring整合hibernate

    内容源自:spring整合hibernate    spring整合注解形式的hibernate 这里和上一部分学习一样用了模板模式, 将hibernate开发流程封装在ORM层提供的模板类Hiber ...

  2. Spring整合Hibernate报错:annotatedClasses is not writable or has an invalid setter method

    Spring 整合Hibernate时报错: org.springframework.beans.factory.BeanCreationException: Error creating bean ...

  3. 【Java EE 学习 53】【Spring学习第五天】【Spring整合Hibernate】【Spring整合Hibernate、Struts2】【问题:整合hibernate之后事务不能回滚】

    一.Spring整合Hibernate 1.如果一个DAO 类继承了HibernateDaoSupport,只需要在spring配置文件中注入SessionFactory就可以了:如果一个DAO类没有 ...

  4. spring整合hibernate的详细步骤

    Spring整合hibernate需要整合些什么? 由IOC容器来生成hibernate的sessionFactory. 让hibernate使用spring的声明式事务 整合步骤: 加入hibern ...

  5. spring整合hibernate

    spring整合hibernate包括三部分:hibernate的配置.hibernate核心对象交给spring管理.事务由AOP控制 好处: 由java代码进行配置,摆脱硬编码,连接数据库等信息更 ...

  6. Spring 整合 Hibernate

    Spring 整合 Hibernate •Spring 支持大多数流行的 ORM 框架, 包括 Hibernate JDO, TopLink, Ibatis 和 JPA. •Spring 对这些 OR ...

  7. 使用Spring整合Hibernate,并实现对数据表的增、删、改、查的功能

    1.1 问题 使用Spring整合Hibernate,并实现资费表的增.删.改.查. 1.2 方案 Spring整合Hibernate的步骤: 1.3 步骤 实现此案例需要按照如下步骤进行. 采用的环 ...

  8. Spring整合Hibernate详细步骤

    阅读目录 一.概述 二.整合步骤 回到顶部 一.概述 Spring整合Hibernate有什么好处? 1.由IOC容器来管理Hibernate的SessionFactory 2.让Hibernate使 ...

  9. SSH整合之spring整合hibernate

    SSH整合要导入的jar包: MySQL中创建数据库 create database ssh_db; ssh_db 一.spring整合hibernate带有配置文件hibernate.cfg.xml ...

随机推荐

  1. yaf框架使用(centos6.5)

    安装好php环境之后 安装扩展包 $yum install php-devel /usr/bin/ 就会出现phpize工具包 下载yaf-2.2.8.gz源文件,解压后,进入源文件 phpize [ ...

  2. Oralce sysaux WRH$_ACTIVE_SESSION_HISTORY清理

    In this Document Symptoms Cause Solution References Symptoms sysaux表空間的WRH$_ACTIVE_SESSION_HISTORY表變 ...

  3. hdu 3032 sg打表找规律 *

    有n堆石子,alice先取,每次可以选择拿走一堆石子中的1~x(该堆石子总数) ,也可以选择将这堆石子分成任意的两堆.alice与bob轮流取,取走最后一个石子的人胜利. 打表代码: #include ...

  4. ASP.NET Identity 3.0教程

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:我相信有些人和我一样,已经开始把ASP.NET 5用于产品开发了.不过现在最大的问题是 ...

  5. ok6410,mmu,内存管理

    MMU 一.MMU学习 MMU其实就是一个页表.将虚拟地址通过查表的方式,对应到物理地址去他由一个或一组芯片组成,一般存在与协处理器中. 1.将虚拟地址转化为物理地址 2.访问权限管理 1.1得出mm ...

  6. C++ 中 char 与 int 转换问题

    itoa 功  能:把一整数转换为字符串 函  数:char *itoa(int value, char *string, int radix); 解  释:itoa 是英文integer to ar ...

  7. 搭建Mantis 缺陷管理系统(转)

    转自 什么是Mantis MantisBT is a free popular web-based bugtracking system (feature list). It is written i ...

  8. ubuntu kylin中如何截图

    windows操作系统中,我通常使用的截图工具是QQ的“ctrl+alt+a”快捷键.但是在ubuntu中,linux qq常年不更新,我也就彻底放弃了使用了,反正ubuntu通常只是拿来开发.其实没 ...

  9. minix3(一)安装以及编辑文件

    作为一条通信狗,最近开始自学操作系统.听说用MINIX3学操作系统很好,就决定跟UCSB的课程试试. 首先在虚拟机上安装MINIX3. 开始用的VM Station,按照百度文库里安装minix3的教 ...

  10. phpexecel 导入导出,格式

    1.日期时间合并到c中 =a1 &b1 或 =a1 + b1 这些都是运算符 2.此时c1的值是这条公式,而并不是公式运算的结果 复制c,粘贴到d,选择粘贴值 3.此时c是时间日期格式的,如需 ...