---
title: "mappingLocations、mappingDirectoryLocations与mappingJarLocations 区别"
author: "AutomaticThoughts"
date: "2012-09-24T10:49:08Z"
tags: []
categories: ["企业架构"]
slug: "mappinglocations-mappingdirectorylocations-mappingjarlocations"
draft: false
lastmod: "2012-09-24T10:49:08Z"
id: 1684163
url: "https://automaticthoughts.iteye.com/blog/1684163"
---

**原文地址：**[mappingLocations、mappingDirectoryLocations与mappingJarLocations 区别](http://blog.sina.com.cn/s/blog_4cef5c7b0100vpf9.html "mappingLocations、mappingDirectoryLocations与mappingJarLocations <wbr>区别")**作者：**[☆水『若寒](http://blog.sina.com.cn/u/1290755195 "☆水『若寒")

由于spring对hibernate配置文件hibernate.cfg.xml的集成相当好，   
所以，在项目中我一直使用spring的org.springframework.orm.hibernate.LocalSessionFactoryBean来取代hibernate.cfg.xml文件的功能   
LocalSessionFactoryBean有好几个属性用来查找hibernate映射文件：mappingResources、mappingLocations、mappingDirectoryLocations与mappingJarLocations   
他们的区别：   
mappingResources：指定classpath下具体映射文件名   
&lt;property name="mappingResources"&gt;   
    &lt;value&gt;petclinic.hbm.xml &lt;/value&gt;   
&lt;/property&gt;   
mappingLocations：可以指定任何文件路径，并且可以指定前缀：classpath、file等   
&lt;property name="mappingLocations"&gt;   
    &lt;value&gt;/WEB-INF/petclinic.hbm.xml &lt;/value&gt;   
&lt;/property&gt; 

&lt;property name="mappingLocations"&gt;   
    &lt;value&gt;classpath:/com/company/domain/petclinic.hbm.xml &lt;/value&gt;   
&lt;/property&gt;   
也可以用通配符指定，'\*'指定一个文件(路径)名，'\*\*'指定多个文件(路径)名，例如：   
&lt;property name="mappingLocations"&gt;   
    &lt;value&gt;classpath:/com/company/domainmaps/\*.hbm.xml &lt;/value&gt;   
&lt;/prop