Ant+xdoclet管理hibernate配置文件

               平时开发数据库软件时,总是先用powerdesigner设计好表,生成数据库表,按照表写相应的javabean,写相应的hibernate映射文件,写相应的hibernate配置文件,过程比较复杂和乏味,而使用xdoclet工具之后,这些文件基本可以自动生成,大大的提高了开发效率。下面来简单地介绍一下ant+xdoclet怎样管理hibernate的配置文件。

1.      下载myeclipse , xdoclet jar包,hibernate jar包

2.      新建项目,新建lib目录,将hibernate和xdoclet所需要的jar包放到工程的lib目录下。

3.      新建Book类,代码如下:

packagecom.lavasoft.zfv.domain.zfv.book.entity;

 

importjava.util.Date;

 

/**

 * File Name:  Book.java

 * Created by: IntelliJ IDEA.

 * Copyright:  Copyright (c) 2003-2006

 * Author:     leizhimin

 * Modifier:   leizhimin

 * Date Time:  2006-12-16 12:54:50

 * Readme:     图书

 */

/**

 * 图书

 *

 * @hibernate.mappingdefault-lazy="false"

 * @hibernate.metaattribute="class-description" value="图书"

 * @hibernate.class table="bk_ts"

 */

publicclass Book {

      private Long id; // 标识

      private String code; // 代码

      private String name; // 名称*

      private String bookman; // 出版社

      private Date pubDate; // 出版日期

      private String author; // 作者

      private String translator; // 译者

      private Double price; // 单价*

      private int amount; // 采购数量*

      private String buyer; // 采购人

      private String assessor; // 审核人

      private Date dateMark; // 操作时间(登记日期)*

      private String operator; // 操作员(录入人)*

      private String sort; // 类别(XXYYZZ格式,对应大中细类别)*

      private String summary; // 摘要(内容简介)

      private String remark; // 备注

 

      public Book() {

      }

 

      /**

       *@hibernate.id generator-class="native" column="id"

       *@hibernate.meta attribute="field-description" value="标识"

       */

      public Long getId() {

               return id;

      }

 

      public void setId(Long id) {

               this.id = id;

      }

 

      /**

       *@hibernate.property column="dm" type="string"length="20"

       *                     not-null="false"

       *@hibernate.meta attribute="field-description" value="代码"

       */

      public String getCode() {

               return code;

      }

 

      public void setCode(String code) {

               this.code = code;

      }

 

      /**

       *@hibernate.property column="mc" type="string"length="100"

       *                     not-null="true"

       *@hibernate.meta attribute="field-description" value="名称"

       */

      public String getName() {

               return name;

      }

 

      public void setName(String name) {

               this.name = name;

      }

 

      /**

       *@hibernate.property column="cbs" type="string"length="24"

       *                     not-null="false"

       *@hibernate.meta attribute="field-description" value="出版社"

       */

      public String getBookman() {

               return bookman;

      }

 

      public void setBookman(String bookman) {

               this.bookman = bookman;

      }

 

      /**

       *@hibernate.property column="cbrq" type="timestamp"not-null="false"

       *@hibernate.meta attribute="field-description" value="出版日期"

       */

      public Date getPubDate() {

               return pubDate;

      }

 

      public void setPubDate(Date pubDate) {

               this.pubDate = pubDate;

      }

 

      /**

       *@hibernate.property column="zz" type="string"length="24"

       *                     not-null="false"

       *@hibernate.meta attribute="field-description" value="作者"

       */

      public String getAuthor() {

               return author;

      }

 

      public void setAuthor(String author) {

               this.author = author;

      }

 

      /**

       *@hibernate.property column="yz" type="string"length="24"

       *                     not-null="false"

       *@hibernate.meta attribute="field-description" value="译者"

       */

      public String getTranslator() {

               return translator;

      }

 

      public void setTranslator(Stringtranslator) {

               this.translator = translator;

      }

 

      /**

       *@hibernate.property column="dj" type="big_decimal"precision="19"

       *                     scale="6"not-null="true"

       *@hibernate.meta attribute="field-description" value="单价"

       */

      public Double getPrice() {

               return price;

      }

 

      public void setPrice(Double price) {

               this.price = price;

      }

 

      /**

       *@hibernate.property column="sl" type="int"not-null="true"

       *@hibernate.meta attribute="field-description" value="数量"

       */

      public int getAmount() {

               return amount;

      }

 

      public void setAmount(int amount) {

               this.amount = amount;

      }

 

      /**

       *@hibernate.property column="cgr" type="string"length="12"

       *                     not-null="false"

       *@hibernate.meta attribute="field-description" value="采购人"

       */

      public String getBuyer() {

               return buyer;

      }

 

      public void setBuyer(String buyer) {

               this.buyer = buyer;

      }

 

      /**

       *@hibernate.property column="shr" type="string"length="12"

       *                     not-null="false"

       *@hibernate.meta attribute="field-description" value="审核人"

       */

      public String getAssessor() {

               return assessor;

      }

 

      public void setAssessor(String assessor) {

               this.assessor = assessor;

      }

 

      /**

       *@hibernate.property column="czsj" type="timestamp"not-null="true"

       *@hibernate.meta attribute="field-description" value="操作时间"

       */

      public Date getDateMark() {

               return dateMark;

      }

 

      public void setDateMark(Date dateMark) {

               this.dateMark = dateMark;

      }

 

      /**

       *@hibernate.property column="czy" type="string"length="12"

       *                     not-null="true"

       *@hibernate.meta attribute="field-description" value="操作员"

       */

      public String getOperator() {

               return operator;

      }

 

      public void setOperator(String operator) {

               this.operator = operator;

      }

 

      /**

       *@hibernate.property column="lb" type="string"length="12" not-null="true"

       *@hibernate.meta attribute="field-description" value="类别"

       */

      public String getSort() {

               return sort;

      }

 

      public void setSort(String sort) {

               this.sort = sort;

      }

 

      /**

       *@hibernate.property column="zy" type="string"length="600"

       *                     not-null="false"

       *@hibernate.meta attribute="field-description" value="摘要"

       */

      public String getSummary() {

               return summary;

      }

 

      public void setSummary(String summary) {

               this.summary = summary;

      }

 

      /**

       *@hibernate.property column="bz" type="string"length="200"

       *                     not-null="false"

       *@hibernate.meta attribute="field-description" value="备注"

       */

      public String getRemark() {

               return remark;

      }

 

      public void setRemark(String remark) {

               this.remark = remark;

      }

}

4.      新建build.xml文件:

<?xml version="1.0"encoding="gb2312"?>

<!--name project的名称,可以随意取   

   default为要执行的target名称(如果要想生成 mapping文件,则将default的值改为generate-mapping

                                                                     如果要想生成hibernate配置文件,则将default的值改为generate-configuration

                                                                     如果要想生成sql语句,则将default的值改为generate-schema-mysql                                                          

     如果不写basedir,则默认的basedir为工程的根目录

-->

<project name="xdoclet" default="generate-schema-mysql">

   <!-- 相当于定义全局变量,例如xdoclet.lib.dir就表示工程目录下的lib目录  -->

   <property name="xdoclet.lib.dir" value="${basedir}/lib" />

   <property name="project.lib.dir" value="${basedir}/lib" />

   <property name="project.src.dir" value="${basedir}/src" />

   <property name="project.resources.dir" value="${basedir}/doc/dbscript" />

   <property name="tomcat.lib.dir" value="F:/apache-tomcat-6.0/apache-tomcat-6.0.35/lib"/>

   <property name="hibernate.cfg.dialect" value="org.hibernate.dialect.MySQLDialect"/>

   <property name="hibernate.cfg.driver" value="com.mysql.jdbc.Driver" />

   <property name="hibernate.cfg.username" value="root" />

   <property name="hibernate.cfg.password" value="root" />

   <property name="hibernate.cfg.jdbcurl" value="jdbc:mysql://localhost:3306/hibernate"/>

   <property name="hibernate.cfg.showsql" value="true" />

  

   <target name="xdoclet">

     <!-- 定义路径 -->

     <path id="xdoclet.task.classpath">

        <!-- ${tomcat.lib.dir}就是前面定义的全局变量,即指  F:/apache-tomcat-6.0/apache-tomcat-6.0.35/lib下的所有jar     -->

        <pathelement path="${tomcat.lib.dir}/*.jar" />

        <!-- fileset 为文件的过滤器,这个指的是${basedir}/lib下的所有jar     -->

        <fileset dir="${xdoclet.lib.dir}">

          <include name="**/*.jar" />

        </fileset>

        <!-- fileset 为文件的过滤器,这个指的是${basedir}/lib下除了hibernate3.jar之外的所有jar包,include指包括,exclude指除去   -->

        <fileset dir="${project.lib.dir}">

          <include name="**/*.jar" />

          <exclude name="**/hibernate3.jar" />

        </fileset>

     </path>

     <!-- 引进xdoclet.task.classpath下所有的包,实例化xdoclet.modules.hibernate.HibernateDocletTask  -->

     <taskdef name="hibernatedoclet" classname="xdoclet.modules.hibernate.HibernateDocletTask"classpathref="xdoclet.task.classpath"/>

   </target>

  

   <!--生成 hibernate映射文件,在执行generate-mapping target之前要执行xdoclet target -->

   <target name="generate-mapping" depends="xdoclet">

     <!-- 将工程目录下的src目录下的任意目录下的domain目录下的任意.java文件按规则生成hbm.xml文件   -->

     <hibernatedoclet destdir="${project.src.dir}" verbose="true" force="false">

        <fileset dir="${project.src.dir}">

          <include name="**/domain/**/*.java" />

        </fileset>

        <hibernate version="3.0" xmlencoding="gb2312"/>

     </hibernatedoclet>

   </target>

  

   <!--生成hibernate的配置文件,在执行generate-configuration target之前要执行xdoclet target -->

   <target name="generate-configuration" depends="xdoclet">

     <hibernatedoclet destdir="${project.src.dir}" verbose="true" force="true">

        <fileset dir="${project.src.dir}">

          <include name="**/domain/**/*.java" />

        </fileset>

        <!-- 生成hibernate.cfg.xml${basedir}/doc/dbscript目录下 -->

        <hibernatecfg destinationFile="hibernate.cfg.xml" dialect="${hibernate.cfg.dialect}" driver="${hibernate.cfg.driver}" username="${hibernate.cfg.username}" password="${hibernate.cfg.password}" jdbcurl="${hibernate.cfg.jdbcurl}" showsql="${hibernate.cfg.showsql}" destdir="${project.resources.dir}" xmlencoding="gb2312" />

     </hibernatedoclet>

   </target>

  

   <!--生成hibernate表中的sql语句,在执行generate-schema-mysql target之前要执行xdoclet target   -->

   <target name="generate-schema-mysql" depends="xdoclet">

     <taskdef name="schemaexport" classname="org.hibernate.tool.hbm2ddl.SchemaExportTask"classpathref="xdoclet.task.classpath"/>

     <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>

     <property name="hibernate.format_sql" value="true" />

     <property name="hibernate.use_sql_comments true" value="true" />

     <schemaexport quiet="no" text="yes"drop="no" delimiter=";" output="${project.resources.dir}/zfvims-mysql-schema.sql">

        <fileset dir="${project.src.dir}">

          <include name="**/domain/**/*.hbm.xml" />

        </fileset>

     </schemaexport>

   </target>

 

</project>

将project元素下的default属性改为generate-mapping,运行build.xml文件,会生成hibernate映射文件把project元素下的default属性改为generate-configuration,运行build.xml文件,会生成hibernate配置文件,把project元素下的default属性改为generate-schema-mysql,运行build.xml文件,会生成数据库表的sql语句。

此时工程的目录为:

 

  

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值