hibernate初學者的第一步

新建項目

學習建立user-library-hibernate,並加入相應的jar

  a.項目右鍵-build path-configure build path-add library

  b.選擇user-library,在其中新建library,命命爲hibernate

  c. 在該library中加入hibernate所需要的jar

      hibernate3.3.2 

      /hibernate3.jar

      /lib/required目錄下的所有包 6

      sl4j-nop jar

引入mysqlJDBC驅動包

MYSQL中建數據庫和相應的表student(id,name,age);

建立hibernate配置文件hibernate.cfg.xml

      參考文檔中COPY,修改對應的數據庫連接,

建立student

建立映射文件Student.hbm.xml 參考相應文檔

將映射文件加到hibernate-cfg.xml

     搭建日誌環境並配置顯示DDL語句

     slf的實現:slf4j nodep ,log4j ,jdk logging api ,apache common-log.

     slf4j.nop.jarslf-api.jar其相應的接口實現

     把slf的接口對到log4j的實現,user libraryhibernate,slf的實現slf4j-nop-1.5.8.jar去掉,添加log4j的實現log4j-1.2.15.jar,再添加一個slf-apilog4j轉換器slf4j-log4j12-1.5.8.jar.

     把slf的接口轉換成log4j的接口.最後添加一個log4j的配置文件log4j.properties

     利用HIBERNATE導出數據庫建表

     //讀取配置文件hibernate.cfg.xml

     Configuration cfg=new AnnotationConfiguration().configure();(有註解時使用AnnotationConfiguration),configure()可以手動指定配置文件名稱.

     Configuration cfg=new Configuration(),會默認讀取hibernate.properties文件

      //創建schemaExport對象

      import org.hibernate.tool.hbm2ddl.SchemaExport;

      SchemaExport export=new SchemaExport(cfg);

      //創建數據庫表

      export.create(true, true);

強烈建議初學者嘗試着去敲代碼。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章