工大后院

 找回密码
 加入后院

扫一扫,访问微社区

QQ登录

只需一步,快速开始

搜索
查看: 2338|回复: 3

hibernate中用Xdoclet生成映射文件

[复制链接]
发表于 2006-6-8 13:23 | 显示全部楼层 |阅读模式
用过hibernate的人都知道,如果要自己手工去写持久化类的映射文件,是件痛苦的不能再痛苦的事情,因为不管你对hibernate有多么精通,不可避免的都会出现这样那样的错误,我个人就试过写了N次,映射文件还是有错误,幸好有Xdoclet这个工具,只要用ant运行配置好的Xdoclet文件,轻轻一点,就自动生成映射文件,多谢TVB,多谢英皇~~~~~~~呢个世界清静晒~~~
  下面说说怎么用Xdoclet生成映射文件吧
1.用eclipse新建一个项目,这个就不用我多说了吧。
2.把hibernate要用到的.jar包都引用进项目,有很多,如果不知道是哪些的话,把hibernate API下的bin里边东西全部都引用进去吧。。。:hug:
3.把Xdoclet的.jar也引用进项目。并且把下载回来的Xdoclet所有文件拷到本项目的根目录下。
4.写持久化类
比如我写了一个test1的持久化类
public class test1 {
        int ID;
        String name;
        String password;
        /**
         * @return the iD
         */
        public int getID() {
                return ID;
        }
        /**
         * @param id the iD to set
         */
        public void setID(int id) {
                ID = id;
        }
        /**
         * @return the name
         */
        public String getName() {
                return name;
        }
        /**
         * @param name the name to set
         */
        public void setName(String name) {
                this.name = name;
        }
        /**
         * @return the password
         */
        public String getPassword() {
                return password;
        }
        /**
         * @param password the password to set
         */
        public void setPassword(String password) {
                this.password = password;
        }

}

5.在持久化类添加生成映射文件的标签,添加后的代码如下:
/**
* @hibernate.class table="test1"
*/
public class test1 {
        int ID;
        String name;
        String password;
        /**
         * @return the iD
         * @hibernate.id generator-class="native"
         */
        public int getID() {
                return ID;
        }
        /**
         * @param id the iD to set
         *
         */
        public void setID(int id) {
                ID = id;
        }
        /**
         * @return the name
         * @hibernate.property
         */
        public String getName() {
                return name;
        }
        /**
         * @param name the name to set
         */
        public void setName(String name) {
                this.name = name;
        }
        /**
         * @return the password
         * @hibernate.property
         */
        public String getPassword() {
                return password;
        }
        /**
         * @param password the password to set
         */
        public void setPassword(String password) {
                this.password = password;
        }
}

注意添加前与添加后有什么不同。
6.创建用于生成映射文件的ANT文件,我创建的文件名是gen-hbm.xml  放在本项目的根目录下。
下面给出gen-hbm.xml这个文件的具体代码:
<?xml version="1.0" encoding="ISO-8859-1"?>
<project name="XDoclet Hibernate Generator" default="hibernate" basedir=".">
        <property name="xdoclet.root.dir" value="E:\hexq\hibernate\Xdoclet\xdoclet-bin-1.2.3\xdoclet-1.2.3" />
        <property name="src.dir" value="src" />
        <property name="web.dir" value="war" />
        <property name="xdoclet.lib.dir" value="${xdoclet.root.dir}/lib" />
        <path id="master-classpath">
                <fileset dir="${xdoclet.lib.dir}">
                        <include name="*.jar" />
                </fileset>
        </path>
        <taskdef name="hibernatedoclet" classname="xdoclet.modules.hibernate.HibernateDocletTask" classpath ref="master-classpath" />
        <target name="hibernate" description="Generate mapping documents">
                <hibernatedoclet destdir="./src" excludedtags="@version,@author,@todo,@see" addedtags="@xdoclet-generated at ${TODAY},@copyright The XDoclet Team,@author XDoclet,@version ${version}" force="false" verbose="true">

                        <fileset dir="./src">

                                <include name="com/hiany/visit/*.java" />
                        </fileset>

                        <hibernate version="3.0" />
                </hibernatedoclet>
        </target>
</project>

在第三行指定了Xdoclet的目录。
7.运行 gen-hbm.xml生成映射文件,生成的映射文件的文件名为test1.hbm.xml,自动放到了跟持久化类同一层的目录下。下面 也给出test1.hbm.xml这个文件的 具体内容:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
>
    <class
        name="com.hiany.visit.test1"
        table="test1"
    >
        <id
            name="ID"
            column="ID"
            type="int"
        >
            <generator class="native">
              <!--  
                  To add non XDoclet generator parameters, create a file named
                  hibernate-generator-params-test1.xml
                  containing the additional parameters and place it in your merge dir.
              -->
            </generator>
        </id>

        <property
            name="name"
            type="java.lang.String"
            update="true"
            insert="true"
            column="name"
        />

        <property
            name="password"
            type="java.lang.String"
            update="true"
            insert="true"
            column="password"
        />

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-test1.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

</hibernate-mapping>
8总结,好的工具就要懂得拿来用~~~~~~:victory:

[ 本帖最后由 hexq 于 2006-6-8 13:34 编辑 ]
发表于 2006-6-8 22:08 | 显示全部楼层
有个软件叫CodeSmith,
有一个专门用于NHibernate的模板,
利用它们可以很快地利用设计好的数据库生成配置文件。
个人觉得生成的配置文件相当专业(不过有点小Bug,)
楼主有兴趣可以试试。
回复

使用道具 举报

发表于 2006-6-8 22:57 | 显示全部楼层
正在学习中,感谢分享!
可惜最近一大堆考试,有空再试试!
回复

使用道具 举报

发表于 2006-6-9 10:02 | 显示全部楼层
没用过。个人用myeclipse插件,对hibernate的各方面提供良好支持,当然也能实现楼主提到的功能。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 加入后院

本版积分规则

QQ|Archiver|手机版|小黑屋|广告业务Q|工大后院 ( 粤ICP备10013660号 )

GMT+8, 2025-5-15 00:52

Powered by Discuz! X3.5

Copyright © 2001-2024 Tencent Cloud.

快速回复 返回顶部 返回列表