如何利用Ant和XDoclet产生映射文件
来源:优易学  2011-9-29 14:43:08   【优易学:中国教育考试门户网】   资料下载   IT书店
  //User.java
  在企业中model类的属性可能有几百个而且还可能继承了很多属性,这样的model如果手写映射文件岂不是很大的工程!
  这里介绍Ant+XDoclet配合来自动生成映射文件。
  必备:Ant和XDocle的jar包。
  操作很简单,自己写一个model类例如People.java,但是要自动生成映射文件这个类需要有注释,写一个build.xml文件,下载Xdoclet,网址:http://xdoclet.sourceforge.net/
  新建包com.test.model,存放实体类Group,User
  package dbdemo;
  import java.util.Date;
  import java.util.Set;
  /**
  * @hibernate.class table="Users"
  *
  * @author Ethan
  *
  * Represents a User
  */
  public class User {
  private String userID;
  private String userName;
  private String password;
  private String emailAddress;
  private Date lastLogon;
  private Set contacts;
  private Set books;
  private Address address;
  /**
  * @hibernate.property column="EmailAddress" type="string"
  * @return String
  */
  public String getEmailAddress() {
  return emailAddress;
  }
  /**
  * @hibernate.property column="LastLogon" type="date"
  * @return Date
  */
  public Date getLastLogon() {
  return lastLogon;
  }
  /**
  * @hibernate.property column="Password" type="string"
  * @return String
  */
  public String getPassword() {
  return password;
  }
  /**
  * @hibernate.id generator-class="assigned" type="string"
  *column="LogonID"
  * @return String
  */
  public String getUserID() {
  return userID;
  }
  /**
  * @hibernate.property column="Name" type="string"
  * @return String
  */
  public String getUserName() {
  return userName;
  }
  /**
  * @param string
  */
  public void setEmailAddress(String string) {
  emailAddress = string;
  }
  /**
  * @param string
  */
  public void setLastLogon(Date date) {
  lastLogon = date;
  }
  /**
  * @param string
  */
  public void setPassword(String string) {
  password = string;
  }
  /**
  * @param string
  */
  public void setUserID(String string) {
  userID = string;
  }
  /**
  * @param string
  */
  public void setUserName(String string) {
  userName = string;
  }
  /**
  * @hibernate.set role="contacts" table="Contacts"
  *cascade="all" readonly="true"
  * @hibernate.collection-key column="User_ID"
  * @hibernate.collection-one-to-many class="dbdemo.Contact"
  * @return java.util.Set
  */
  public Set getContacts() {
  return contacts;
  }
  /**
  * @param set
  */
  public void setContacts(Set set) {
  contacts = set;
  }
  /**
  * @hibernate.set role="books" table="Book_User_Link"
  *cascade="all" eadonly="true"
  * @hibernate.collection-key column="UserID"
  * @hibernate.collection-many-to-many
  *class="dbdemo.Book" column="BookID"
  * @return java.util.Set
  */
  public Set getBooks() {
  return books;
  }
  /**
  * @param set
  */
  public void setBooks(Set set) {
  books = set;
  }
  /**
  * @hibernate.one-to-one class="dbdemo.Address"
  * @return dbdemo.Address
  */
  public Address getAddress() {
  return address;
  }
  /**
  * @param address
  */
  public void setAddress(Address address) {
  this.address = address;
  }
  }
  //在test目录下建立build.xml,其中<property name="xdoclet.home" value="C:/xdoclet-plugins-dist-1.0.4">为你所解压的xdoclet目录。 Ant build File build.xml
  <project name="Hibernate Example" default="about" basedir=".">
  <!-- The location where your xdoclet jar files reside -->
  <property name="xdoclet.lib.home" value="c:/java_api/xdoclet-1.2b3/lib"/>
  <target name="clean" depends="init" description="removes all directories
  related to this build">
  <delete dir="${dist}"/>
  </target>
  <target name="init" description="Initializes properties that are used by
  other targets.">
  <property name="dist" value="dist"/>
  </target>
  <target name="prepare" depends="init,clean" description="creates dist dir
  ectory">
  <echo message="Creating required directories..."/>
  <mkdir dir="${dist}"/>
  </target>
  <target name="hibernate" depends="prepare"
  description="Generates Hibernate class descriptor files.">
  <taskdef name="hibernatedoclet" classname="xdoclet.
  modules.hibernate.HibernateDocletTask"><classpath>
  <fileset dir="${xdoclet.lib.home}">
  <include name="*.jar"/>
  </fileset>
  </classpath>
  </taskdef>
  <!-- Execute the hibernatedoclet task -->
  <hibernatedoclet
  destdir="."
  excludedtags="@version,@author,@todo"
  force="true"
  verbose="true"
  mergedir="${dist}">
  <fileset dir=".">
  <include name="**/dbdemo/*.java"/>
  </fileset>
  <hibernate version="2.0"/>
  </hibernatedoclet>
  </target>
  <target name="about" description="about this build file" depends="init">
  <echo message="Use this format for the arguments:"/>
  <echo message="ant hibernate"/>
  <echo message=""/>
  </target>
  </project>
  执行过程:  Windows-->ShowView-->Other-->Ant文件里面(Ant)-->在Ant空白处右键 -->Add Buildfiles-->选择你要生成配置文件的bulild.xml文件点击OK,让后分别执行,所要生成的文件即可.赶快试试吧...

责任编辑:小草

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训