1. 创建项目
· 新建一个java项目:hibernatebegin_2,注意选中“创建单独的源文件夹和输出文件夹”,同时添加“用户库”:hibernate。
2. 映射文件user.hbm.xml
· 新建一个包,包名:javamxj.hibernate,然后在此包下新建一个文件,文件名:user.hbm.xml。
user.hbm.xml
<?xml version="1.0" encoding="gbk"?>
<!doctype hibernate-mapping public "-//hibernate/hibernate mapping dtd//en"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="javamxj.hibernate.user" table="usertable2">
<meta attribute="class-description">
运行 hbm2java 任务, 利用 hbm.xml文件生成java类文件
@author javamxj(分享java快乐)
@link blog: htpp://javamxj.mblogger.cn
htpp://blog.csdn.net/javamxj/
</meta>
<id name="id" type="string" unsaved-value="null" length="32" column="id">
<generator class="uuid.hex"/>
</id>
<property name="username" type="string" not-null="true" length="24">
<meta attribute="field-description">@param 用户名</meta>
</property>
<property name="password" type="string" not-null="true" >
<column name="密码" length="24" not-null="true"></column>
</property>
</class>
</hibernate-mapping>
● 与上篇文章中的user.hbm.xml文件比较,可以发现标签上增加了许多额外的设定。
· 这里<meta>标签中的内容将插入到类的javadoc说明去。
· <id>标签使用 uuid.hex 来定义主键的产生算法,uuid算法使用ip地址、jvm的启动时间、系统时间和一个计数值来产生主键。
· <property>标签中的<column>用于生成数据表中的列。
3. 构建文件build.xml
· 将上篇文章中的“hibernate.cfg.xml”配置文件复制到src目录下。
//重新排版,可能格式不太正确
<?xml version='1.0' encoding='GBK'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-2.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">javamxj</property>
<!-- 指定User的映射文件 -->
<mapping resource="javamxj/hibernate/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
· 在项目根目录下建立一个build.xml,这个文件含有四个任务,这里会用到“generate-code”、“schemaexport”两个任务,至于用法可以看注释。要注意环境变量的设置要符合自己的实际配置,比如库文件目录的设置是"d:/java/hibernate/lib",是沿用上篇文章中的设置。
build.xml
<?xml version="1.0" encoding="gbk"?>
<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>


user.java
package javamxj.hibernate;
import java.io.serializable;
import org.apache.commons.lang.builder.tostringbuilder;
/**
*
* 运行 hbm2java 任务, 利用 hbm.xml文件生成java类文件
* @author javamxj(分享java快乐)
* @link blog: htpp://javamxj.mblogger.cn
* htpp://blog.csdn.net/javamxj/
*
*/
public class user implements serializable {
/** identifier field */
private string id;
/** persistent field */
private string username;
/** persistent field */
private string password;
/** full constructor */
public user(string username, string password) {
this.username = username;
this.password = password;
}
/** default constructor */
public user() {
}
public string getid() {
return this.id;
}
public void setid(string id) {
this.id = id;
}
/**
* @param 用户名
*/
public string getusername() {
return this.username;
}
public void setusername(string username) {
this.username = username;
}
public string getpassword() {
return this.password;
}
public void setpassword(string password) {
this.password = password;
}
public string tostring() {
return new tostringbuilder(this)
.append("id", getid())
.tostring();
}
}

|
schema-export.sql
|
|
drop table if exists usertable2
create table usertable2 ( id varchar(32) not null, ddd varchar(24) not null, 密码 varchar(24) not null, primary key (id) ) |

你好,我按你的说得作了,他说build successful
我刷新还是得不到
sql
这篇文章是转贴过来的,没在Eclipse试过;之前用Eclipse2.1与一个插件就可以正确生成.sql;现在一般都是先创建好数据表后,再用工具生成相应的.java源文件与.xml映射文件。
这里是另一篇文档,看是否有参考价值,
http://www.wujianrong.com/archives/2007/01/hbmddl.html
祝你好运!