spring對(duì)hibernate配置文件hibernate.cfg.xml的集成相當(dāng)好,可以在Spring中配置Hibernate的SessionFactory從而取代Hibernate.cfg.xml和HibernateSessionFactory.java
Spring在集成Hibernate時(shí)又分為兩種形式:(集成后就不需要Hibernate.cfg.xml了)
使用Hibernate的映射文件*.hbm.xml時(shí)掃描映射文件的方法
Spring集成Hibernate時(shí)去掉了Hibernate.cfg.xml,此時(shí)如果還繼續(xù)使用Hibernate的映射文件.hbm.xml的話,在配置Hibernate的 SessionFactory 時(shí)就要配置以何種方式尋找Hibernate映射文件.hbm.xml
此時(shí)spring中配置SessionFactory Bean時(shí)它對(duì)應(yīng)的class應(yīng)為org.springframework.orm.hibernate.LocalSessionFactoryBean
例如:
<!-- 配置數(shù)據(jù)源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://******:3306/database?useUnicode=true&characterEncoding=utf8"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
<property name="maxActive" value="500"></property>
<property name="maxIdle" value="100"></property>
<property name="maxWait" value="10000" />
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="60" />
<property name="logAbandoned" value="true" />
<property name="validationQuery" value="select 1" />
</bean>
<!-- 定義Hibernate的sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<!-- 數(shù)據(jù)庫(kù)連接方言 -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<!-- 在控制臺(tái)輸出SQL語(yǔ)句 -->
<prop key="hibernate.show_sql">true</prop>
<!-- 格式化控制臺(tái)輸出的SQL語(yǔ)句 -->
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.connection.release_mode">after_statement</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!--Hibernate映射文件 -->
<property name="mappingLocations">
<value>/TBS/Model/*.hbm.xml</value>
</property>
</bean>
<!-- Action -->
<bean id="LoginAction" class="PSM.Action.LoginAction">
<property name="loginDAO">
<ref local="LoginDAO"/>
</property>
</bean>
LocalSessionFactoryBean有好幾個(gè)屬性用來(lái)查找hibernate映射文件:mappingResources、mappingLocations、mappingDirectoryLocations與mappingJarLocations
他們的區(qū)別:
mappingResources:指定classpath下具體映射文件名
<property name="mappingResources">
<value>petclinic.hbm.xml </value>
</property>
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>
也可以用通配符指定,’‘指定一個(gè)文件(路徑)名,’*‘指定多個(gè)文件(路徑)名,例如:
<property name="mappingLocations">
<value>classpath:/com/company/domainmaps/*.hbm.xml </value>
</property>
上面的配置是在com/company/domain包下任何maps路徑下的hbm.xml文件都被加載為映射文件
mappingDirectoryLocations:指定映射的文件路徑
<property name="mappingDirectoryLocations">
<list>
<value>WEB-INF/HibernateMappings</value>
</list>
</property>
也可以通過(guò)classpath來(lái)指出
<property name="mappingDirectoryLocations">
<list>
<value>classpath:/XXX/package/</value>
</list>
</property>
mappingJarLocations:指定加載的映射文件在jar文件中
使用jpa注解形式的pojo對(duì)象,而去掉*.hbm.xml的Hibernate映射文件 時(shí)配置的方法
Spring集成Hibernate時(shí)去掉了Hibernate.cfg.xml,此時(shí)如果使用jpa注解形式的pojo對(duì)象,而去掉Hibernate的映射文件*.hbm.xml的話,在配置Hibernate的SessionFactory時(shí)就要配置以何種方式尋找jpa注解形式的pojo映射對(duì)象
此時(shí)spring中配置SessionFactory bean時(shí)它對(duì)應(yīng)的class應(yīng)為org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" /><!-- 引用數(shù)據(jù)源 -->
<property name="packagesToScan">
<list>
<value>com.cn.nos.services.pojo*</value><!-- 加載hibernate的jpa注解形式的實(shí)體類 -->
</list>
</property>
<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>
<!--<prop key="hibernate.current_session_context_class">thread</prop>-->
</props>
</property>
</bean>
AnnotationSessionFactoryBean中查找jpa注解形式的pojo映射對(duì)象的屬性有:annotatedClasses、packagesToScan
annotatedClasses:指定classpath下指定的注解映射實(shí)體類的類名
<property name="annotatedClasses">
<list>
<value>com.test.ObjectBean</value><!-- 可以在這個(gè)list中配置多個(gè) -->
</list>
</property>
packagesToScan指定映射文件的包名
<property name="packagesToScan">
<list>
<value>com.cn.nos.services.pojo*</value><!-- 加載hibernate的jpa注解形式的實(shí)體類 -->
</list>
</property>