---
title: "用xdoclet自动生成hibernate的映射文件，配置文件"
author: "lmning"
date: "2009-05-16T14:34:53Z"
tags: []
slug: "xdoclet-hibernate"
draft: false
lastmod: "2009-05-16T14:34:53Z"
id: 388963
url: "https://lmning.iteye.com/blog/388963"
---

到[http://sourceforge.net/](http://sourceforge.net/)

下载xdoclet-plugins-dist-1.0.4。

 

 

如果在eclipse下的话，不需要配置（如果没有ide需要安装好ant）

 

build.xml

 

```
<?xml version="1.0" encoding="UTF-8"?>
<project name="oa" default="hibernatecfg" basedir=".">
	<property name="src.dir" value="${basedir}/src" />
	<property name="xdoclet.home" value="D:/soft/xdoclet" />

	<path id="xdoclet.classpath">
		<fileset dir="${xdoclet.home}/xdoclet-plugins-dist-1.0.4/lib">
			<include name="**/*.jar" />
		</fileset>
	</path>
	<taskdef name="xdoclet" classname="org.xdoclet.ant.XDocletTask"
		classpathref="xdoclet.classpath" />

	<target name="hibernatecfg">
		<xdoclet>
			<fileset dir="${src.dir}/com/lmning/model">
				<include name="**/*.java" />
			</fileset>
			<component
				classname="org.xdoclet.plugin.hibernate.HibernateConfigPlugin"
				destdir="${src.dir}" version="3.0" hbm2ddlauto="update"
				jdbcurl="jdbc:mysql://127.0.0.1/oa"
				jdbcdriver="com.mysql.jdbc.Driver" jdbcusername="root"
				jdbcpassword="1234" dialect="org.hibernate.dialect.MySQLDialect"
				showsql="true" />
		</xdoclet>
	</target>

	<target name="mapping">
		<xdoclet>
			<fileset dir="${src.dir}/com/lmning/model">
				<include name="**/*.java" />
			</fileset>
			<component
				classname="org.xdoclet.plugin.hibernate.HibernateMappingPlugin"
				version="3.0" destdir="${src.dir}" />
		</xdoclet>
	</target>
</project>
```

 

 model:

 

```
package com.lmning.model;

/**
 * 
 * @author Administrator
 * @hibernate.class table="User"
 */
public class User {
	
	/**
	 * @hibernate.id
	 * 		generator-class="native"
	 */
	private int id;
	
	/**
	 * @hibernate.property
	 */
	private String name;
	
	/**
	 * @hibernate.property
	 */
	private String sex;
			
	/**
	 * @hibernate.property
	 */
	private String phone;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public Orgnization getOrg() {

}
```

 

 

运行ant，ok，User.hbm.xml和hibernate.cfg.xml就自动生成了。