做Hibernate应用的开发,肯定会设计到pojo的映射文件的编写,当设计到大量的pojo时,维护映射文件变成了一件相当困难的事情,因为要保持pojo和映射文件的同步,很可能就会出错. 如果采用xdoclet那么就可以很方便的保持它们之间的同步,进一步提高工作效率.
这里举一个简单的使用xdoclet生成映射文件的例子:
首先需要使用的包:
1.xdoclet-1.2.3.jar /xjavadoc.jar /xdoclet-hibernate-module-1.2.3.jar
2.ant相关的包
首先,建立ant配置文件(一旦建立,之后就可以在项目中不用更改了)
<?
xmlversion="1.0"encoding="ISO-8859-1"
?>
< project name ="XDocletExamples" default ="compile" basedir ="." >
< property file ="config.properties" />
<!-- =================================================================== -->
<!-- Definetheclasspath -->
<!-- =================================================================== -->
< path id ="samples.class.path" >
< fileset dir ="lib.dir" >
< include name ="*.jar" />
</ fileset >
</ path >
<!-- =================================================================== -->
<!-- Initialise -->
<!-- =================================================================== -->
< target name ="init" >
< tstamp >
< format property ="TODAY" pattern ="d-MM-yy" />
</ tstamp >
<! -这里很关键,一般来说除了classpathref会修改之外,其他不用修改- >
< taskdef
name ="hibernatedoclet"
classname ="xdoclet.modules.hibernate.HibernateDocletTask"
classpathref ="samples.class.path"
/>
</ target >
<!-- =================================================================== -->
<!-- InvokeXDoclet'shibernate -->
<!-- =================================================================== -->
< target name ="hibernate" depends ="init" description ="Generatemappingdocuments(runjarfirst)" >
< echo > +---------------------------------------------------+ </ echo >
< echo > || </ echo >
< echo > |RUNNINGHIBERNATEDOCLET| </ echo >
< echo > || </ echo >
< echo > +---------------------------------------------------+ </ echo >
<! -这里也是比较关键的地方- >
< hibernatedoclet
destdir ="${basedir}"
excludedtags ="@version,@author,@todo,@see"
addedtags ="@xdoclet-generatedat${TODAY},@copyrightTheXDocletTeam,@authorXDoclet,@version${version}"
force ="true"
verbose ="false" >
< fileset dir ="${basedir}" >
< include name ="kkvo.java" />
</ fileset >
< hibernate version ="3.0" />
</ hibernatedoclet >
</ target >
<!-- =================================================================== -->
<!-- Compilesalltheclasses -->
<!-- =================================================================== -->
< target name ="compile" depends ="hibernate" >
< echo > +---------------------------------------------------+ </ echo >
< echo > || </ echo >
< echo > |COMPILINGSOURCES| </ echo >
< echo > || </ echo >
< echo > +---------------------------------------------------+ </ echo >
< javac
destdir ="${basedir}"
classpathref ="${basedir}"
debug ="on"
deprecation ="on"
optimize ="off"
>
< src path ="${basedir}" />
</ javac >
</ target >
<!-- =================================================================== -->
<!-- Clean -->
<!-- =================================================================== -->
< target name ="clean" >
< delete dir ="${samples.dist.dir}" />
</ target >
</ project >
< project name ="XDocletExamples" default ="compile" basedir ="." >
< property file ="config.properties" />
<!-- =================================================================== -->
<!-- Definetheclasspath -->
<!-- =================================================================== -->
< path id ="samples.class.path" >
< fileset dir ="lib.dir" >
< include name ="*.jar" />
</ fileset >
</ path >
<!-- =================================================================== -->
<!-- Initialise -->
<!-- =================================================================== -->
< target name ="init" >
< tstamp >
< format property ="TODAY" pattern ="d-MM-yy" />
</ tstamp >
<! -这里很关键,一般来说除了classpathref会修改之外,其他不用修改- >
< taskdef
name ="hibernatedoclet"
classname ="xdoclet.modules.hibernate.HibernateDocletTask"
classpathref ="samples.class.path"
/>
</ target >
<!-- =================================================================== -->
<!-- InvokeXDoclet'shibernate -->
<!-- =================================================================== -->
< target name ="hibernate" depends ="init" description ="Generatemappingdocuments(runjarfirst)" >
< echo > +---------------------------------------------------+ </ echo >
< echo > || </ echo >
< echo > |RUNNINGHIBERNATEDOCLET| </ echo >
< echo > || </ echo >
< echo > +---------------------------------------------------+ </ echo >
<! -这里也是比较关键的地方- >
< hibernatedoclet
destdir ="${basedir}"
excludedtags ="@version,@author,@todo,@see"
addedtags ="@xdoclet-generatedat${TODAY},@copyrightTheXDocletTeam,@authorXDoclet,@version${version}"
force ="true"
verbose ="false" >
< fileset dir ="${basedir}" >
< include name ="kkvo.java" />
</ fileset >
< hibernate version ="3.0" />
</ hibernatedoclet >
</ target >
<!-- =================================================================== -->
<!-- Compilesalltheclasses -->
<!-- =================================================================== -->
< target name ="compile" depends ="hibernate" >
< echo > +---------------------------------------------------+ </ echo >
< echo > || </ echo >
< echo > |COMPILINGSOURCES| </ echo >
< echo > || </ echo >
< echo > +---------------------------------------------------+ </ echo >
< javac
destdir ="${basedir}"
classpathref ="${basedir}"
debug ="on"
deprecation ="on"
optimize ="off"
>
< src path ="${basedir}" />
</ javac >
</ target >
<!-- =================================================================== -->
<!-- Clean -->
<!-- =================================================================== -->
< target name ="clean" >
< delete dir ="${samples.dist.dir}" />
</ target >
</ project >
然后,我就可以建立自己的pojo了:
/**
* @author Administrator
*@hibernate.classtable="testTable"
*dynamic-insert="true"
*dynamic-update="true"
*
*/
public class kkvo {
Integerid;
Stringname;
/**
*@hibernate.idgenerator-class="native"
*column="id"
*type="int"
*
*
* @return theid
*/
public IntegergetId() {
return id;
}
/**
* @param id
*theidtoset
*/
public void setId(Integerid) {
this .id = id;
}
/**
*@hibernate.propertycolumn="name"
*type="integer"
*not-null="true"
* @return thename
*/
public StringgetName() {
return name;
}
/**
* @param name
*thenametoset
*/
public void setName(Stringname) {
this .name = name;
}
}
* @author Administrator
*@hibernate.classtable="testTable"
*dynamic-insert="true"
*dynamic-update="true"
*
*/
public class kkvo {
Integerid;
Stringname;
/**
*@hibernate.idgenerator-class="native"
*column="id"
*type="int"
*
*
* @return theid
*/
public IntegergetId() {
return id;
}
/**
* @param id
*theidtoset
*/
public void setId(Integerid) {
this .id = id;
}
/**
*@hibernate.propertycolumn="name"
*type="integer"
*not-null="true"
* @return thename
*/
public StringgetName() {
return name;
}
/**
* @param name
*thenametoset
*/
public void setName(Stringname) {
this .name = name;
}
}
最后,通过ant命令就可以生成该pojo的映射文件,很是方便