用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:\softwareForJava\xdoclet\xdoclet-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. * @hibernate.class
  4. *     table="t_user"
  5. * @author welcome
  6. */
  7. public class User {
  8. private String userId;
  9. private String userName;
  10. private Group group;
  11. /**
  12. * @hibernate.id column="userId"
  13. * generator-class="assigned"
  14. */
  15. public String getUserId() {
  16. return userId;
  17. }
  18. public void setUserId(String userId) {
  19. this.userId = userId;
  20. }
  21. /**
  22. * @hibernate.property
  23. */
  24. public String getUserName() {
  25. return userName;
  26. }
  27. public void setUserName(String userName) {
  28. this.userName = userName;
  29. }
  30. /**
  31. * @hibernate.many-to-one
  32. *     column="groupId"
  33. *     cascade="all"
  34. *     class="com.xdoclet.model.Group"
  35. * @param group
  36. */
  37. public Group getGroup() {
  38. return group;
  39. }
  40. public void setGroup(Group group) {
  41. this.group = group;
  42. }
  43. }

Group.java

  1. package com.xdoclet.model;
  2. import java.util.Set;
  3. /**
  4. * @hibernate.class
  5. *     table="t_group"
  6. * @author welcome
  7. */
  8. public class Group {
  9. private String groupId;
  10. private String groupName;
  11. private Set userSets;
  12. /**
  13. * @hibernate.id
  14. *     column="groupId"
  15. *     generator-class="assigned"
  16. * @return
  17. */
  18. public String getGroupId() {
  19. return groupId;
  20. }
  21. public void setGroupId(String groupId) {
  22. this.groupId = groupId;
  23. }
  24. /**
  25. * @hibernate.property
  26. *     column="groupName"
  27. * @return
  28. */
  29. public String getGroupName() {
  30. return groupName;
  31. }
  32. public void setGroupName(String groupName) {
  33. this.groupName = groupName;
  34. }
  35. /**
  36. * @hibernate.set inverse="true"
  37. * @hibernate.collection-key column="groupId"
  38. * @hibernate.collection-one-to-many
  39. *     class="com.xdoclet.model.User"
  40. * @return
  41. */
  42. public Set getUserSets() {
  43. return userSets;
  44. }
  45. public void setUserSets(Set userSets) {
  46. this.userSets = userSets;
  47. }
  48. }

注意:实体类中的注解是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. log4j.rootLogger=INFO,A1
  3. #A1 is the output device
  4. log4j.appender.A1=org.apache.log4j.FileAppender
  5. log4j.appender.A1.File=e:/log4j.htm
  6. #use html layout
  7. 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. <property name="src.dir" value="${basedir}/src" />
  5. <!-- 定义xdoclet的目录 -->
  6. <property name="xdoclet.home" value="E:\softwareForJava\xdoclet\xdoclet-1.2.3">
  7. </property>
  8. <!-- 定义构建路径 -->
  9. <path id="xdoclet.classpath">
  10. <fileset dir="${xdoclet.home}/lib">
  11. <include name="*.jar" />
  12. </fileset>
  13. </path>
  14. <path id="lib.classpath">
  15. <fileset dir="${xdoclet.home}/lib">
  16. <include name="**/*.jar" />
  17. </fileset>
  18. <fileset dir="${basedir}/lib">
  19. <include name="**/*.jar" />
  20. </fileset>
  21. </path>
  22. <!-- 生成Hibernate的映射文件 -->
  23. <target name="生成Hibernate的映射文件" unless="hibernatedoclet.unnecessary" description="Generate Hibernate mapping files">
  24. <taskdef name="hibernatedoclet" classname="xdoclet.modules.hibernate.HibernateDocletTask" classpathref="xdoclet.classpath" />
  25. <hibernatedoclet destdir="${src.dir}" mergedir="${src.dir}" excludedtags="@version,@author,@todo,@see" verbose="false">
  26. <fileset dir="${src.dir}">
  27. <include name="com/xdoclet/model/*.java" />
  28. </fileset>
  29. <hibernate version="3.0" />
  30. </hibernatedoclet>
  31. </target>
  32. <!-- 生成Hibernate配置文件 -->
  33. <target name="生成Hibernate配置文件" depends="生成Hibernate的映射文件">
  34. <taskdef name="hibernatedoclet" classname="xdoclet.modules.hibernate.HibernateDocletTask" classpathref="xdoclet.classpath" />
  35. <hibernatedoclet destdir="${src.dir}">
  36. <fileset dir="${src.dir}">
  37. <include name="com/xdoclet/model/*.java" />
  38. </fileset>
  39. <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">
  40. <otherProperty name="hbm2ddl" value="create-update" />
  41. <otherProperty name="format_sql" value="true" />
  42. </hibernatecfg>
  43. </hibernatedoclet>
  44. </target>
  45. <!-- 导出数据库表结构 -->
  46. <target name="导出数据库表结构" depends="生成Hibernate配置文件">
  47. <taskdef name="schemaexport" classname="org.hibernate.tool.hbm2ddl.SchemaExportTask" classpathref="lib.classpath" />
  48. <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
  49. <property name="hibernate.format_sql" value="true" />
  50. <property name="hibernate.use_sql_comments" value="true" />
  51. <schemaexport output="schema-export.sql" quiet="no" text="yes" drop="no" delimiter=";">
  52. <fileset dir="${basedir}/src">
  53. <include name="com/xdoclet/model/*.hbm.xml" />
  54. </fileset>
  55. </schemaexport>
  56. </target>
  57. </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. import org.hibernate.cfg.Configuration;
  3. import org.hibernate.tool.hbm2ddl.SchemaExport;
  4. public class ExportTable {
  5. public static void main(String[] args) {
  6. Configuration configuration=new Configuration().configure("hibernate.cfg.xml");
  7. SchemaExport export=new SchemaExport(configuration);
  8. export.create(true, true);
  9. }
  10. }

此处需要导入hibernate的jar包和MySQL驱动包

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

3.生成配置文件

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

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

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

此时控制台输出:

  1. Buildfile: E:"JAR"jbpm"jbpm-4.3"workspace"xdoclet"build.xml
  2. 生成Hibernate的映射文件:
  3. [hibernatedoclet] (XDocletMain.start                   47 ) Running <hibernate/>
  4. [hibernatedoclet] Generating mapping file for com.xdoclet.model.Group.
  5. [hibernatedoclet]    com.xdoclet.model.Group
  6. [hibernatedoclet] Generating mapping file for com.xdoclet.model.User.
  7. [hibernatedoclet]    com.xdoclet.model.User
  8. 生成Hibernate配置文件:
  9. [hibernatedoclet] addOtherProperty(): name=null, null
  10. [hibernatedoclet] addOtherProperty(): name=null, null
  11. [hibernatedoclet] (XDocletMain.start                   47 ) Running <hibernatecfg/>
  12. [hibernatedoclet] Generating hibernate.cfg.xml configuration file
  13. 导出数据库表结构:
  14. [schemaexport] (cfg.Environment                     500 ) Hibernate 3.2.0
  15. [schemaexport] (cfg.Environment                     533 ) hibernate.properties not found
  16. [schemaexport] (cfg.Environment                     667 ) Bytecode provider name : cglib
  17. [schemaexport] (cfg.Environment                     584 ) using JDK 1.4 java.sql.Timestamp handling
  18. [schemaexport] (cfg.Configuration                   274 ) Reading mappings from file: E:"JAR"jbpm"jbpm-4.3"workspace"xdoclet"src"com"xdoclet"model"Group.hbm.xml
  19. [schemaexport] (cfg.HbmBinder                       300 ) Mapping class: com.xdoclet.model.Group -> t_group
  20. [schemaexport] (cfg.Configuration                   274 ) Reading mappings from file: E:"JAR"jbpm"jbpm-4.3"workspace"xdoclet"src"com"xdoclet"model"User.hbm.xml
  21. [schemaexport] (cfg.HbmBinder                       300 ) Mapping class: com.xdoclet.model.User -> t_user
  22. [schemaexport] (dialect.Dialect                     141 ) Using dialect: org.hibernate.dialect.MySQL5Dialect
  23. [schemaexport] (cfg.HbmBinder                       2375) Mapping collection: com.xdoclet.model.Group.userSets -> t_user
  24. [schemaexport] (hbm2ddl.SchemaExport                154 ) Running hbm2ddl schema export
  25. [schemaexport] (hbm2ddl.SchemaExport                174 ) writing generated schema to file: E:"JAR"jbpm"jbpm-4.3"workspace"xdoclet"schema-export.sql
  26. [schemaexport]
  27. [schemaexport]     alter table t_user
  28. [schemaexport]         drop
  29. [schemaexport]         foreign key FKCB63CCB6CEAB0634;
  30. [schemaexport]
  31. [schemaexport]     drop table if exists t_group;
  32. [schemaexport]
  33. [schemaexport]     drop table if exists t_user;
  34. [schemaexport]
  35. [schemaexport]     create table t_group (
  36. [schemaexport]         groupId varchar(255) not null,
  37. [schemaexport]         groupName varchar(255),
  38. [schemaexport]         primary key (groupId)
  39. [schemaexport]     );
  40. [schemaexport]
  41. [schemaexport]     create table t_user (
  42. [schemaexport]         userId varchar(255) not null,
  43. [schemaexport]         userName varchar(255),
  44. [schemaexport]         groupId varchar(255),
  45. [schemaexport]         primary key (userId)
  46. [schemaexport]     );
  47. [schemaexport]
  48. [schemaexport]     alter table t_user
  49. [schemaexport]         add index FKCB63CCB6CEAB0634 (groupId),
  50. [schemaexport]         add constraint FKCB63CCB6CEAB0634
  51. [schemaexport]         foreign key (groupId)
  52. [schemaexport]         references t_group (groupId);
  53. [schemaexport] (hbm2ddl.SchemaExport                196 ) schema export complete
  54. BUILD SUCCESSFUL
  55. 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

Eclipse使用xdoclet1.2.3 生成hibernate配置文件和映射文件的更多相关文章

  1. Java IDE 编辑器 --- IntelliJ IDEA 进阶篇 生成 hibernate 实体与映射文件

    原文:转:Java IDE 编辑器 --- IntelliJ IDEA 进阶篇 生成 hibernate 实体与映射文件 2011-04-30 12:50 很多人不知道怎么用 IntelliJ IDE ...

  2. Hibernate 配置文件与映射文件 总结

    hibernate是一个彻底的ORM(Object Relational Mapping,对象关系映射)开源框架. 一.Hibernate配置文件详解 Hibernate配置文件有两种形式:XML与p ...

  3. Hibernate配置文件和映射文件详解

    Hibernate是一个彻底的ORM(Object Relational Mapping,对象关系映射)开源框架. 我们先看一下官方文档所给出的,Hibernate 体系结构的高层视图: 其中PO=P ...

  4. Hibernate配置文件与映射文件的创建

    1. config文件的创建: 内容: <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hib ...

  5. Eclipse使用hibernate插件反向生成实体类和映射文件

    一般dao层的开发是这样的,先进行数据库的设计,什么E-R图之类的那些,然后选择一款数据库产品,建好表.最后反向生成Java实体和映射文件,这样可以保证一致性和便捷性. 如果用myeclipse,逆向 ...

  6. Xdoclet + Ant自动生成Hibernate配置文件

    在使用Hibernate的时候,过多的Hibernate配置文件是一个让人头疼的问题.最近接触了Xdoclet这个工具.它实际上就是一个自动代码生成的工具,Xdoclet不能单独运行,必须搭配其他工具 ...

  7. Xdoclet + Ant自己主动生成Hibernate配置文件

    在使用Hibernate的时候,过多的Hibernate配置文件是一个让人头疼的问题. 近期接触了Xdoclet这个工具. 它实际上就是一个自己主动代码生成的工具.Xdoclet不能单独执行,必须搭配 ...

  8. Mybatis第六篇【配置文件和映射文件再解读、占位符、主键生成与获取、Mapper代理】

    配置文件和映射文件再解读 映射文件 在mapper.xml文件中配置很多的sql语句,执行每个sql语句时,封装为MappedStatement对象,mapper.xml以statement为单位管理 ...

  9. IBatisNet -- 保护你的配置文件及映射文件信息

    通常情况下我们在使用IBatisNet的时候,配置文件和映射文件都是暴露在外的,如果能进入到服务器,那么你的程序的操作数据库的SQL语句,数据库连接字符串等信息都将很轻松的被看到,这样是很危险的.然而 ...

随机推荐

  1. bzoj2049: [Sdoi2008]Cave 洞穴勘测 lct裸题

    题意:三种操作一种摧毁一条边,一种链接一条边,一种查询两个点是否联通 题解:lct的link和cut即可 /********************************************** ...

  2. dva subscription的使用方法

    import { routerRedux } from 'dva/router' export default { namespace: 'notice', state: { notices:[], ...

  3. JS BOM操作

    Bom:浏览器对象模型(Browser Object Model,简称 BOM)提供了独立于内容而与浏览器窗口进行交互的对象.描述了与浏览器进行交互的方法和接口,可以对浏览器窗口进行访问和操作 (1) ...

  4. xml生成javabean(zhuan)

    package com.dom4j; import java.io.File;import java.io.FileWriter;import java.io.IOException;import j ...

  5. New Concept English Two 6 13

    $课文11 礼尚往来 105. I was having dinner at a restaurant when Tony Steele came in. 我正在一家饭馆吃饭,托尼.斯蒂尔走了进来. ...

  6. 词云:解决pip install wordcloud安装过程中报错“error: command 'x86_64-linux-gnu-gcc' failed with exit status 1”问题

    外部环境:ubuntu16.04, 64bits, 全局环境python2.7 在虚拟环境(python3.5)中执行 pip install wordcloud 时安装失败,报错: error: c ...

  7. test20181024 nan

    题意 nan 问题描述 我们有一个序列,现在他里面有三个数1,2,2.我们从第三个数开始考虑: 第三个数是2,所以我们在序列后面写2个3,变成1,2,2,3,3. 第四个数是3,所以我们在序列后面写3 ...

  8. nginx brotli 压缩试用

    brotli 的压缩比相对gzip 有好多提升 测试试用docker 测试代码 https://github.com/rongfengliang/rollup-babel-demolibrary 运行 ...

  9. cratedb 基本试用

    安装 docker run -d -p 4200:4200 crate UI访问 http://localhost:4200/#!/ 创建数据 tweets 是默认导入的,点击帮助导航可以操作 登陆 ...

  10. ambassador 学习六 Module说明

    模块允许给与特定的mapping 或者整体添加特定的行为,方便进行系统的控制. 当前的module 定义主要是系统级别的 当前系统主要的配置 --- apiVersion: ambassador/v0 ...