Spring整合Hibernate之AnnotationSessionFactoryBean與LocalSessionFactoryBean
阿新 • • 發佈:2019-02-13
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下具體對映檔名
src根目錄下的example.hbm.xml檔案/** * 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; }
<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>