隨筆-199  評(píng)論-203  文章-11  trackbacks-0
          //User.java
          在企業(yè)中model類(lèi)的屬性可能有幾百個(gè)而且還可能繼承了很多屬性,這樣的model如果手寫(xiě)映射文件豈不是很大的工程!
          這里介紹Ant+XDoclet配合來(lái)自動(dòng)生成映射文件。
          必備:Ant和XDocle的jar包。
          操作很簡(jiǎn)單,自己寫(xiě)一個(gè)model類(lèi)例如People.java,但是要自動(dòng)生成映射文件這個(gè)類(lèi)需要有注釋?zhuān)瑢?xiě)一個(gè)build.xml文件,

          1. 下載Xdoclet,網(wǎng)址:http://xdoclet.sourceforge.net/
          2. 新建包c(diǎn)om.test.model,存放實(shí)體類(lèi)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>

          執(zhí)行過(guò)程:  Windows-->ShowView-->Other-->Ant文件里面(Ant)-->在Ant空白處右鍵-->Add Buildfiles-->選擇你要生成配置文件的bulild.xml文件點(diǎn)擊OK,讓后分別執(zhí)行,所要生成的文件即可.趕快試試吧...

           

          posted on 2009-08-04 21:44 Werther 閱讀(1366) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): 10.Java
          主站蜘蛛池模板: 正镶白旗| 镇原县| 句容市| 鱼台县| 柘荣县| 丰台区| 平阴县| 文山县| 天祝| 吉水县| 远安县| 永昌县| 伊宁县| 横山县| 革吉县| 博乐市| 梁平县| 海阳市| 吉林市| 兴国县| 吉安市| 宜兰县| 巴彦淖尔市| 逊克县| 隆林| 眉山市| 社会| 金堂县| 承德县| 孟州市| 隆子县| 乌兰察布市| 蓝田县| 阿拉善盟| 海丰县| 唐山市| 田林县| 鲁甸县| 托里县| 乌什县| 泾源县|