用xdoclet生成hibernate映射文件
主要是跟着Eclipse快速上手Hibernate--3. 利用XDoclet开发一步一步走下去,不过也碰到一些问题。
新建项目 HibernateBegin_3
新建包 javamxj.hibernate
新建类 User.java
/** */ /**
* @author javamxj
* @hibernate.class table = "UserTable3"
*/
public class User ... {
private String id;
private String username;
private String password;
/** *//**
* @return Returns the id.
* @hibernate.id column = "id" generator-class = "uuid.hex"
*
*/
public String getId() ...{
return id;
}
/** *//**
* @param id
* The id to set.
*/
public void setId(String id) ...{
this.id = id;
}
/** *//**
* @return Returns the password.
* @hibernate.property column = "password"
*/
public String getPassword() ...{
return password;
}
/** *//**
* @param password
* The password to set.
*/
public void setPassword(String password) ...{
this.password = password;
}
/** *//**
* @return Returns the username.
* @hibernate.property column = "username"
*/
public String getUsername() ...{
return username;
}
/** *//**
* @param username
* The username to set.
*/
public void setUsername(String username) ...{
this.username = username;
}
}
新建配置文件 hibernate.cfg.xml
hibernate.cfg.xml主要是数据库连接部分,根据不同的数据库配置下就可以了。
<! DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
< hibernate-configuration >
< session-factory >
<!-- 是否将运行期生成的SQL输出到日志以供调试 -->
< property name ="show_sql" > true </ property >
<!-- SQL方言,这里设定的是MySQL -->
< property name ="dialect" > net.sf.hibernate.dialect.MySQLDialect </ property >
<!-- JDBC驱动程序 -->
< property name ="connection.driver_class" > com.mysql.jdbc.Driver </ property >
<!-- JDBC URL, "?useUnicode=true&characterEncoding=GBK" 表示使用GBK进行编码 -->
< property name ="connection.url" > jdbc:mysql://localhost:3306/hibernatetest?useUnicode=true & characterEncoding=GBK </ property >
<!-- 数据库用户名 -->
< property name ="connection.username" > root </ property >
<!-- 数据库密码 -->
< property name ="connection.password" > root </ property >
<!-- 指定User的映射文件 -->
< mapping resource ="javamxj/hibernate/User.hbm.xml" />
</ session-factory >
</ hibernate-configuration >
新建 build.xml
build.xml中根据不同情况修改一下 库文件目录 就可以了,其他的都可以不变,然后把build.xml拷到根目录下再使用Ant工具就可以使用了。
< project name ="利用工具开发Hibernate" default ="help" basedir ="." >
<!-- ****** 环境设置,可以根据自己的实际配置自行更改 ***** -->
<!-- 源文件目录, 可以通过 项目->属性->Java构建路径 更改 -->
< property name ="src.dir" value ="./src" />
<!-- 输出的class文件目录,可以通过 项目->属性->Java构建路径 更改 -->
< property name ="class.dir" value ="./bin" />
<!-- 库文件目录 -->
< property name ="lib.dir" value ="D:/java/Hibernate/lib" />
<!-- 定义类路径 -->
< 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-code -->
运行Hbm2Java,利用 hbm.xml 文件生成Java类文件" />
< echo message ="generate-hbm -->
运行HibernateDoclet,生成 Hibernate 类的映射文件" />
< echo message ="schemaexport -->
运行SchemaExport,利用 hbm.xml 文件生成数据表" />
< echo message ="" /> </ target >
<!-- ************************************************************** -->
<!-- Hbm2Java 任务 -->
<!-- ************************************************************** -->
< target name ="generate-code" >
< echo message ="运行 Hbm2Java 任务,
利用 hbm.xml 文件生成Java类文件" />
< taskdef name ="hbm2java"
classname ="net.sf.hibernate.tool.hbm2java.Hbm2JavaTask"
classpathref ="project.class.path" >
</ taskdef >
< hbm2java output ="${src.dir}" >
< fileset dir ="${src.dir}" >
< include name ="**/*.hbm.xml" />
</ fileset >
</ hbm2java >
</ target >
<!-- ************************************************************** -->
<!-- HibernateDoclet 任务 -->
<!-- ************************************************************** -->
< target name ="generate-hbm" >
< echo message ="运行HibernateDoclet,生成 Hibernate 类的映射文件" />
< taskdef name ="hibernatedoclet"
classname ="xdoclet.modules.hibernate.HibernateDocletTask"
classpathref ="project.class.path" >
</ taskdef >
< hibernatedoclet destdir ="${src.dir}"
excludedtags ="@version,@author,@todo" force ="true" encoding ="GBK"
verbose ="true" >
< fileset dir ="${src.dir}" >
< include name ="**/*.java" />
</ fileset >
< hibernate version ="2.0" xmlencoding ="GBK" />
</ hibernatedoclet > </ target >
<!-- ************************************************************** -->
<!-- SchemaExport 任务 -->
<!-- ************************************************************** -->
< target name ="schemaexport" >
< echo message ="运行SchemaExport,利用 hbm.xml 文件生成数据表" />
< taskdef name ="schemaexport"
classname ="net.sf.hibernate.tool.hbm2ddl.SchemaExportTask"
classpathref ="project.class.path" >
</ taskdef >
< schemaexport config ="${src.dir}/hibernate.cfg.xml" quiet ="no"
text ="no" drop ="no" output ="schema-export.sql" >
</ schemaexport >
</ target >
</ project >
3个文件各自放在正确的路径,马上就可以自动生成hibernate映射文件了。
使用ANT工具,双击“generate-hbm”任务,然后按一下“F5”功能键刷新一下包“javamxj.hibernate”,应该可以看到这个包下的“User.hbm.xml”。
生成数据表
需要测试的话,使用 Test.java(Eclipse快速上手Hibernate--3. 利用XDoclet开发)就可以测试下就可以了。
需要注意的是,由于Eclipse快速上手Hibernate--3. 利用XDoclet开发写的时间有点早,其中使用的是Hibernate2,而现在已经有Hibernate3了,由于Hibernate3和Hibernate2其中的文件框架有点不大一样,所以如果使用Hibernate3就要改很多地方。
学习资源
Eclipse快速上手Hibernate--3. 利用XDoclet开发