做Hibernate应用的开发,肯定会设计到pojo的映射文件的编写,当设计到大量的pojo时,维护映射文件变成了一件相当困难的事情,因为要保持pojo和映射文件的同步,很可能就会出错. 如果采用xdoclet那么就可以很方便的保持它们之间的同步,进一步提高工作效率.
这里举一个简单的使用xdoclet生成映射文件的例子:
首先需要使用的包:
1.xdoclet-1.2.3.jar /xjavadoc.jar /xdoclet-hibernate-module-1.2.3.jar
2.ant相关的包
首先,建立ant配置文件(一旦建立,之后就可以在项目中不用更改了)
<?
xml version="1.0" encoding="ISO-8859-1"
?>
< project name ="XDoclet Examples" default ="compile" basedir ="." >
< property file ="config.properties" />
<!-- =================================================================== -->
<!-- Define the class path -->
<!-- =================================================================== -->
< 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 >
<!-- =================================================================== -->
<!-- Invoke XDoclet's hibernate -->
<!-- =================================================================== -->
< target name ="hibernate" depends ="init" description ="Generate mapping documents (run jar first)" >
< echo > +---------------------------------------------------+ </ echo >
< echo > | | </ echo >
< echo > | R U N N I N G H I B E R N A T E D O C L E T | </ echo >
< echo > | | </ echo >
< echo > +---------------------------------------------------+ </ echo >
<! -这里也是比较关键的地方- >
< hibernatedoclet
destdir ="${basedir}"
excludedtags ="@version,@author,@todo,@see"
addedtags ="@xdoclet-generated at ${TODAY},@copyright The XDoclet Team,@author XDoclet,@version ${version}"
force ="true"
verbose ="false" >
< fileset dir ="${basedir}" >
< include name ="kkvo.java" />
</ fileset >
< hibernate version ="3.0" />
</ hibernatedoclet >
</ target >
<!-- =================================================================== -->
<!-- Compiles all the classes -->
<!-- =================================================================== -->
< target name ="compile" depends ="hibernate" >
< echo > +---------------------------------------------------+ </ echo >
< echo > | | </ echo >
< echo > | C O M P I L I N G S O U R C E S | </ 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 ="XDoclet Examples" default ="compile" basedir ="." >
< property file ="config.properties" />
<!-- =================================================================== -->
<!-- Define the class path -->
<!-- =================================================================== -->
< 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 >
<!-- =================================================================== -->
<!-- Invoke XDoclet's hibernate -->
<!-- =================================================================== -->
< target name ="hibernate" depends ="init" description ="Generate mapping documents (run jar first)" >
< echo > +---------------------------------------------------+ </ echo >
< echo > | | </ echo >
< echo > | R U N N I N G H I B E R N A T E D O C L E T | </ echo >
< echo > | | </ echo >
< echo > +---------------------------------------------------+ </ echo >
<! -这里也是比较关键的地方- >
< hibernatedoclet
destdir ="${basedir}"
excludedtags ="@version,@author,@todo,@see"
addedtags ="@xdoclet-generated at ${TODAY},@copyright The XDoclet Team,@author XDoclet,@version ${version}"
force ="true"
verbose ="false" >
< fileset dir ="${basedir}" >
< include name ="kkvo.java" />
</ fileset >
< hibernate version ="3.0" />
</ hibernatedoclet >
</ target >
<!-- =================================================================== -->
<!-- Compiles all the classes -->
<!-- =================================================================== -->
< target name ="compile" depends ="hibernate" >
< echo > +---------------------------------------------------+ </ echo >
< echo > | | </ echo >
< echo > | C O M P I L I N G S O U R C E S | </ 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.class table="testTable"
* dynamic-insert="true"
* dynamic-update="true"
*
*/
public class kkvo ... {
Integer id;
String name;
/** *//**
* @hibernate.id generator-class="native"
* column="id"
* type="int"
*
*
* @return the id
*/
public Integer getId() ...{
return id;
}
/** *//**
* @param id
* the id to set
*/
public void setId(Integer id) ...{
this.id = id;
}
/** *//**
* @hibernate.property column="name"
* type="integer"
* not-null="true"
* @return the name
*/
public String getName() ...{
return name;
}
/** *//**
* @param name
* the name to set
*/
public void setName(String name) ...{
this.name = name;
}
}
* @author Administrator
* @hibernate.class table="testTable"
* dynamic-insert="true"
* dynamic-update="true"
*
*/
public class kkvo ... {
Integer id;
String name;
/** *//**
* @hibernate.id generator-class="native"
* column="id"
* type="int"
*
*
* @return the id
*/
public Integer getId() ...{
return id;
}
/** *//**
* @param id
* the id to set
*/
public void setId(Integer id) ...{
this.id = id;
}
/** *//**
* @hibernate.property column="name"
* type="integer"
* not-null="true"
* @return the name
*/
public String getName() ...{
return name;
}
/** *//**
* @param name
* the name to set
*/
public void setName(String name) ...{
this.name = name;
}
}
最后,通过ant命令就可以生成该pojo的映射文件,很是方便