因为学习java框架技术的原因,经常在网上下载一些ssh方面的代码来看,在这个过程中发现大部分朋友在分享的过程中没有将项目的sql文件一并拿出,这给自己的部署学习过程带来不少麻烦。相信这也是不少初学者都遇到过的小麻烦。没事的时候找找网上有不有现成的解决方案可用。发现利用hibernate内的“SchemaExport”就可以解决问题。现分享如下;
一、创建一个新项目用来存放所需要的jar包,配置文件,工具类
二、建立项目结构图如下:
-
三、配置
配置hibernate配置文件和log4j文件
四、工具类编写如下:
package hibernate2dll; import model.ProductTable; import org.hibernate.cfg.Configuration; import org.hibernate.tool.hbm2ddl.SchemaExport; class UserTest{ public static void main(String[] args) throws Exception{ //ProductTable //ProTypeTable //UserTable //配置环境,分析xml映射文件 Configuration conf= new Configuration() .addClass(ProductTable.class); //生成并输出sql到文件(当前目录)和数据库 SchemaExport dbExport=new SchemaExport(conf); dbExport.create(true, true); } }
五、运行项目