彻底搞定用Xdoclet生成Hibernate所有配置文件
生活随笔
收集整理的這篇文章主要介紹了
彻底搞定用Xdoclet生成Hibernate所有配置文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
背景:
??? 本人在用Xdoclet生成hibernate的配置文件、實體映射文件和sql腳本的時候,每次總要出點問題,今日問題再現,我在網上泡了一個多小時,沒有一篇很完整,很成功的例子。一怒之下,今天誓死也要啃掉這塊硬骨頭!呵呵,經過4個小時的努力,問題搞定了。現在寫出來,以供和各位網友交流。那些小兒科的配置問題不說了,主要是沒時間寫了,關鍵看Xdoclet的ant腳本和實體類的Xdoclet標簽怎么寫,如果你還不熟悉ant,建議你補充一下ant知識再來看本文。
環境描述:
Windows Server 2003 Stand Edition
Java SDK 1.5
Ant-1.6.5
Hibernate 3.2.1ga
MySQL-5.0.27
XDoclet-1.2.3
目標:
??? 利用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應用,你可以稍作修改將此腳本植入你的項目中,非常的方便。歡迎和我再次交流!
?* 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應用,你可以稍作修改將此腳本植入你的項目中,非常的方便。歡迎和我再次交流!
總結
以上是生活随笔為你收集整理的彻底搞定用Xdoclet生成Hibernate所有配置文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: textview 滚动显示.txt
- 下一篇: 使用Xdoclet和Ant构建Hiber