spring整合hibernage配置

class下面两种类都能实现:

1  org.springframework.orm.hibernate3.LocalSessionFactoryBean

   2  org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean

 

注解形式配置session工场 

 

<!--Hibernate SessionFatory-->

        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
                <property name="dataSource" ref="dataSource"/>
                <!-- Annotation 式的配置 
                
                <property name="packagesToScan" value="com.ambo.jdt.example.anno_domain" />-->
                
                <!-- hbm 式的配置 -->
                <property name="mappingDirectoryLocations">
                        <list>
                                <value>classpath*:com/ambo/vte/hbm</value>
                                 <!-- JBPM hbm file -->
                                 <value>classpath*:/org/jbpm/**/*.hbm.xml</value>
                        </list>
                </property>
                
               <!--   hbm属性的第二种设置方法       
             <property name="mappingResources"> 
                    <list> 
                        <value>tarena/hbm/catelog.hbm.xml</value> 
                        <value>tarena/hbm/bookinfo.hbm.xml</value> 
                        <value>tarena/hbm/userinfo.hbm.xml</value> 
                        <value>tarena/hbm/order.hbm.xml</value> 
                    </list> 
                </property> 
                -->      
 
                <property name="hibernateProperties">
                        <props>
                                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                                <prop key="hibernate.show_sql">true</prop>
                                <prop key="hibernate.format_sql">true</prop>
                                <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
                                <prop key="hibernate.cache.use_query_cache">true</prop>
                                <!--<prop key="hibernate.hbm2ddl.auto">update</prop>-->
                                <!--<prop key="hibernate.jdbc.batch_size">30</prop>-->
                                
                        </props>
                </property>
        </bean>

 

=====================配置方法2 

1 配置hibernate的配置文件

2 将hbm配置文件放到spring的配置文件中

 

<hibernate-configuration>
<session-factory>
   <property name="dialect">
    org.hibernate.dialect.SQLServerDialect
   </property>
   <property name="connection.url">
    jdbc:microsoft:sqlserver://localhost:1433;databasename=hibernate
   </property>
   <property name="connection.username">sa</property>
   <property name="connection.password">111111</property>
   <property name="connection.driver_class">
    com.microsoft.jdbc.sqlserver.SQLServerDriver
   </property>
   <property name="myeclipse.connection.profile">MSSQL</property>
   <property name="show_sql">true</property>
   <mapping resource="org/shirdrn/entity/Person.hbm.xml" />
</session-factory>
</hibernate-configuration>
 
spring为:
<bean id="springSessionFactory"
   class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
   <property name="configLocation">
    <value>file:src/hibernate.cfg.xml</value>
   </property>
</bean>

 

=============================

 

对hibernate 的实体映射文件的查找

 

hibernate.cfg.xml

org.springframework.orm.hibernate.LocalSessionFactoryBean

 

LocalSessionFactoryBean有好几个属性用来查找hibernate映射文件:mappingResources、mappingLocations、mappingDirectoryLocations与mappingJarLocations

他们的区别:

 

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

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

 

 

2   mappingLocations:可以指定任何文件路径,并且可以指定前缀:classpath、file等

<property name="mappingLocations">
    <value>/WEB-INF/petclinic.hbm.xml</value>
</property>
<property name="mappingLocations">
    <value>classpath:/com/company/domain/petclinic.hbm.xml</value>
</property>

 

 

也可以用通配符指定,'*'指定一个文件(路径)名,'**'指定多个文件(路径)名,例如:

<property name="mappingLocations">
    <value>classpath:/com/company/domainmaps/*.hbm.xml</value>
</property>

 

上面的配置是在com/company/domain包下任何maps路径下的hbm.xml文件都被加载为映射文件

mappingDirectoryLocations:指定映射的文件路径

mappingJarLocations:指定加载的映射文件在jar文件中

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(spring,Hibernate)