zoukankan      html  css  js  c++  java
  • Eclipse使用xdoclet1.2.3 生成hibernate配置文件和映射文件

    用ant和xdoclet生成hibernate配置文件可以为我们省去很多配置的操作,废话不多说,直接给栗子:

    测试环境:

    eclipse:Eclipse Java EE IDE for Web Developers 4.6.0

    ant:eclipse自带ant,无需下载配置

    xdoclet:xdoclet-1.2.3

    hibernate:hibernate-distribution-3.3.2.GA-dist + hibernate-annotations-3.4.0.GA(由于是老版本所以是有两个的)

    1,、配置xdoclet

    首先解压下载好的xdoclet1.2.3:



    打开eclipse,进入window》preferences》javaEE》XDoclet,选择xdoclet版本 Version:1.2.3(自带ant只支持1.2.1-1.2.3)和XDoclet Home:点击浏览选择刚才安装的xdoclet根路径,这里是:E:softwareForJavaxdocletxdoclet-1.2.3 ,点击OK,如下图



    2.示例程序

    创建一个java project:xdocletTest。

    在工程上右键添加一个名为build.xml的文件

    在工程上右键添加一个名为lib的文件夹,把hibernate依赖的jar包及mysql驱动jar包都放到lib文件夹下

    两个实体类一个为Group(组),一个为(User)

    为了更好的显示日志信息,添加log4j.properties文件到src路径下

    (1)配置实体类

    User.java

    1. package com.xdoclet.model;  
    2.   
    3. /** 
    4.  
    5.  * @hibernate.class 
    6.  
    7.  *     table="t_user" 
    8.  
    9.  * @author welcome 
    10.  
    11.  */  
    12.   
    13. public class User {  
    14.   
    15.     private String userId;  
    16.   
    17.     private String userName;  
    18.   
    19.     private Group group;  
    20.   
    21.     /** 
    22.  
    23.      * @hibernate.id column="userId" 
    24.  
    25.      * generator-class="assigned" 
    26.  
    27.      */  
    28.   
    29.     public String getUserId() {  
    30.   
    31.        return userId;  
    32.   
    33.     }  
    34.   
    35.     public void setUserId(String userId) {  
    36.   
    37.        this.userId = userId;  
    38.   
    39.     }  
    40.   
    41.     /** 
    42.  
    43.      * @hibernate.property 
    44.  
    45.      */  
    46.   
    47.     public String getUserName() {  
    48.   
    49.        return userName;  
    50.   
    51.     }  
    52.   
    53.     public void setUserName(String userName) {  
    54.   
    55.        this.userName = userName;  
    56.   
    57.     }  
    58.   
    59.     /** 
    60.  
    61.      * @hibernate.many-to-one 
    62.  
    63.      *     column="groupId" 
    64.  
    65.      *     cascade="all" 
    66.  
    67.      *     class="com.xdoclet.model.Group" 
    68.  
    69.      * @param group 
    70.  
    71.      */  
    72.   
    73.     public Group getGroup() {  
    74.   
    75.        return group;  
    76.   
    77.     }  
    78.   
    79.     public void setGroup(Group group) {  
    80.   
    81.        this.group = group;  
    82.   
    83.     }  
    84.   
    85. }  

    Group.java

    1. package com.xdoclet.model;  
    2.   
    3. import java.util.Set;  
    4.   
    5. /** 
    6.  
    7.  * @hibernate.class 
    8.  
    9.  *     table="t_group" 
    10.  
    11.  * @author welcome 
    12.  
    13.  */  
    14.   
    15. public class Group {  
    16.   
    17.     private String groupId;  
    18.   
    19.     private String groupName;  
    20.   
    21.     private Set userSets;  
    22.   
    23.     /** 
    24.  
    25.      * @hibernate.id 
    26.  
    27.      *     column="groupId" 
    28.  
    29.      *     generator-class="assigned" 
    30.  
    31.      * @return 
    32.  
    33.      */  
    34.   
    35.     public String getGroupId() {  
    36.   
    37.        return groupId;  
    38.   
    39.     }  
    40.   
    41.     public void setGroupId(String groupId) {  
    42.   
    43.        this.groupId = groupId;  
    44.   
    45.     }  
    46.   
    47.     /** 
    48.  
    49.      * @hibernate.property 
    50.  
    51.      *     column="groupName" 
    52.  
    53.      * @return 
    54.  
    55.      */  
    56.   
    57.     public String getGroupName() {  
    58.   
    59.        return groupName;  
    60.   
    61.     }  
    62.   
    63.     public void setGroupName(String groupName) {  
    64.   
    65.        this.groupName = groupName;  
    66.   
    67.     }  
    68.   
    69.     /** 
    70.  
    71.      * @hibernate.set inverse="true" 
    72.  
    73.      * @hibernate.collection-key column="groupId" 
    74.  
    75.      * @hibernate.collection-one-to-many 
    76.  
    77.      *     class="com.xdoclet.model.User" 
    78.  
    79.      * @return 
    80.  
    81.      */  
    82.   
    83.     public Set getUserSets() {  
    84.        return userSets;  
    85.   
    86.     }  
    87.   
    88.     public void setUserSets(Set userSets) {  
    89.   
    90.        this.userSets = userSets;  
    91.   
    92.     }  
    93.   
    94. }  

    注意:实体类中的注解是xdoclet的,可以去查看xdoclet关于hibernate的相关文档http://xdoclet.sourceforge.net/xdoclet/tags/hibernate-tags.html


    (2)log4j.properties配置文件

    1. #All level less than INFO will be logged  
    2.   
    3. log4j.rootLogger=INFO,A1  
    4.   
    5. #A1 is the output device  
    6.   
    7. log4j.appender.A1=org.apache.log4j.FileAppender  
    8.   
    9. log4j.appender.A1.File=e:/log4j.htm  
    10.   
    11. #use html layout  
    12.   
    13. log4j.appender.A1.layout=org.apache.log4j.HTMLLayout  

    此文件会将日志信息记录为html格式的文件,然后输出到E盘下。


    (3)build.xml

    1. <span style="font-size:14px;"><?xml version="1.0" encoding="GBK"?>  
    2. <project name="使用xdoclet映射hibernate" basedir=".">  
    3.   
    4.     <!-- 定义源文件目录变量 -->  
    5.     <property name="src.dir" value="${basedir}/src" />  
    6.   
    7.     <!-- 定义xdoclet的目录 -->  
    8.     <property name="xdoclet.home" value="E:softwareForJavaxdocletxdoclet-1.2.3">  
    9.     </property>  
    10.   
    11.     <!-- 定义构建路径 -->  
    12.     <path id="xdoclet.classpath">  
    13.         <fileset dir="${xdoclet.home}/lib">  
    14.             <include name="*.jar" />  
    15.         </fileset>  
    16.     </path>  
    17.   
    18.     <path id="lib.classpath">   
    19.         <fileset dir="${xdoclet.home}/lib">  
    20.             <include name="**/*.jar" />  
    21.         </fileset>  
    22.         <fileset dir="${basedir}/lib">  
    23.             <include name="**/*.jar" />  
    24.         </fileset>      
    25.     </path>  
    26.   
    27.     <!-- 生成Hibernate的映射文件 -->  
    28.     <target name="生成Hibernate的映射文件" unless="hibernatedoclet.unnecessary" description="Generate Hibernate mapping files">  
    29.         <taskdef name="hibernatedoclet" classname="xdoclet.modules.hibernate.HibernateDocletTask" classpathref="xdoclet.classpath" />  
    30.         <hibernatedoclet destdir="${src.dir}" mergedir="${src.dir}" excludedtags="@version,@author,@todo,@see" verbose="false">  
    31.             <fileset dir="${src.dir}">  
    32.                 <include name="com/xdoclet/model/*.java" />  
    33.             </fileset>  
    34.             <hibernate version="3.0" />  
    35.         </hibernatedoclet>  
    36.     </target>  
    37.   
    38.     <!-- 生成Hibernate配置文件 -->  
    39.     <target name="生成Hibernate配置文件" depends="生成Hibernate的映射文件">  
    40.         <taskdef name="hibernatedoclet" classname="xdoclet.modules.hibernate.HibernateDocletTask" classpathref="xdoclet.classpath" />  
    41.         <hibernatedoclet destdir="${src.dir}">  
    42.             <fileset dir="${src.dir}">  
    43.                 <include name="com/xdoclet/model/*.java" />  
    44.             </fileset>  
    45.             <hibernatecfg destdir="${src.dir}" version="3.0" hbm2ddl="create-update" jdbcUrl="jdbc:mysql://localhost:3306/oa" driver="com.mysql.jdbc.Driver" username="root" password="123456" dialect="org.hibernate.dialect.MySQL5Dialect" showSql="true">  
    46.                 <otherProperty name="hbm2ddl" value="create-update" />  
    47.                 <otherProperty name="format_sql" value="true" />  
    48.             </hibernatecfg>  
    49.         </hibernatedoclet>  
    50.     </target>  
    51.   
    52.     <!-- 导出数据库表结构 -->  
    53.   
    54.     <target name="导出数据库表结构" depends="生成Hibernate配置文件">  
    55.         <taskdef name="schemaexport" classname="org.hibernate.tool.hbm2ddl.SchemaExportTask" classpathref="lib.classpath" />  
    56.         <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />  
    57.         <property name="hibernate.format_sql" value="true" />  
    58.         <property name="hibernate.use_sql_comments" value="true" />  
    59.         <schemaexport output="schema-export.sql" quiet="no" text="yes" drop="no" delimiter=";">  
    60.             <fileset dir="${basedir}/src">  
    61.                 <include name="com/xdoclet/model/*.hbm.xml" />  
    62.             </fileset>  
    63.         </schemaexport>  
    64.     </target>  
    65. </project></span>  

    这里需要注意的是这两个元素

    <otherProperty name="hbm2ddl" value="create-update" />

    <otherProperty name="format_sql" value="true" />

    在xdoclet1.0.4以后的版本中hbm2ddl需要以额外的方式来设置,之前我按照1.0.4版本中的方式去设置,此属性死活不会出现在hibernate.cfg.xml中,最后得知这是xdoclet的一个bug。(第一个我玩了大半天。。。)


    (4)ExportTable.java (生成数据库表结构)

    1. package com.xdoclet.export;  
    2.   
    3. import org.hibernate.cfg.Configuration;  
    4.   
    5. import org.hibernate.tool.hbm2ddl.SchemaExport;  
    6.   
    7. public class ExportTable {  
    8.   
    9.     public static void main(String[] args) {  
    10.   
    11.        Configuration configuration=new Configuration().configure("hibernate.cfg.xml");  
    12.   
    13.        SchemaExport export=new SchemaExport(configuration);  
    14.   
    15.        export.create(truetrue);  
    16.   
    17.     }  
    18.   
    19. }  
    此处需要导入hibernate的jar包和MySQL驱动包

    最后你看到的项目结构应该是这样的:



    3.生成配置文件

    打开mysql,创建一个名为oa的数据库

    在eclipse中运行ant:点window->show view->ant,添加我们的ant脚本到eclipse的ant视图中,右键ant视图空白处或者点击下图位置添加构建脚本:


    选择build.xml文件,在”导出数据库表结构“上点击run as ant

    此时控制台输出:

    [plain] view plain copy
    1. Buildfile: E:"JAR"jbpm"jbpm-4.3"workspace"xdoclet"build.xml  
    2.   
    3. 生成Hibernate的映射文件:  
    4.   
    5. [hibernatedoclet] (XDocletMain.start                   47 ) Running <hibernate/>  
    6.   
    7. [hibernatedoclet] Generating mapping file for com.xdoclet.model.Group.  
    8.   
    9. [hibernatedoclet]    com.xdoclet.model.Group  
    10.   
    11. [hibernatedoclet] Generating mapping file for com.xdoclet.model.User.  
    12.   
    13. [hibernatedoclet]    com.xdoclet.model.User  
    14.   
    15. 生成Hibernate配置文件:  
    16.   
    17. [hibernatedoclet] addOtherProperty(): name=null, null  
    18.   
    19. [hibernatedoclet] addOtherProperty(): name=null, null  
    20.   
    21. [hibernatedoclet] (XDocletMain.start                   47 ) Running <hibernatecfg/>  
    22.   
    23. [hibernatedoclet] Generating hibernate.cfg.xml configuration file  
    24.   
    25. 导出数据库表结构:  
    26.   
    27. [schemaexport] (cfg.Environment                     500 ) Hibernate 3.2.0  
    28.   
    29. [schemaexport] (cfg.Environment                     533 ) hibernate.properties not found  
    30.   
    31. [schemaexport] (cfg.Environment                     667 ) Bytecode provider name : cglib  
    32.   
    33. [schemaexport] (cfg.Environment                     584 ) using JDK 1.4 java.sql.Timestamp handling  
    34.   
    35. [schemaexport] (cfg.Configuration                   274 ) Reading mappings from file: E:"JAR"jbpm"jbpm-4.3"workspace"xdoclet"src"com"xdoclet"model"Group.hbm.xml  
    36.   
    37. [schemaexport] (cfg.HbmBinder                       300 ) Mapping class: com.xdoclet.model.Group -> t_group  
    38.   
    39. [schemaexport] (cfg.Configuration                   274 ) Reading mappings from file: E:"JAR"jbpm"jbpm-4.3"workspace"xdoclet"src"com"xdoclet"model"User.hbm.xml  
    40.   
    41. [schemaexport] (cfg.HbmBinder                       300 ) Mapping class: com.xdoclet.model.User -> t_user  
    42.   
    43. [schemaexport] (dialect.Dialect                     141 ) Using dialect: org.hibernate.dialect.MySQL5Dialect  
    44.   
    45. [schemaexport] (cfg.HbmBinder                       2375) Mapping collection: com.xdoclet.model.Group.userSets -> t_user  
    46.   
    47. [schemaexport] (hbm2ddl.SchemaExport                154 ) Running hbm2ddl schema export  
    48.   
    49. [schemaexport] (hbm2ddl.SchemaExport                174 ) writing generated schema to file: E:"JAR"jbpm"jbpm-4.3"workspace"xdoclet"schema-export.sql  
    50.   
    51. [schemaexport]  
    52.   
    53. [schemaexport]     alter table t_user  
    54.   
    55. [schemaexport]         drop  
    56.   
    57. [schemaexport]         foreign key FKCB63CCB6CEAB0634;  
    58.   
    59. [schemaexport]  
    60.   
    61. [schemaexport]     drop table if exists t_group;  
    62.   
    63. [schemaexport]  
    64.   
    65. [schemaexport]     drop table if exists t_user;  
    66.   
    67. [schemaexport]  
    68.   
    69. [schemaexport]     create table t_group (  
    70.   
    71. [schemaexport]         groupId varchar(255) not null,  
    72.   
    73. [schemaexport]         groupName varchar(255),  
    74.   
    75. [schemaexport]         primary key (groupId)  
    76.   
    77. [schemaexport]     );  
    78.   
    79. [schemaexport]  
    80.   
    81. [schemaexport]     create table t_user (  
    82.   
    83. [schemaexport]         userId varchar(255) not null,  
    84.   
    85. [schemaexport]         userName varchar(255),  
    86.   
    87. [schemaexport]         groupId varchar(255),  
    88.   
    89. [schemaexport]         primary key (userId)  
    90.   
    91. [schemaexport]     );  
    92.   
    93. [schemaexport]  
    94.   
    95. [schemaexport]     alter table t_user  
    96.   
    97. [schemaexport]         add index FKCB63CCB6CEAB0634 (groupId),  
    98.   
    99. [schemaexport]         add constraint FKCB63CCB6CEAB0634  
    100.   
    101. [schemaexport]         foreign key (groupId)  
    102.   
    103. [schemaexport]         references t_group (groupId);  
    104.   
    105. [schemaexport] (hbm2ddl.SchemaExport                196 ) schema export complete  
    106.   
    107. BUILD SUCCESSFUL  
    108.   
    109. Total time: 1 second  

    此时再刷新工程目录,就会发现已经生成了hibernate的配置文件和映射文件,而且sql 脚本竟然也生成了!最后如下:


    再运行ExportTable.java(运行之前要先build path导入hibernate的jar包和MySQL驱动jar包),就生成了数据库表结构了,赶紧打开MySQL看一下吧!

    源文件百度云下载:

    屠龙宝刀,点击就送》》链接:http://pan.baidu.com/s/1hs2W5q0  密码:x2hp


    参考: http://www.blogjava.net/sxyx2008/archive/2010/09/30/333554.html

  • 相关阅读:
    64win7+64Oracle+32plsql
    Delphi与Java中的日期互换
    QT变异版本下载(SJLJ长跳转,DWARF不传递错误(32位专用),SEH(64位专用)),以及QT的实验室项目
    顿悟,职业生涯最重要的是行业水平的积累,而不是多学某一门语言(很危险)——遥想铁血强人刘志军对铁路行业的理解以及执行力
    天下古今之庸人,皆以一惰字致败(联想到了自己的高考数学大题)
    这七种情况下,不要创业
    Android 点击桌面快捷方式和Notifycation跳转到Task栈顶Activity
    字符串模式匹配算法——BM、Horspool、Sunday、KMP、KR、AC算法一网打尽
    QSplashScreen开机画面(不断的repaint)
    一个QT 3D转动控件
  • 原文地址:https://www.cnblogs.com/xiang--liu/p/9710290.html
Copyright © 2011-2022 走看看