用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. java反射教程

    什么是反射,为什么它是有用的,以及如何使用它? 1.什么是反射? “反射通常是JVM中运行的程序需要检测和修改运行时程序的行为的一种能力.”这个概念通常与内省(Introspection)混淆.以下是 ...

  2. js 判断一个对象是否为空

    由于对于一个空对象{},其boolean值也是真,所以不能简单的用boolean来判断: jQuery的源码里有一个判断空对象的方法 function isEmptyObject(a) { var b ...

  3. c#实现对登陆信息的反馈,实现对网站登录密码的扫描

    最近发现我们学校的电信上网改密码的页面很简单,没有验证码,于是我就很好奇,后来发现原来是我们学校的电信的那个改密码的页面有漏洞于是就可以通过扫描账号免费上网 原理就是对修改密码的页面进行POST请求 ...

  4. springboot---数据整合篇

    本文讲解 Spring Boot 基础下,如何使用 JDBC,配置数据源和通过 JdbcTemplate 编写数据访问. 环境依赖 修改 POM 文件,添加spring-boot-starter-jd ...

  5. Leetcode 1022. Sum of Root To Leaf Binary Numbers

    dfs class Solution: def sumRootToLeaf(self, root: TreeNode) -> int: stack=[(root,0)] ans=[] bi_st ...

  6. Ubuntu网络配置IP和DNS等,适用于14.04,16.04和17.10

    本文主要介绍Ubuntu系统的网络设置,包括IP,DNS和主机名等,适用于14.04,16.04和17.10等版本 ===============  完美的分割线 ================ = ...

  7. 三网合一 中国移动铁通光猫 HG6821M 如何设置宽带自动连接

    假期炎热,都说大连是海滨城市比较凉爽,但是那地方潮气太大,实在是不太好呆,于是乎我打道回府来到了内陆家中. 回到家中发现老式的光猫被替换成了  最新的  HG6821M  . 按照铭牌上的说明,登录. ...

  8. BZOJ4372: 烁烁的游戏【动态点分治】

    Description 背景:烁烁很喜欢爬树,这吓坏了树上的皮皮鼠. 题意: 给定一颗n个节点的树,边权均为1,初始树上没有皮皮鼠. 烁烁他每次会跳到一个节点u,把周围与他距离不超过d的节点各吸引出w ...

  9. 51Nod 1049:最大子段和(dp)

    1049 最大子段和  基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 N个整数组成的序列a[1],a[2],a[3],-,a[n],求该序列如a[i]+ ...

  10. .NET 中创建支持集合初始化器的类型

    对象初始化器和集合初始化器只是语法糖,但是能让你的代码看起来更加清晰.至少能让对象初始化的代码和其他业务执行的代码分开,可读性会好一些. 本文将编写一个类型,可以使用集合初始化器构造这个类型.不只是添 ...