程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 利用Ant和XDoclet自動產生映射文件例子

利用Ant和XDoclet自動產生映射文件例子

編輯:關於JAVA

//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,讓後分別執行,所要生成的文件即可.趕快試試吧...

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved