使用xdoclet快速开发hibernate

在我用SSH+velocity开发过程中,hibernate和velocity是我觉得最好的工具.最后看了一些关于ant和xdoclet和
上网查了一些资料,参考了一些资料后整理了一下.以后每次要创建一张新的表,只要创建一个新的pojo类,然后
用ant运行,就可以得到hbm.xml文件的sql的dll语句,比我之前从database->hbm.xml->pojo方便多了,也快了
很多.如果平时要用半小时创建好一张表和pojo的话,我现在用五分钟就可以了.并且维护起来也方便多了.
1,所需要的包:
xdoclet-hibernate-module-1.2.3.jar
xdoclet-1.2.3.jar
xjavadoc-1.1.jar
xdoclet-xdoclet-module-1.2.3.jar
hibernate3.jar
等等.一些相关的包都可以我在我提供的链接下载.

2,在工程的根目录创建一个hibernate-ant目录,然后创建hibernate-ant.build.xml(名字随便可以了.)
内容如下:以下的内容包含了一些简单的注释了.

<? xml version="1.0" encoding="utf-8" ?>
< project  name ="利用工具开发Hibernate"  default ="help"  basedir ="." >

    
<!--  ******  环境设置,可以根据自己的实际配置自行更改 *****  -->
    
<!--  **********************************************  -->
    
<!--  ***********        使用方法         *************  -->
    
<!--  **********************************************  -->
    
<!--
        将本目录(hibernate-ant目录,包括lib目录)放进项目下的根目录(与src,bin同一级) 
        然后修改本配置文件以便合适所需项目(一般配置 pojo.package.name即可)
        注:如果需要在数据库操作则要修改hibernate.cfg.xml中的connection.url属性.
        
        然后用ant工具 运行 所需的target
        1,target generate-hbm 生成hbm.java 文件
        2,target schemaexport 生成sql dll 文件
    
-->

    
<!--  源文件目录, 可以通过 项目->属性->Java构建路径 更改  -->
    
< property  name ="src.dir"  value ="../src"   />
    
<!--  输出的class文件目录,可以通过 项目->属性->Java构建路径 更改  -->
    
< property  name ="class.dir"  value ="../bin"   />
    
<!--  库文件目录   -->
    
< property  name ="lib.dir"  value ="./hibernate-lib"   />
    
<!--  hbm2dll的文件名  -->
    
< property  name ="database.file.name"  value ="schema-export.sql"   />
    
<!--  保存hbm2dll生成的文件的目录  -->
    
< property  name ="database.dir"  value ="database"   />
    
<!--  定义存放POJO的包名 一般为po,bo,pojo,do,database... 等等 一般修改本属性即可使用 -->
    
< property  name ="pojo.package.name"  value ="database"   />
    
<!--  是否只输出文本.(是否不进行数据库操作)  true or false -->
    
< property  name ="just.text"  value ="true"   />


    
<!--  定义类路径  -->
    
< path  id ="project.class.path" >
        
< fileset  dir ="${lib.dir}" >
            
< include  name ="*.jar"   />
        
</ fileset >
        
< pathelement  location ="${class.dir}"   />
    
</ path >

    
< target  name ="help" >
        
< echo  message ="利用工具开发Hibernate"   />
        
< echo  message ="-----------------------------------"   />
        
< echo  message =""   />
        
< echo  message ="提供以下任务:"   />
        
< echo  message =""   />
        
< echo  message ="generate-hbm     --> 运行HibernateDoclet,生成 Hibernate 类的映射文件"   />
        
< echo  message ="schemaexport     --> 运行SchemaExport,利用 hbm.xml 文件生成数据表"   />
        
< echo  message =""   />
    
</ target >


    
<!--  **************************************************************  -->
    
<!--  HibernateDoclet 任务  -->
    
<!--  **************************************************************  -->
    
< target  name ="generate-hbm" >
        
< echo  message ="运行HibernateDoclet,生成 Hibernate 类的映射文件"   />

        
< taskdef  name ="hibernatedoclet"  classname ="xdoclet.modules.hibernate.HibernateDocletTask"  classpathref ="project.class.path" >
        
</ taskdef >
        
<!--
            destdir         输出目录;
            force,          每次都强行执行,覆盖原有文件;
        
-->
        
< hibernatedoclet  destdir ="${src.dir}"  excludedtags ="@version,@author,@todo"  force ="true"  encoding ="GBK"  verbose ="true" >

            
< fileset  dir ="${src.dir}" >
                
< include  name ="**/${pojo.package.name}/*.java"   />
            
</ fileset >

            
< hibernate  version ="3.0"  xmlencoding ="utf-8"   />
        
</ hibernatedoclet >
    
</ target >


    
<!--  **************************************************************  -->
    
<!--  SchemaExport 任务  -->
    
<!--  **************************************************************  -->
    
< target  name ="schemaexport" >
        
< echo  message ="运行SchemaExport,利用 hbm.xml 文件生成数据表"   />

        
< taskdef  name ="schemaexport"  classname ="org.hibernate.tool.hbm2ddl.SchemaExportTask"  classpathref ="project.class.path" >
        
</ taskdef >
        
< mkdir  dir ="../${database.dir}"   />
        
<!--
            quiet=true                       不要把脚本输出到stdout;
            drop=true                        只进行drop tables的步骤 ;
            text=true                        不执行在数据库中运行的步骤 ;
            output=my_schema.ddl             把输出的ddl脚本输出到一个文件 ;
            config=hibernate.cfg.xml         从XML文件读入Hibernate配置 ;
            properties=hibernate.properties  从文件读入数据库属性 ;
            delimiter=";"                    为脚本设置行结束符
        
-->
        
< schemaexport  config ="hibernate.cfg.xml"  quiet ="no"  text ="${just.text}"  drop ="no"  output ="../${database.dir}/${database.file.name}"  delimiter =";" >
            
< fileset  dir ="${src.dir}" >
                
< include  name ="**/*.hbm.xml"   />
            
</ fileset >
        
</ schemaexport >
    
</ target >

</ project >
 
3,要创建一个hibernate.cfg.xml,该文件与hibernate-ant.build.xml同一级.
内容如下:

<? xml version="1.0" encoding="gb2312" ?>
<! DOCTYPE hibernate-configuration PUBLIC 
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"
>
< hibernate-configuration >
    
<!--  SessionFactory 配置  -->
    
< session-factory >
        
< property  name ="connection.url" >
            jdbc:mysql://localhost/test?useUnicode=true
&amp; characterEncoding=utf8
        
</ property >
        
< property  name ="connection.username" > root </ property >
        
< property  name ="connection.password" > root </ property >
        
< property  name ="connection.driver_class" >
            com.mysql.jdbc.Driver
        
</ property >

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

    
</ session-factory >
</ hibernate-configuration >

如果是使用oracle的话可以将MySQLDialect更新改oracle的方言.
这个文件有两个作用:
1),当如果要根据hbm.xml生成dll语句后并同时要数据库要执行dll,这样就可以根据该文件提供的链接对数据库
进行操作
2),是根据方言生成不同的DLL.

3, 将相关的lib放到/hibernate-ant/hibernate-lib中.

4,写带有xdoclet的pojo类,如下:

package  spring.test.daotest.database;

import  java.util.Date;

/**
 * @hibernate.class table="user"
 
*/

public   class  User  implements  java.io.Serializable  {
    
private static final long serialVersionUID = 1L;

    
private Long id;
    
private String name;
    
private Integer age;
    
private Date birthday;

    
/**
     * @hibernate.property length="19" not-null="false"
     * 
@return
     
*/

    
public Date getBirthday() {
        
return birthday;
    }


    
public void setBirthday(Date birthday) {
        
this.birthday = birthday;
    }


    
/** default constructor */
    
public User() {
    }


    
// Property accessors

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

    
public Long getId() {
        
return this.id;
    }


    
public void setId(Long id) {
        
this.id = id;
    }


    
/**
     * @hibernate.property length="100" column="name"
     
*/

    
public String getName() {
        
return this.name;
    }


    
public void setName(String name) {
        
this.name = name;
    }


    
/**
     * @hibernate.property length="11" column="age"
     
*/

    
public Integer getAge() {
        
return this.age;
    }


    
public void setAge(Integer age) {
        
this.age = age;
    }


}

5,然后在eclipse用ant运行"generate-hbm" 目标 ,运行后,与database/XX.java同目录中将产生hbm.xml文件.
上面所写的类将在spring.test.daotest.database下生成hbm.xml文件,如下:

<? xml version="1.0" encoding="utf-8" ?>

<! DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>

< hibernate-mapping
>
    
< class
        
name ="spring.test.daotest.database.User"
        table
="user"
    
>

        
< id
            
name ="id"
            column
="id"
            type
="java.lang.Long"
        
>
            
< generator  class ="native" >
              
<!--   
                  To add non XDoclet generator parameters, create a file named 
                  hibernate-generator-params-User.xml 
                  containing the additional parameters and place it in your merge dir. 
              
-->  
            
</ generator >
        
</ id >

        
< property
            
name ="birthday"
            type
="java.util.Date"
            update
="true"
            insert
="true"
            column
="birthday"
            length
="19"
            not-null
="false"
        
/>

        
< property
            
name ="name"
            type
="java.lang.String"
            update
="true"
            insert
="true"
            column
="name"
            length
="100"
        
/>

        
< property
            
name ="age"
            type
="java.lang.Integer"
            update
="true"
            insert
="true"
            column
="age"
            length
="11"
        
/>

        
<!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-User.xml
            containing the additional properties and place it in your merge dir.
        
-->

    
</ class >

</ hibernate-mapping >

6,运行  "schemaexport" 目标 .运行该目标 后.将会在项目根目录生成database目录,并生成schema-export.sql文件,文件内容如下:

drop   table   if   exists   user ;
create   table   user  (id  bigint   not   null  auto_increment, birthday  datetime , name  varchar ( 100 ), age  integer primary   key  (id));

附件:
http://ronaldchan2005.googlepages.com/hibernate-ant.zip


附件的使用:

1,解压附件后,将hibernate-ant目录copy到工程的根目录,与bin,src同级
2,修改<property name="pojo.package.name" value="database" />这里的database,注释有说明.
3,如果不是使用是Mysql的话,修改hibernate.cfg.xml的方言,并将jdbc驱动放到hibernate-lib中(mysql的话不用了.
附件中已包含)
4,如果要自动在数据库中执行DLL,要修改hibernate.cfg.xml的
connectionURL和数据库的用户密码等.
jdbc:mysql://localhost/test?useUnicode=true&amp;characterEncoding=utf8
5,在eclipse用的ant工具运行目标:generate-hbm,schemaexport.这样就完成了hbm.xml,DLL的创建.

总结:
用了ant和xdoclet确实是加快了我们开发的速度,同理一样可以对struts和spring的配置文件进行自动配置.
ronaldchan2005(at)gmail.com
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值