使用 SchemaExport 自动建表

之前都是用最蠢的方法: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) {        Configuration cfg = new Configuration().configure();
        SchemaExport schemaExport= new SchemaExport(cfg);
        schemaExport.create(false, true);
    }
}

你可能感兴趣的:(java,sql,Hibernate,xml)