标签:
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下具体映射文件名
- 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路径下和其他文件夹下的映射文件
- 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>
指定classpath下的example.hbm.xml映射文件
- <property name="mappingLocations">
- <value>classpath:example.hbm.xml </value>
- </property>
也可以使用*作为通配符
3、mappingDirectoryLocations,指定包含映射文件的文件夹的目录
- 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文件中
- 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指定映射文件的包名
-
- ublic void setPackagesToScan(String... packagesToScan) {
- this.packagesToScan = packagesToScan;
- <property name="packagesToScan" value="com/anyview/entities/"></property></strong></span>
Spring整合Hibernate之AnnotationSessionFactoryBean与LocalSessionFactoryBean
标签:
原文地址:http://www.cnblogs.com/hyl8218/p/5850702.html