---
title: "hibernate中的映射文件加载路径"
author: "William_Third"
date: "2013-03-01T21:13:22Z"
tags: []
categories: ["编程语言"]
slug: "hibernate"
draft: false
lastmod: "2013-03-01T21:14:17Z"
id: 1820120
url: "https://william-third.iteye.com/blog/1820120"
---

hibernate中，hbm.xml映射文件加载，目前据我所知有两个，

1. web.xml中

```xml
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
```


   applicationContext.xml中，

```xml
<property name="mappingDirectoryLocations">
    <list>
	<value>/WEB-INF/classes/mappings</value>
    </list>
</property>
```


2. 若不在 web.xml通过加载 applicationContext.xml来创建数据表，而是在代码中显示加载，
在工程中创建 /WEB-INF/classes 文件夹

```java
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
```

因为 ClassPathXmlApplicationContext默认根目录在 /WEB-INF/classes

   applicationContext.xml中，

```xml
<property name="mappingDirectoryLocations">
        <list>
            <value>/mappings</value>
        </list>
</property>
```