之前都是用最蠢的方法:SQL建立数据库相关的表,然后再Java写映射写配置文件.....
实际上使用Hibernate自带的工具hbm2ddl,建立根据你的对象建立数据库是很惬意的一件事;)
首先当然要建好POJO object, XML Mapping File(也可以使用工具根据POJO class建立),配置文件(hibernate.cfg.xml)
然后运行下面的Java代码
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class SchemaUtil { public static void main(String[] args) {
实际上使用Hibernate自带的工具hbm2ddl,建立根据你的对象建立数据库是很惬意的一件事;)
首先当然要建好POJO object, XML Mapping File(也可以使用工具根据POJO class建立),配置文件(hibernate.cfg.xml)
然后运行下面的Java代码
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class SchemaUtil { public static void main(String[] args) {
Configuration cfg = new Configuration().configure();
SchemaExport schemaExport= new SchemaExport(cfg);
schemaExport.create(false, true);
SchemaExport schemaExport= new SchemaExport(cfg);
schemaExport.create(false, true);
}
}
本文介绍了一种利用Hibernate的hbm2ddl工具简化数据库创建过程的方法。通过简单的Java代码即可根据对象模型自动生成数据库表结构。
1万+

被折叠的 条评论
为什么被折叠?



