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
    评论
Myeclipse开发struts+hibernate+spring新手入门---环境配置----项目开发示例 Myeclipse开发struts+hibernate+spring小记 开发前准备工作: 1、下载eclipse3.1版本 下载地址: 2、下载Myeclipse插件 下载地址: 3、根据需要下载所需要的数据库驱动。 部分下载地址: 4、下载的eclipse3.1进行解压缩。安装Myeclipse,分为手工安装和自动安装两种安装方法。 具体安装方法如下: 手工安装: 自动安装: 5、安装完成后。启动Myeclipse。 准备开发工作: 步骤: 1、新建项目: New>projects后进入下图 本文主要介绍开发Web Project项目,因此需要选择Web Project,下一步,进入下图: Project Name:为需要建立的项目名称。填写好项目名称后,Finish。新建项目完成。本文在项目命名为test.。 项目建立完成之后,在Myeclipse视图中,Package Explorer中显示如下: test:为项目名称。 Src:为源文件所放路径。 WebRoot:为Web root folder 2、加载Struts 在所建立项目test上右键后,提示如下页面: 在弹出的菜单中,根据图例提示,选择Myeclipse后在弹出的二级菜单中选择Add Struts Capabilities…后提示如下图所示: 在弹出的对话框中,设置如下: Struts config path:默认,一般不需要改变。 Struts specification:选择Struts版本,在此项目中,选择Struts 1.2版本。 Base Package for new classes:设置为空。 取消Install Struts TLDs选项。 其他默认: Finish.后在Package Explorer的Lib包如下: 到此,加载Struts完成。在此需要关注Struts-config.xml文件。 3、加载hibernate: 同加载struts方式类似,选择Myeclipse后在弹出的二级菜单中选择Add Hibernate Capabilities…后提示如下图所示: 可以根据提示,选择Hibernate Specification:本项目选择Hibernate3,其他默认,Next> 默认后直接下一步。在此可以去掉Configuration File Name中的内容。Next> 后去掉下一级页面的Create SessionFactory class选项。然后Finish,Finish完成后,在Lib目录下会提示相关的jar包。 4、加载spring in action in action 同加载struts方式类似,选择Myeclipse后在弹出的二级菜单中选择Add spring in action in action Capabilities…后提示如下图所示: 选择所需要的spring in action in action包,然后默认下一步,直到完成。 到此,Struts+hibernate+spring配置完成。 附加:在项目名称的右键后进入Properties然后进入Java Build Path后查看加载的jar包。 具体项目的开发详细文档请见下一篇文章《struts+hibernate+spring项目开发示例》 Myeclipse平台struts+hibernate+spring项目开发示例 接:Myeclipse开发struts+hibernate+spring新手入门---环境配置 如果您按照上篇文章配置成功,可以进行开发简单的J2EE项目。 开发前准备: 1、新建项目包:如下图: Dao:数据层;service:逻辑层;web:web展现层。 2、建立好三层架构,部署好包名。建立一个jsp文件,取名为:login.jsp 代码如下: <% @ taglib uri = " http://struts.apache.org/tags-bean " prefix = " bean " %> <% @ taglib uri = " http://struts.apache.org/tags-html " prefix = " html " %> <% @ taglib uri = " http://struts.apache.org/tags-logic " prefix = " logic " %> <% @ taglib uri = " http://struts.apache.org/tags-tiles " prefix = " tiles " %> < HTML > < HEAD > < TITLE > test < TITLE > < HEAD > < BODY > < html:form action = " /login " method = " post " > 用户名: < html:text property = " username " size = " 15 " /> < br > 密 码: < html:password property = " password " size = " 15 " />< br > < html:submit property = " submit1 " style = " FONT-WEIGHT:bold;COLOR:red " > 登 录 < html:submit > < html:form > < BODY > < HTML > 3、配置struts-config.xml文件;建立action、formbean; 改配置可以自动生成,使用eclipse进行自动生成,在建立时,如下图,可直接生成对应的action类和formbean类。 1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"> 3 4 <struts-config> 5 <data-sources /> 6 <form-beans> 7 <form-bean name="LoginForm" type="com.test.web.form.LoginForm" /> 8 9 </form-beans> 10 11 <global-exceptions /> 12 <global-forwards /> 13 <action-mappings> 14 <action attribute="LoginForm" input="/login.jsp" name="LoginForm" path="LoginAction" scope="request" type="com.test.web.action.LoginAction" validate="true"> 15 <forward name="faile" path="faile.jsp" /> 16 <forward name="success" path="success.jsp" /> 17 </action> 18 19 </action-mappings> 20 21 <message-resources parameter="ApplicationResources" /> 22 </struts-config> 对应生成的formbean如下代码所示: 1 import javax.servlet.http.HttpServletRequest; 2 import org.apache.struts.action.ActionErrors; 3 import org.apache.struts.action.ActionForm; 4 import org.apache.struts.action.ActionMapping; 5 6 /** 7 * MyEclipse Struts 8 * Creation date: 11-10-2005 9 * 10 * XDoclet definition: 11 * @struts.form name="LoginForm" 12 */ 13 public class LoginForm extends ActionForm { 14 15 /** password property */ 16 private String password; 17 18 /** username property */ 19 private String username; 20 21 22 /** 23 * Method validate 24 * @param mapping

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

单人旅途

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值