1.通過《Ant介紹及使用》部署和測試Ant,確保能正常使用
配置環境變量
測試
2.在項目根目錄下建立一個build.xml文件,把下面的內容Copy進去
<?xml version="1.0" encoding="GBK"?> <project name="構建腳本" default="生成Hibernate配置文件" basedir=".">
<property name="src.dir" value="${basedir}/src"/> <property name="build.dir" value="${basedir}/bin"/> <property name="webapp.dir" value="${basedir}/src/webapp"/> <property name="xdoclet.home" value="D:/opensources/xdoclet/xdoclet-plugins-1.0.3"/>
<!-- Build classpath --> <path id="xdoclet.task.classpath"> <fileset dir="${xdoclet.home}/lib"> <include name="**/*.jar"/> </fileset> <fileset dir="${xdoclet.home}/plugins"> <include name="**/*.jar"/> </fileset> </path> <taskdef name="xdoclet" classname="org.xdoclet.ant.XDocletTask" classpathref="xdoclet.task.classpath" /> <target name="生成Hibernate配置文件"> <xdoclet> <fileset dir="${src.dir}/com/yzk/oa/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="root" dialect="org.hibernate.dialect.MySQLDialect" showsql="true" /> </xdoclet> </target> <target name="生成hibernate映射文件"> <xdoclet> <fileset dir="${src.dir}/com/yzk/oa/model"> <include name="**/*.java"/> </fileset> <component classname="org.xdoclet.plugin.hibernate.HibernateMappingPlugin" version="3.0" destdir="${src.dir}" /> </xdoclet> </target> </project> |
3.修改內容,主要要修改的部分以黃色標注
上面的數據庫是以MySQL為例子,如果是Oracle或者其他則要相應的其他數據庫變量
4.對實體類進行Xdoclet注釋,以便解析
package com.yzk.oa.model;
import java.util.Set; /** * @hibernate.class table="t_org1" * @author Administrator * */ public class Organization { /** * @hibernate.id generator-class="native" */ private int id; /** * @hibernate.property */ private String name; /** * @hibernate.property */ private String sn; /** * @hibernate.property */ private String description; /** * @hibernate.many-to-one column="pid" */ private Organization parent; /** * @hibernate.set lazy="extra" inverse="true" * @hibernate.key column="pid" * @hibernate.one-to-many class="com.yzk.oa.model.Organization" */ private Set children;
。。。。。這時省略了get/set內容。。。。。 public void setDescription(String description) { this.description = description; } } |
package com.yzk.oa.model; /** * @hibernate.class table="t_person1" * @author Administrator * */ public class Person { /** * @hibernate.id * generator-class="native" */ private int id; /** * @hibernate.property */ private String name; /** * @hibernate.property */ private String sex; /** * @hibernate.property */ private String address; /** * @hibernate.property */ private int age; /** * @hibernate.property */ private String duty; //職務 /** * @hibernate.property */ private String phone; /** * @hibernate.many-to-one */ private Organization org;
。。。。省略了Get/Set代碼。。。。。 } |
5.可以利用ant工具來生成了,這里我使用MyEclipse中內置的ant工具來生成
window-show views-ant/ant
Add Buildfiles --選擇項目中的build.xml文件
右鍵運行即可,然后刷新項目就可以看到生成的映射文件及配置文件了
6.大功告成~~~~