Eclipse快速上手Hibernate-利用Hbm映射文件开发[转]

| 3 Comments | No TrackBacks

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&amp;characterEncoding=GBK" 表示使用GBK进行编码 -->        
    <property name="connection.url">jdbc:mysql://localhost:3306/HibernateTest?useUnicode=true&amp;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>

4. 运行任务
 
·  右击“build.xml” ->“运行” ->这里应该有“ant 构建”和“ant 构建...”两个菜单,其中“ant 构建”直接运行缺省任务,这里是指“help”任务;如果要运行其它的任务,可以通过“ant 构建...”菜单选择。
·  这里还有一种更好的方法,eclipse主菜单上点击“窗口” ->“显示视图” ->点击“ant”,这样就调出了ant视图,在这个视图窗口的空白处,右击,在弹出菜单中选择“添加构建文件”,然后将 hibernatebegin_2项目根目录下的“build.xml”文件载入即可。效果如图:
这样,想运行某个任务,直接双击ant视图中的任务即可。
 
生成user.java
·  双击“generate-code”任务,在控制台应该可以看到如下输出:
 
·  如果在src目录下没有看到“user.java”这个文件,那么选中src目录,然后按一下“f5”功能键刷新一下src目录,应该可以在包 “javamxj.hibernate”下看到“user.java”。这个文件就是“hbm2java”生成器根据hbm文件产生的,如下:

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();
}

}

可以对照“user.hbm.xml”文件,看看都是哪些属性转化成什么代码。
 
●  生成数据表
·  启动mysql,应该确定含有hibernatetest数据库,不过这次不需要建立数据表了。
·  双击“schemaexport”任务,控制台输出如下,注意自动生成的sql语句:
 
·  同时,在项目根目录下,也会产生一个“schema-export.sql”文件(如果没有,按f5键刷新目录),这个文件是在“build.xml”中设定的:
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)


No TrackBacks

TrackBack URL: http://www.wujianrong.com/mt-tb.cgi/1648

3 Comments

你好,我按你的说得作了,他说build successful
我刷新还是得不到
sql

这篇文章是转贴过来的,没在Eclipse试过;之前用Eclipse2.1与一个插件就可以正确生成.sql;现在一般都是先创建好数据表后,再用工具生成相应的.java源文件与.xml映射文件。

这里是另一篇文档,看是否有参考价值,

http://www.wujianrong.com/archives/2007/01/hbmddl.html

祝你好运!

Leave a comment

About this Entry

This page contains a single entry by kevinwu published on January 27, 2007 5:07 PM.

从hbm文件生成ddl was the previous entry in this blog.

Eclipse插件 包含swing插件 is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.