随笔-199  评论-203  文章-11  trackbacks-0
//User.java
在企业中model类的属性可能有几百个而且还可能继承了很多属性,这样的model如果手写映射文件岂不是很大的工程!
这里介绍Ant+XDoclet配合来自动生成映射文件。
必备:Ant和XDocle的jar包。
操作很简单,自己写一个model类例如People.java,但是要自动生成映射文件这个类需要有注释,写一个build.xml文件,

  1. 下载Xdoclet,网址:http://xdoclet.sourceforge.net/
  2. 新建包com.test.model,存放实体类Group,User
  3. package dbdemo;
  4. import java.util.Date;
  5. import java.util.Set;
  6. /**
  7.  * @hibernate.class table="Users"
  8.  *
  9.  * @author Ethan
  10.  *
  11.  * Represents a User
  12.  */
  13. public class User {
  14.     private String userID;
  15.     private String userName;
  16.     private String password;
  17.     private String emailAddress;
  18.     private Date lastLogon;
  19.     private Set contacts;
  20.     private Set books;
  21.     private Address address;
  22.     /**
  23.      * @hibernate.property column="EmailAddress" type="string"
  24.      * @return String
  25.      */
  26.     public String getEmailAddress() {
  27.         return emailAddress;
  28.     }
  29.     /**
  30.      * @hibernate.property column="LastLogon" type="date"
  31.      * @return Date
  32.      */
  33.     public Date getLastLogon() {
  34.         return lastLogon;
  35.     }
  36.     /**
  37.      * @hibernate.property column="Password" type="string"
  38.      * @return String
  39.      */
  40.     public String getPassword() {
  41.         return password;
  42.     }
  43.     /**
  44.      * @hibernate.id generator-class="assigned" type="string"
  45.      *                      column="LogonID"
  46.      * @return String
  47.      */
  48.     public String getUserID() {
  49.         return userID;
  50.     }
  51.     /**
  52.      * @hibernate.property column="Name" type="string"
  53.      * @return String
  54.      */
  55.     public String getUserName() {
  56.         return userName;
  57.     }
  58.     /**
  59.      * @param string
  60.      */
  61.     public void setEmailAddress(String string) {
  62.         emailAddress = string;
  63.     }
  64.     /**
  65.      * @param string
  66.      */
  67.     public void setLastLogon(Date date) {
  68.         lastLogon = date;
  69.     }
  70.     /**
  71.      * @param string
  72.      */
  73.     public void setPassword(String string) {
  74.         password = string;
  75.     }
  76.     /**
  77.      * @param string
  78.      */
  79.     public void setUserID(String string) {
  80.         userID = string;
  81.     }
  82.     /**
  83.      * @param string
  84.      */
  85.     public void setUserName(String string) {
  86.         userName = string;
  87.     }
  88.     /**
  89.      * @hibernate.set role="contacts" table="Contacts"
  90.      *                        cascade="all" readonly="true"
  91.      * @hibernate.collection-key column="User_ID"
  92.      * @hibernate.collection-one-to-many class="dbdemo.Contact"
  93.      * @return java.util.Set
  94.      */
  95.     public Set getContacts() {
  96.         return contacts;
  97.     }
  98.     /**
  99.      * @param set
  100.      */
  101.     public void setContacts(Set set) {
  102.         contacts = set;
  103.     }
  104.     /**
  105.      * @hibernate.set role="books" table="Book_User_Link"
  106.      *                            cascade="all" eadonly="true"
  107.      * @hibernate.collection-key column="UserID"
  108.      * @hibernate.collection-many-to-many
  109.      *                            class="dbdemo.Book" column="BookID"
  110.      * @return java.util.Set
  111.      */
  112.     public Set getBooks() {
  113.         return books;
  114.     }
  115.     /**
  116.      * @param set
  117.      */
  118.     public void setBooks(Set set) {
  119.         books = set;
  120.     }
  121.     /**
  122.      * @hibernate.one-to-one class="dbdemo.Address"
  123.      * @return dbdemo.Address
  124.      */
  125.     public Address getAddress() {
  126.         return address;
  127.     }
  128.     /**
  129.      * @param address
  130.      */
  131.     public void setAddress(Address address) {
  132.         this.address = address;
  133.     }
  134. }

//在test目录下建立build.xml,其中<property name="xdoclet.home" value="C:/xdoclet-plugins-dist-1.0.4">为你所解压的xdoclet目录。Ant build File build.xml
  1.  <project name="Hibernate Example" default="about" basedir=".">
  2.  
  3.        <!-- The location where your xdoclet jar files reside -->
  4.  
  5.        <property name="xdoclet.lib.home" value="c:/java_api/xdoclet-1.2b3/lib"/>
  6.  
  7.  
  8.  
  9.        <target name="clean" depends="init" description="removes all directories
  10. related to this build">
  11.  
  12.              <delete dir="${dist}"/>
  13.  
  14.        </target>
  15.  
  16.  
  17.        <target name="init" description="Initializes properties that are used by
  18. other targets.">
  19.              <property name="dist" value="dist"/>
  20.        </target>
  21.  
  22.        <target name="prepare" depends="init,clean" description="creates dist dir
  23. ectory">
  24.              <echo message="Creating required directories..."/>
  25.              <mkdir dir="${dist}"/>
  26.        </target>
  27.  
  28.        <target name="hibernate" depends="prepare"
  29.          description="Generates Hibernate class descriptor files.">
  30.              <taskdef name="hibernatedoclet"                 classname="xdoclet.
  31. modules.hibernate.HibernateDocletTask">                  <classpath>
  32.                    <fileset dir="${xdoclet.lib.home}">
  33.                        <include name="*.jar"/>
  34.                    </fileset>
  35.                  </classpath>
  36.              </taskdef>
  37.  
  38.              <!-- Execute the hibernatedoclet task -->
  39.  
  40.              <hibernatedoclet
  41.                    destdir="."
  42.                    excludedtags="@version,@author,@todo"
  43.                    force="true"
  44.                    verbose="true"
  45.                    mergedir="${dist}">
  46.  
  47.                    <fileset dir=".">
  48.                        <include name="**/dbdemo/*.java"/>
  49.                    </fileset>
  50.  
  51.                    <hibernate version="2.0"/>
  52.  
  53.              </hibernatedoclet>
  54.        </target>
  55.  
  56.        <target name="about" description="about this build file" depends="init">
  57.              <echo message="  Use this format for the arguments:"/>
  58.              <echo message="      ant hibernate"/>
  59.              <echo message=""/>
  60.        </target>
  61.  
  62.  </project>

执行过程:  Windows-->ShowView-->Other-->Ant文件里面(Ant)-->在Ant空白处右键-->Add Buildfiles-->选择你要生成配置文件的bulild.xml文件点击OK,让后分别执行,所要生成的文件即可.赶快试试吧...

 

posted on 2009-08-04 21:44 Werther 阅读(1355) 评论(0)  编辑  收藏 所属分类: 10.Java

只有注册用户登录后才能发表评论。


网站导航: