Hibernate Tools以Ant Task方式运行配置

本文介绍如何通过AntTask方式配置HibernateTools,实现从映射文件生成Java源文件及DDL文件。需在Build.xml中指定必要的库文件路径及配置项。

Hibernate Tools以Ant Task方式运行,主要在Build.xml中配置。内容如下:

 

<?xml version="1.0" encoding="UTF-8"?>
<project name="University_FG" default="all" basedir=".">
	<!-- "." represents the src folder -->
	<!-- Declare path variables to refer the third party libraries required-->

	<path id="toolslib">
		<path location="./lib/hibernate-tools.jar" />
		<path location="./lib/hibernate3.jar" />
		<path location="./lib/freemarker.jar" />
		<path location="./lib/mysql.jar" />
		<path location="./lib/mysql-connector-java-5.1.7.jar" />
		<path location="./lib/dom4j-1.6.1.jar" />
		<path location="./lib/log4j-1.2.15.jar" />
		<path location="./lib/slf4j-api-1.5.6.jar" />
		<path location="./lib/slf4j-jdk14-1.5.6.jar" />
		<path location="./lib/commons-logging-1.1.jar" />
	</path>

	<!-- To include the src and lib names in the path -->
	<path id="mypath">
		<fileset dir="./lib">
			<include name="**.*" />
		</fileset>
	</path>

	<!-- Declare the HibernateToolTask -->
	<target name="all">
		<taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask" classpathref="mypath" />
		<!-- Declare the destination directory -->
		<hibernatetool destdir="./src">
			<!-- Declare Classpath  -->
			<classpath>
				<path location="./config" />
			</classpath>
			<!-- Declare the configuration file path -->
			<configuration configurationfile="./config/hibernate.cfg.xml">
			</configuration>
			<!-- Convert the mapping file to Java source files -->
			<hbm2java/>
			<!-- Convert the mapping file to DDL files -->
			<hbm2ddl export="true" outputfilename="university_sql.ddl" />
		</hibernatetool>
	</target>

</project>

 

build.xml的内容有两部分:

第一部分是Path,主要是引入所需要的Jar包

第二部分是Hibernate Tools的相关配置。  

hibernatetool属性
属性名称 定义 使用情况
destdir 生成文件的输入目录 Required
templatepath 用户编辑模板的路径 Optional
classpath 解析资源时的依赖环境 Optional,但通常是要求
property(and propertyset) 控制输出的属性设置,大部分与用户定义模板提供属性有关。 Optional

configuration

(annotationconfiguration, jpaconfiguration,

jdbcconfiguration)

必须为Hibernate元模型指定四个属性之一。 Required

hbm2java(hbm2cfgxml,

hbmtemplate, etc.)

指定一个或多个的输出方式 Required

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

   

本例中就有两种输出方式:

一种是根据Mapping生成POJO文件,一种是根据Mapping生成DDL,并生成数据库表。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值