Spring整合Hibernate之AnnotationSessionFactoryBean与LocalSessionFactoryBean

来源:互联网 发布:mac office365 编辑:程序博客网 时间:2024/05/22 04:41

Spring集成Hibernate由两种形式

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

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


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

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

<bean id="sessionFactory"class="org.springframework.orm.hibernate.LocalSessionFactoryBean"><property name="dataSource" ref="dataSource" /><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop><prop key="hibernate.show_sql">true</prop><prop key="hibernate.format_sql">true</prop></props></property></bean>

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

mappingResources、mappingLocations、mappingDirectoryLocations与mappingJarLocations 

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

/** * Set Hibernate mapping resources to be found in the class path, * like "example.hbm.xml" or "mypackage/example.hbm.xml". * Analogous to mapping entries in a Hibernate XML config file. * Alternative to the more generic setMappingLocations method. * <p>Can be used to add to mappings from a Hibernate XML config file, * or to specify all mappings locally. * @see #setMappingLocations * @see org.hibernate.cfg.Configuration#addResource */public void setMappingResources(String... mappingResources) {this.mappingResources = mappingResources;}
src根目录下的example.hbm.xml文件

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

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

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


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

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

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

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

</pre><pre>

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

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

也可以使用*作为通配符


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

/** * Set locations of directories that contain Hibernate mapping resources, * like "WEB-INF/mappings". * <p>Can be used to add to mappings from a Hibernate XML config file, * or to specify all mappings locally. * @see org.hibernate.cfg.Configuration#addDirectory(java.io.File) */public void setMappingDirectoryLocations(Resource... mappingDirectoryLocations) {this.mappingDirectoryLocations = mappingDirectoryLocations;}
包含WEB-INF/mappings目录下的所有*hbm.xml映射文件

<property name="mappingDirectoryLocations"><list><value>WEB-INF/mappings</value></list></property>

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

<property name="mappingDirectoryLocations"><list><value>classpath:hbm/</value></list></property>


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

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

<property name="mappingDirectoryLocations">     <value>WEB-INF/lib/example.hbm.jar</value></property>

</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">/** * Specify annotated classes, for which mappings will be read from * class-level annotation metadata. * @see org.hibernate.cfg.AnnotationConfiguration#addAnnotatedClass(Class) */public void setAnnotatedClasses(Class<?>... annotatedClasses) {     this.annotatedClasses = annotatedClasses;}



 

<property name="annotatedClasses"> <list> <value>com.anyview.entities.ClassTable</value> <value>com.anyview.entities.ClassStudentTable</value> </list> </property>





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

        /** * Specify packages to search using Spring-based scanning for entity classes in * the classpath. This is an alternative to listing annotated classes explicitly. * <p>Default is none. Specify packages to search for autodetection of your entity * classes in the classpath. This is analogous to Spring's component-scan feature * ({@link org.springframework.context.annotation.ClassPathBeanDefinitionScanner}). */public void setPackagesToScan(String... packagesToScan) {this.packagesToScan = packagesToScan;}

<property name="packagesToScan" value="com/anyview/entities/"></property></strong></span>


0 0
原创粉丝点击