使用Ant自动化发开Hibernate
在开发使用Hibernate框架应用程序中,无外乎要创建hibernate.cfg.xml,xxx.hbm.xml,pojos和databaseSchema这些文件,框架这东西刚开始学会的时候,都是心情舒畅,热情高涨,所有文件都是自己手写不管这样重复性的工作有多么的乏味,爱情都会由低潮期更别说重复Coding了,久而久之在编写一个个xml、pojo和schema的过程中,不由从心中产生一种不舒畅之气。我曾经有一段时间见到代码文件就有想吐的冲动,不过还好我还是挺过来,嘛也就是像爱情麻木期一样,怎么都无所谓了。直到最近了解到Ant这个工具,让我看到了另一种发开Hibernate应用程序的方式,并不是说这样的开发方式就一定好于纯手工编写,适合自己才是最好的,但我是一个比较爱偷懒的,自动化构建非常的吸引我。开头字数凑够了就进入正题了,首先直接上build.xml,这个文件是ant所需要的
<?xml version="1.0" encoding="UTF-8"?>
<project name="hibernatetools" basedir="." default="useHibernateTool">
<!-- 导入build.properties文件,该文件定义一些常量,这些常量也可以是用<property name="" value="">的形式定义 -->
<property file="build.properties"></property>
<!-- 定义依赖包所在的位置,"${reference_libs_dir}"表示引用名为"reference_libs_dir"的常量 -->
<path id="referencelib">
<fileset dir="${reference_libs_dir}">
<!-- 匹配模式,表示该目录下所有jar文件 -->
<include name="**/*.jar"/>
</fileset>
</path>
<!-- 自定义Ant Task,但是使用的自定义类为Hibernate为我们提供的 -->
<taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask" classpathref="referencelib"></taskdef>
<!-- 使用hibernatetool -->
<target name="useHibernateTool">
<hibernatetool>
<!-- 指定资源搜索位置 -->
<classpath>
<path location="${source_folder_dir}"></path>
<path location="bin"></path>
</classpath>
<!-- 指定Hibernate的配置文件 -->
<configuration configurationfile="${source_folder_dir}/hibernate.cfg.xml"/>
<!-- 根据hbm生成java代码 -->
<hbm2java jdk5="true" destdir="${source_folder_dir}"/>
<!-- 根据hbm生成databaseSchema -->
<hbm2ddl destdir="${database_schema_dir}" export="true" outputfilename="sqlStatement.sql" format="true"/>
</hibernatetool>
</target>
</project>
复制代码
build.properties文件:
source_folder_dir=src
reference_libs_dir=referencelibs
database_schema_dir=dbschema
复制代码
这是项目最开始的状态:
此时项目中仅仅只是定义了cfg和hbm文件,数据库schema和pojo都没有定义,"referencelibs"中存放的是该程序要成功需要的依赖库文件。运行Ant,此时项目的状态:
可以看到,我们的需要的pojo类和schema都生成好了,其实按照我的build.xml设置,数据库已经直接设置到了两张表了。验证器正确性:
Customer.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.beliefbetrayal.hibernate.pojos.Customer" table="customer">
<!-- 主键设置 -->
<id name="id" type="string">
<column name="id"></column>
<generator class="uuid"></generator>
</id>
<!-- 属性设置 -->
<property name="username" column="username" type="string"></property>
<property name="balance" column="balance" type="integer"></property>
<set name="orders" inverse="true" cascade="all">
<key column="customer_id" ></key>
<one-to-many class="com.beliefbetrayal.hibernate.pojos.Order"/>
</set>
</class>
</hibernate-mapping>
复制代码
Customer.java
package com.beliefbetrayal.hibernate.pojos;
// Generated 2012-2-8 1:10:59 by Hibernate Tools 3.3.0.GA
import java.util.HashSet;
import java.util.Set;
/**
* Customer generated by hbm2java
*/
public class Customer implements java.io.Serializable {
private String id;
private String username;
private Integer balance;
private Set<Order> orders = new HashSet<Order>(0);
public Customer() {
}
public Customer(String username, Integer balance, Set<Order> orders) {
this.username = username;
this.balance = balance;
this.orders = orders;
}
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getBalance() {
return this.balance;
}
public void setBalance(Integer balance) {
this.balance = balance;
}
public Set<Order> getOrders() {
return this.orders;
}
public void setOrders(Set<Order> orders) {
this.orders = orders;
}
}
复制代码
可以看到,我们借助Ant生成的POJO完全没问题。看看数据库Schema:
create table customer (
id varchar(255) not null,
username varchar(255),
balance integer,
primary key (id)
);
create table orders (
id varchar(255) not null,
orderNumber varchar(255),
cost integer,
customer_id varchar(255),
primary key (id)
);
alter table orders
add index FKC3DF62E51E0069B1 (customer_id),
add constraint FKC3DF62E51E0069B1
foreign key (customer_id)
references customer (id);
来自:http://www.cnblogs.com/beliefbetrayal/archive/2012/02/08/2342137.html
分享到:
相关推荐
Ant打包和Hibernate文件生成实例,Ant打包和Hibernate文件生成实例
android使用ant自动构建,打包,更多参考文章 http://www.67tgb.com/?p=629
博文链接:https://liudaoru.iteye.com/blog/221684
Ant自动打包用的.
ANT使用指南ANT使用指南ANT使用指南ANT使用指南ANT使用指南ANT使用指南ANT使用指南ANT使用指南ANT使用指南
ant 使用入门ant 使用入门ant 使用入门ant 使用入门
利用ant,自动化发布工程。具体过程包括:备份之前发布的工程,从svn checkout工程,编译工程,打包工程,关闭web服务器,删除已发布在web服务器的工程,复制打包的工程到web服务器,解压工程,启动web服务器。
利用hibernate-Tools里的相关工具类,使得java实体类(POJO)、hbm映射文件、数据库表(Schema)之间可以相互转化。也就是说,只要有其中一样,就可以通过各种途径得到其它两样。如果手里已经有了其中一样东西,要想...
一步步教你实现Ant工具和Junit工具的结合
使用起来更很方便,能够快速的找到自己想要的工具
ant 通过编写脚本就能进行java工程的自动发布
自动化测试系列一 ANT ANT自动化测试,包含ANT脚本
使用ant打war包使用ant打war包使用ant打war包使用ant打war包使用ant打war包使用ant打war包
ant自动打包流程: 1.生成keystore, 签名时要用到. 可参考http://www.cnblogs.com/feisky/archive/2010/01/17/1650076.html 2.更改localproperties中的sdk.dir, 为你机器上环境的安装路径 3.更改ant下的路径, 可参考...
NULL 博文链接:https://corejava2008.iteye.com/blog/859701
直接运行Ant 博文链接:https://spoony.iteye.com/blog/119344
文档详细记录了jenkins+ant+jmeter自动化接口测试框架搭建的过程步骤,供大家参阅。
NULL 博文链接:https://wenzongliang.iteye.com/blog/2179121
ant工具使用指南ant工具使用指南ant工具使用指南
Jmeter+ant自动生成测试报告的模板,测试报告中增加TPS(Throughput)和90% Line等指标;