利用hibernate映射文件生成表

来源:互联网 发布:360浏览器优化版 编辑:程序博客网 时间:2024/06/17 09:44

还在为下载的的工程里没有数据库文件而苦恼吗?本文介绍的方法将解决你的这个苦恼

 

很简单:

 

产生Hibernate的SessionFactory类及hibernate.cfg.xml文件,然后手动写好Hibernate的映射文件(当然也可以是以前写好的,,在hibernate.cfg.xml中增加对应的mapping source,然后写一个如下的main方法运行即可。(注意:数据库中需得手动建好对应的库)

public static void main(String[] args) {
   Configuration con=new Configuration().configure(); //读取hibernate.cfb.xml配置信息,这里写路径
   SchemaExport export=new SchemaExport(con);     
   export.create(true, true);  //这句话建议不写  ,因为会将以前建好的表重新删除再建,数据也没有了,不写这句但必须就要在hibernate.cfg.xml文件中加 <pr//operty name="hbm2ddl.auto">update</property>
}

执行生成的是对应的sql,执行一下OK

原创粉丝点击