利用hibernate自动生成数据表的方法

来源:互联网 发布:美军激光 知乎 编辑:程序博客网 时间:2024/06/05 16:11
package com.hst.mapping;

import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;



public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        
        /**
         * 有hibernate.hbm.xml文件时,用该方法
         */
        //默认读取hibernate.cfg.xml文件
        Configuration configuration = new Configuration().configure();
        //生产并输出sql到当前目录和数据库。
        SchemaExport schemaExport = new SchemaExport(configuration);
        //创建表结构,第一个true表示在控制台打印SQL语句,第二个true表示导入sql到数据库
        schemaExport.create(true, true);
        
        /**
         * 注解时用该方法自动生成数据库
         */
        //AnnotationConfiguration
        AnnotationConfiguration cfg=new AnnotationConfiguration().configure();
        SchemaExport export=new SchemaExport(cfg);
        export.create(true, true);
    }

}

0 0