java代码生成数据的schema_利用ant,hibernate的schemaexport自动生成数据库

先用eclipse新建一个项目student作为示范

新建Student类

如下:

package student;

public class Student {

private Long id;

private String name;

private String sex;

private String email;

public String getEmail() {

return email;

}

public void setEmail(String email) {

this.email = email;

}

public Long getId() {

return id;

}

public void setId(Long id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getSex() {

return sex;

}

public void setSex(String sex) {

this.sex = sex;

}

}

接下来编写ant任务生成数据库文件

注意:

我把eclipse放在了D盘,即D:/eclipse,eclipse对应的workspace也在D盘,D:/workspace

我把所有的lib及其依赖包都放在D:/workspace/lib,这些jar文件当然都包括hibernate3.jar还有其依赖包(里面还有其他的东西,例如spring的jar还有xdoclet的jar,这里用不到就不涉及了)

在D:/workspace/student项目目录下新建一个hibernate-build.xml用于书写ant任务,文件目录如下:D:/workspace/student/hibernate-build.xml

当然,eclipse 项目的文件我是分成两个目录的,即src(存储原文件,后缀为.java)和bin(存储编译过的文件,后缀为.class)

目录D:/workspace/student/src/Student.java

D:/workspace/student/bin/Student.class

4f1150b881333f12a311ae9ef34da474.png4f1150b881333f12a311ae9ef34da474.png4f1150b881333f12a311ae9ef34da474.png4f1150b881333f12a311ae9ef34da474.png4f1150b881333f12a311ae9ef34da474.png    

4f1150b881333f12a311ae9ef34da474.png4f1150b881333f12a311ae9ef34da474.png4f1150b881333f12a311ae9ef34da474.png4f1150b881333f12a311ae9ef34da474.png4f1150b881333f12a311ae9ef34da474.png4f1150b881333f12a311ae9ef34da474.png    

4f1150b881333f12a311ae9ef34da474.png4f1150b881333f12a311ae9ef34da474.png4f1150b881333f12a311ae9ef34da474.png4f1150b881333f12a311ae9ef34da474.png    

4f1150b881333f12a311ae9ef34da474.png4f1150b881333f12a311ae9ef34da474.png4f1150b881333f12a311ae9ef34da474.png4f1150b881333f12a311ae9ef34da474.png4f1150b881333f12a311ae9ef34da474.png

4f1150b881333f12a311ae9ef34da474.png

4f1150b881333f12a311ae9ef34da474.png

4f1150b881333f12a311ae9ef34da474.png4f1150b881333f12a311ae9ef34da474.png4f1150b881333f12a311ae9ef34da474.png4f1150b881333f12a311ae9ef34da474.png4f1150b881333f12a311ae9ef34da474.png4f1150b881333f12a311ae9ef34da474.png4f1150b881333f12a311ae9ef34da474.png

如上,看到quiet="false" text="true" delimiter=";" 了吗

还是要解释一下

quiet=t"rue"   不要把脚本输出到stdout

drop="true"    只进行drop tables的步骤

text="true"      不执行在数据库中运行的步骤

output="database.sql"   把生成的脚本输出到database.sql文件中

config="hibernate,cfg.xml"   从xml文件读取hibernate的配置

properties="hibernate.properties"    从文件读入数据库属性

delemiter=";"   为脚本设置行结束符

注意运行的classpath要包括所有用到的jar还有src源文件(之前弄了多次都不成功,,原因就是没有加入源文件)

成功之后就会看到在 D:\workspace\student下生成了database.sql文件

下次讲在xdoclet下生成 hbm.xml和hibernate.cfg.xml文件

posted on 2007-04-08 10:17 狼 阅读(2204) 评论(0)  编辑  收藏 所属分类: hibernate学习笔记

你可能感兴趣的:(java代码生成数据的schema_利用ant,hibernate的schemaexport自动生成数据库)