1 新建項目
2 學習建立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
3 引入mysql的JDBC驅動包
4 在MYSQL中建數據庫和相應的表student(id,name,age);
5 建立hibernate配置文件hibernate.cfg.xml
參考文檔中COPY,修改對應的數據庫連接,
6 建立student類
7 建立映射文件Student.hbm.xml 參考相應文檔
8 將映射文件加到hibernate-cfg.xml中
搭建日誌環境並配置顯示DDL語句
slf的實現:slf4j nodep ,log4j ,jdk logging api ,apache common-log.
slf4j.nop.jar是slf-api.jar其相應的接口實現
把slf的接口對到log4j的實現,在user library中hibernate,把slf的實現slf4j-nop-1.5.8.jar去掉,添加log4j的實現log4j-1.2.15.jar,再添加一個slf-api和log4j轉換器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);
強烈建議初學者嘗試着去敲代碼。