xdoclet 与 hibernate


这里将利用xdoclet来为hibernate生成相关代码(由Customer.java生成Customer.hbm.xml),看看xdoclet对提供工作效率的。chin a i t p oe er . co mnQtB
运行环境:
1、xdoclet
2、ant
3、hibernate
需要的jar包有www.chinai tp 采集 ow er.comnQ

  • xdoclet-/X.X.X/.jar
  • xdoclet-hibernate-module-/X.X.X/.jar
  • xdoclet-xjavadoc-/X.X.X/.jar
  • xdoclet-xdoclet-module-/X.X.X/.jar
  • log4j-/X.X.X/.jar
  • commons-collections-2.0.jar
  • commons-logging.jar

    前四个可以从xdoclet里的lib里面找到(这里“ /X.X.X/ ” 表示版本号),后面的几个我想大家都熟了吧可以从很多地方找到。chin a i t p oe er . co mnQtB

    程序
    看下文件结构
    .(项目目录)
         |
         -src(文件夹,命名src.dir,存放源文件)
         |
         -lib(文件夹,命名lib.dir,存放上面提到的几个jar文件)
                  |
                  -*.jar
         |
         -bin(文件夹,命名bin.dir)
                 |
                 -src(文件夹,存放生成的*.java文件,这里将不用到)
                 |
                 -classes(文件夹,存放所编译的*.class文件)
       |
       -build.xml
     
    看下源程序吧。
    1、Customer.javachin a i t p oe er . co mnQtB
    /*
     * Created on 2004-12-20
     * @author roson
     */
    package hibernate;
    import java.util.Set;
    import java.util.Collections;
    /**
     * @author roson
     * @since 1.0
     * @version 1.0
     * @hibernate.class tables="customers"
     */
    public class Customer {
        /**This customer's identifier field.
         */
        private long id;
       
        /**This customer's name field.
         */
        private String name;
       
        /**The customer's orders set.
         */
        private Set orders=Collections.EMPTY_SET;
       
        /**The default construtor for Hibernate to instantiate with.
         */
        public Customer() {}
       
        /**The getter method for this Customer's identifier.
         *
         * @hibernate.id generator-class="native"
         */
        public long getId()
        {
            return id;
        }
       
        /**The setter method for this Customer's identifier.
         */
        public void setId(long id)
        {
            this.id=id;
        }
       
        /**The getter method for this Customer's name.
         *
         * @hibernate.property
         */
        public String getName()
        {
            return name;
        }
       
        /**The setter method for this Customer's name.
         */
        public void setName(String name)
        {
            this.name=name;
        }
       
        /**The getter method for this Customer's orders.
         *
         * @hibernate.set role="orders"
         *
         * @hibernate.collection-key column="customer_id"
         *
         * @hibernate.collection-one-to-many class="Order"
         */
        public Set getOrders()
        {
            return orders;
        }
       
        /**The setter method for this Customer's orders.
         */
        public void setOrders(Set orders)
        {
            this.orders=orders;
        }
    }chin a i t p oe er . co mnQtB
    可以看到在源程序中多了一些注释之类的标签,xdoclet就是通过这些标签来识别自动生成xml文件的,除此之外Customer.java与普通的javabean没什么区别。
    注意:这里Customer.java打包为hibernate,如要改动要更改一下build.xml。chin a i t p oe er . co mnQtB
    2、build.xmlchin a i t p oe er . co mnQtB
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <project name="hibernate" default="compile" basedir=".">
     <property name="src.dir" location="src"></property>
     <property name="bin.dir" value="bin"></property>
     <property name="lib.dir" location="lib"></property>
     <path id="lib.path">
     <fileset dir="${lib.dir}">
     <include name="*.jar"/>
     </fileset> 
      <pathelement location="lib/xdoclet-hibernate-module-1.2.1.jar"/>
     </path>
     <target name="init">
      <tstamp>
      <format property="TODAY" pattern="yy-mm-d"/>
      </tstamp>
      <mkdir dir="bin"/>
      <mkdir dir="${bin.dir}/src"/>
      <mkdir dir="${bin.dir}/classes"/>
      <taskdef   name="hibernatedoclet"
         classname="xdoclet.modules.hibernate.HibernateDocletTask"
         classpathref="lib.path"
        />
      
      <taskdef name="ejbdoclet"
        classname="xdoclet.ejb.EjbDocletTask"
        classpathref="lib.path">
      </taskdef>
     </target>www.chinai tp 采集 ow er.comnQ


     <target name="compile" depends="init,hibernate">
     <javac srcdir="${src.dir};${bin.dir}/src" destdir="${bin.dir}/classes">
     <classpath refid="lib.path"></classpath>
     </javac>
     </target>
     www.chinai tp 采集 ow er.comnQ

     <target name="hibernate" depends="init">
      <hibernatedoclet
       destdir="${bin.dir}"
       mergedir="${bin.dir}"
       force="${bin.dir}"
       excludedtags="@version,@author,@todo"
       addedtags="@xdoclet-generated at ${TODAY},@copyright netone,@author roson,@version ${version}"
       verbose="false"
       >
       <fileset dir="${src.dir}">
       <include name="*.java"/>
       </fileset>
       <hibernate version="2.0"/>
      </hibernatedoclet>
     </target>
     
     
     <target name="clean">
     <delete dir="${bin.dir}"></delete>
     </target>
    </project>chin a i t p oe er . co mnQtB
    build.xml文件通过ant来运行。首先 请确保ant能够运行,接着 打开 命令提示行(在运行那里输入cmd),转到上面结构所提的“项目目录”,直接输入  ant    就行。
    注意:
    最让我烦恼的就是 taskdef 部分。原因就是没有把所需要的jar包放进 lib 文件夹。并且要理解的是,它所需的classname="xdoclet.modules.hibernate.HibernateDocletTask"  ,并不仅仅要求含有HibernateDocletTask.class这个文件的包,而是同时需要几个包同时存在。希望大家不要在犯同样的错误。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值