彻底搞定用Xdoclet生成Hibernate所有配置文件

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:
目标:
    利用XDoclet从Java持久化类生成hibernate mapping,hibernate.cfg.xml和mysql数据库脚本。
 
具体步骤:(省略环境配置过程)
 
第一步:先写实体类
 
package com.lavasoft.zfv.domain.zfv.book.entity;
import java.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.mapping default-lazy="false"
 * @hibernate.meta attribute="class-description" value="图书"
 * @hibernate.class table="bk_ts"
 */

public class 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(String translator) {
        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;
    }
}

 
第二步:写xdoclet的ant脚本
 
说明,此脚本有四个目标,执行后的结果分别是:
generate-configuration:生成hibernate.cfg.xml
generate-mapping:生成hibernate实体类映射文件
generate-schema-mysql:生成MySQL数据库脚本
注意:在生成MySQL数据库脚本之前应该先生成生成hibernate实体类映射文件。
 
 
<?xml version="1.0" encoding="gb2312"?>
<project name="xdoclet-hibernate-zfv" default="xdoclet" basedir="../../../../../">
    <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="E:\myserver\zfv-tomcat-5.5.20\common\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="zfvims"/>
    <property name="hibernate.cfg.password" value="leizhimin"/>
    <property name="hibernate.cfg.jdbcurl" value="jdbc:mysql://localhost:3306/zfvims"/>
    <property name="hibernate.cfg.showsql" value="true"/>
    <target name="xdoclet">
        <path id="xdoclet.task.classpath">
            <pathelement path="${tomcat.lib.dir}/*.jar"/>
            <fileset dir="${xdoclet.lib.dir}">
                <include name="**/*.jar"/>
            </fileset>
            <fileset dir="${project.lib.dir}">
                <include name="**/*.jar"/>
                <exclude name="**/hibernate2.jar"/>
            </fileset>
        </path>
        <taskdef name="hibernatedoclet"
            classname="xdoclet.modules.hibernate.HibernateDocletTask"
            classpathref="xdoclet.task.classpath"
        />
    </target>
    <target name="generate-mapping" depends="xdoclet">
        <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>
    <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>
            <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>
  <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.MySQLInnoDBDialect"/>
        <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>
 
第三步:生成的结果如下
 
生成的配置文件
 
<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 2.0//EN" "
[url]http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd[/url] ">
<!-- Generated file - Do not edit! -->
<hibernate-configuration>
 <!-- a SessionFactory instance listed as /jndi/name -->
 <session-factory>
  <!-- properties -->
  <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="show_sql">true</property>
  <property name="use_outer_join">false</property>
   <property name="connection.username">zfvims</property>
   <property name="connection.password">leizhimin</property>
   <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
   <property name="connection.url">jdbc:mysql://localhost:3306/zfvims</property>
  <!-- mapping files -->
  <mapping resource="com/lavasoft/zfv/domain/zfv/book/entity/Book.hbm.xml"/>
  <mapping resource="com/lavasoft/zfv/domain/zfv/book/entity/Reader.hbm.xml"/>
 </session-factory>
</hibernate-configuration>

 
生成的映射文件
 
<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
    "
[url]http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd[/url] ">
<hibernate-mapping
        default-lazy="false"
>
    <class
        name="com.lavasoft.zfv.domain.zfv.book.entity.Book"
        table="bk_ts"
    >
        <meta attribute="class-description">图书</meta>
        <id
            name="id"
            column="id"
            type="java.lang.Long"
        >
            <meta attribute="field-description">标识</meta>
            <generator class="native">
              <!--  
                  To add non XDoclet generator parameters, create a file named 
                  hibernate-generator-params-Book.xml 
                  containing the additional parameters and place it in your merge dir. 
              --> 
            </generator>
        </id>
        <property
            name="code"
            type="string"
            update="true"
            insert="true"
            column="dm"
            length="20"
            not-null="false"
        >
            <meta attribute="field-description">代码</meta>
        </property>
        <property
            name="name"
            type="string"
            update="true"
            insert="true"
            column="mc"
            length="100"
            not-null="true"
        >
            <meta attribute="field-description">名称</meta>
        </property>
        <property
            name="bookman"
            type="string"
            update="true"
            insert="true"
            column="cbs"
            length="24"
            not-null="false"
        >
            <meta attribute="field-description">出版社</meta>
        </property>
        <property
            name="pubDate"
            type="timestamp"
            update="true"
            insert="true"
            column="cbrq"
            not-null="false"
        >
            <meta attribute="field-description">出版日期</meta>
        </property>
        <property
            name="author"
            type="string"
            update="true"
            insert="true"
            column="zz"
            length="24"
            not-null="false"
        >
            <meta attribute="field-description">作者</meta>
        </property>
        <property
            name="translator"
            type="string"
            update="true"
            insert="true"
            column="yz"
            length="24"
            not-null="false"
        >
            <meta attribute="field-description">译者</meta>
        </property>
        <property
            name="price"
            type="big_decimal"
            precision="19"
            scale="6"
            update="true"
            insert="true"
            column="dj"
            not-null="true"
        >
            <meta attribute="field-description">单价</meta>
        </property>
        <property
            name="amount"
            type="int"
            update="true"
            insert="true"
            column="sl"
            not-null="true"
        >
            <meta attribute="field-description">数量</meta>
        </property>
        <property
            name="buyer"
            type="string"
            update="true"
            insert="true"
            column="cgr"
            length="12"
            not-null="false"
        >
            <meta attribute="field-description">采购人</meta>
        </property>
        <property
            name="assessor"
            type="string"
            update="true"
            insert="true"
            column="shr"
            length="12"
            not-null="false"
        >
            <meta attribute="field-description">审核人</meta>
        </property>
        <property
            name="dateMark"
            type="timestamp"
            update="true"
            insert="true"
            column="czsj"
            not-null="true"
        >
            <meta attribute="field-description">操作时间</meta>
        </property>
        <property
            name="operator"
            type="string"
            update="true"
            insert="true"
            column="czy"
            length="12"
            not-null="true"
        >
            <meta attribute="field-description">操作员</meta>
        </property>
        <property
            name="sort"
            type="string"
            update="true"
            insert="true"
            column="lb"
            length="12"
            not-null="true"
        >
            <meta attribute="field-description">类别</meta>
        </property>
        <property
            name="summary"
            type="string"
            update="true"
            insert="true"
            column="zy"
            length="600"
            not-null="false"
        >
            <meta attribute="field-description">摘要</meta>
        </property>
        <property
            name="remark"
            type="string"
            update="true"
            insert="true"
            column="bz"
            length="200"
            not-null="false"
        >
            <meta attribute="field-description">备注</meta>
        </property>
        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-Book.xml
            containing the additional properties and place it in your merge dir.
        -->
    </class>
</hibernate-mapping>

 
生成的数据库脚本
 
    drop table if exists bk_ts;
    create table bk_ts (
        id bigint not null auto_increment comment '标识',
        dm varchar(20) comment '代码',
        mc varchar(100) not null comment '名称',
        cbs varchar(24) comment '出版社',
        cbrq datetime comment '出版日期',
        zz varchar(24) comment '作者',
        yz varchar(24) comment '译者',
        dj numeric(19,6) not null comment '单价',
        sl integer not null comment '数量',
        cgr varchar(12) comment '采购人',
        shr varchar(12) comment '审核人',
        czsj datetime not null comment '操作时间',
        czy varchar(12) not null comment '操作员',
        lb varchar(12) not null comment '类别',
        zy text comment '摘要',
        bz varchar(200) comment '备注',
        primary key (id)
    ) comment='图书' type=InnoDB;

 
概括结合Xdoclet来做hibernate应用会给你带来非常高效率和价值,但是配置脚本和实体标签的写法不容易掌握,需要大量实践才可以。这个例子虽然是针对MySQL的,可是稍作修改(数据库方言),就可以用于Oracle、DB2、Sybase下的Hibernate应用,你可以稍作修改将此脚本植入你的项目中,非常的方便。欢迎和我再次交流!


本文转自 leizhimin 51CTO博客,原文链接:http://blog.51cto.com/lavasoft/12545,如需转载请自行联系原作者
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
4月前
|
SQL Java 关系型数据库
Hibernate - hibernate.cfg.xml配置文件详解
Hibernate 配置文件主要用于配置数据库连接和 Hibernate 运行时所需的各种属性。每个 Hibernate 配置文件对应一个 Configuration 对象–这样说不太正确,应该是项目初始化时会实例化Configuration对象,该实例对象(Configuration对象)会读取配置文件配置的属性值为实例对象赋值。
82 0
|
8月前
|
SQL Java 数据库连接
Hibernate常见配置
Hibernate常见配置
48 0
|
SQL Java 数据库连接
Mybaties(二) MyBatis配置文件详解
详细介绍mybaties中mapper.xml中各标签的用法
Mybaties(二)  MyBatis配置文件详解
|
XML Java 数据库连接
|
SQL Oracle 前端开发
Mybatis【配置文件】(二)
配置文件和映射文件还有挺多的属性我还没有讲的,现在就把它们一一补全 在mapper.xml文件中配置很多的sql语句,执行每个sql语句时,封装为MappedStatement对象,mapper.xml以statement为单位管理sql语句 Statement的实际位置就等于namespace+StatementId
143 0
Mybatis【配置文件】(二)
|
SQL Oracle Java
Mybatis【配置文件】(一)
配置文件和映射文件还有挺多的属性我还没有讲的,现在就把它们一一补全 在mapper.xml文件中配置很多的sql语句,执行每个sql语句时,封装为MappedStatement对象,mapper.xml以statement为单位管理sql语句 Statement的实际位置就等于namespace+StatementId
106 0
Mybatis【配置文件】(一)
|
XML Oracle Java
Hibernate中hibernate.cfg.xml文件和Xxx.hbm.xml文件的详细解释(二)上
Hibernate中hibernate.cfg.xml文件和Xxx.hbm.xml文件的详细解释(二)
206 0
Hibernate中hibernate.cfg.xml文件和Xxx.hbm.xml文件的详细解释(二)上
|
SQL XML 安全
Hibernate中hibernate.cfg.xml文件和Xxx.hbm.xml文件的详细解释(二)下
Hibernate中hibernate.cfg.xml文件和Xxx.hbm.xml文件的详细解释(二)
134 0
Hibernate中hibernate.cfg.xml文件和Xxx.hbm.xml文件的详细解释(二)下
|
XML SQL Oracle
Hibernate.cfg.xml配置分析讲解
学习Hibernate时,经常会遇到Hibernate.cfg.xml配置问题,这里将介绍Hibernate.cfg.xml配置问题的解决方法。 Hibernate的描述文件可以是一个properties属性文件,也可以是一个xml文件。下面讲一下Hibernate.cfg.xml配置。配置格式如下:
157 0
|
SQL 关系型数据库 Java
Hibernate properties文件
1 ###################### 2 ### Query Language ### 3 ###################### 4 5 ## define query language constants / function names 6 7 hibernate.
1163 0

热门文章

最新文章