当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
计算机二级辅导:利用Ant和XDoclet自动产生映射文件例子
发布时间:2010/3/14 18:07:04 来源:城市学习网 编辑:MOON
  //User.java
  在企业中model类的属性可能有几百个而且还可能继承了很多属性,这样的model如果手写映射文件岂不是很大的工程!
  这里介绍Ant+XDoclet配合来自动生成映射文件。
  必备:Ant和XDocle的jar包。
  操作很简单,自己写一个model类例如People.java,但是要自动生成映射文件这个类需要有注释,写一个build.xml文件,下载Xdoclet,网址:http://xdoclet.sourceforge.net/
  新建包com.test.model,存放实体类Group,User
  packagedbdemo;
  importjava.util.Date;
  importjava.util.Set;
  /**
  *@hibernate.classtable="Users"
  *
  *@authorEthan
  *
  *RepresentsaUser
  */
  publicclassUser{
  privateStringuserID;
  privateStringuserName;
  privateStringpassword;
  privateStringemailAddress;
  privateDatelastLogon;
  privateSetcontacts;
  privateSetbooks;
  privateAddressaddress;
  /**
  *@hibernate.propertycolumn="EmailAddress"type="string"
  *@returnString
  */
  publicStringgetEmailAddress(){
  returnemailAddress;
  }
  /**
  *@hibernate.propertycolumn="LastLogon"type="date"
  *@returnDate
  */
  publicDategetLastLogon(){
  returnlastLogon;
  }
  /**
  *@hibernate.propertycolumn="Password"type="string"
  *@returnString
  */
  publicStringgetPassword(){
  returnpassword;
  }
  /**
  *@hibernate.idgenerator-type="string"
  *column="LogonID"
  *@returnString
  */
  publicStringgetUserID(){
  returnuserID;
  }
  /**
  *@hibernate.propertycolumn="Name"type="string"
  *@returnString
  */
  publicStringgetUserName(){
  returnuserName;
  }
  /**
  *@paramstring
  */
  publicvoidsetEmailAddress(Stringstring){
  emailAddress=string;
  }
  /**
  *@paramstring
  */
  publicvoidsetLastLogon(Datedate){
  lastLogon=date;
  }
  /**
  *@paramstring
  */
  publicvoidsetPassword(Stringstring){
  password=string;
  }
  /**
  *@paramstring
  */
  publicvoidsetUserID(Stringstring){
  userID=string;
  }
  /**
  *@paramstring
  */
  publicvoidsetUserName(Stringstring){
  userName=string;
  }
  /**
  *@hibernate.setrole="contacts"table="Contacts"
  *cascade="all"readonly="true"
  *@hibernate.collection-keycolumn="User_ID"
  *@hibernate.collection-one-to-many
  *@returnjava.util.Set
  */
  publicSetgetContacts(){
  returncontacts;
  }
  /**
  *@paramset
  */
  publicvoidsetContacts(Setset){
  contacts=set;
  }
  /**
  *@hibernate.setrole="books"table="Book_User_Link"
  *cascade="all"eadonly="true"
  *@hibernate.collection-keycolumn="UserID"
  *@hibernate.collection-many-to-many
  *column="BookID"
  *@returnjava.util.Set
  */
  publicSetgetBooks(){
  returnbooks;
  }
  /**
  *@paramset
  */
  publicvoidsetBooks(Setset){
  books=set;
  }
  /**
  *@hibernate.one-to-one
  *@returndbdemo.Address
  */
  publicAddressgetAddress(){
  returnaddress;
  }
  /**
  *@paramaddress
  */
  publicvoidsetAddress(Addressaddress){
  this.address=address;
  }
  }
  //在test目录下建立build.xml,其中property name="xdoclet.home" value="C:/xdoclet-plugins-dist-1.0.4"为你所解压的xdoclet目录。 AntbuildFilebuild.xml
  projectname="HibernateExample"default="about"basedir="."
  !Thelocationwhereyourxdocletjarfilesreside
  propertyname="xdoclet.lib.home"value="c:/java_api/xdoclet-1.2b3/lib"/
  targetname="clean"depends="init"description="removesalldirectories
  relatedtothisbuild"
  deletedir="${dist}"/
  /target
  targetname="init"description="Initializespropertiesthatareusedby
  othertargets."
  propertyname="dist"value="dist"/
  /target
  targetname="prepare"depends="init,clean"description="createsdistdir
  ectory"
  echomessage="Creatingrequireddirectories..."/
  mkdirdir="${dist}"/
  /target
  targetname="hibernate"depends="prepare"
  description="GeneratesHibernateclassdescriptorfiles."
  taskdefname="hibernatedoclet"classname="xdoclet.
  modules.hibernate.HibernateDocletTask"classpath
  filesetdir="${xdoclet.lib.home}"
  includename="*.jar"/
  /fileset
  /classpath
  /taskdef
  !Executethehibernatedoclettask
  hibernatedoclet
  destdir="."
  excludedtags="@version,@author,@todo"
  force="true"
  verbose="true"
  mergedir="${dist}"
  filesetdir="."
  includename="**/dbdemo/*.java"/
  /fileset
  hibernateversion="2.0"/
  /hibernatedoclet
  /target
  targetname="about"description="aboutthisbuildfile"depends="init"
  echomessage="Usethisformatforthearguments:"/
  echomessage="anthibernate"/
  echomessage=""/
  /target
  /project
  执行过程: WindowsShowViewOtherAnt文件里面(Ant)在Ant空白处右键 Add Buildfiles选择你要生成配置文件的bulild.xml文件OK,让后分别执行,所要生成的文件即可.赶快试试吧...
  :
广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved