一帐我、Hibernate概述
hibernate核心內(nèi)容是ORM(關(guān)系對象模型)〔聊遥可以將對象自動的生成數(shù)據(jù)庫中的信息,使得開發(fā)更加的面向?qū)ο笞彀臁_@樣作為程序員就可以使用面向?qū)ο蟮乃枷雭聿僮?a target="_blank" rel="nofollow">數(shù)據(jù)庫瞬场,而不用關(guān)心繁瑣的JDBC。所以涧郊,hibernate處于三層架構(gòu)中的D層(持久層)贯被。
1、Hibernate可以使用在Java的任何項目中妆艘,不一定非要使用在Javaweb項目中彤灶。因為Hibernate不需要類似于tomact這些容器的支持,可以直接通過一個main方法進(jìn)行測試批旺。
2枢希、通過下面的實例,可以發(fā)現(xiàn)使用Hibernate可以大大減少代碼量朱沃。
3苞轿、由于使用了Hibernate茅诱,代碼中不涉及具體的JDBC語句,所以就方便了代碼的可移植性搬卒。
二瑟俭、Hibernate開發(fā)的環(huán)境搭建
(一)Hibernate的環(huán)境搭建非常簡單,只需要引入Hibernate核心包(單擊下載)以及Hibernate依賴包(單擊下載)即可契邀。
(二)加入數(shù)據(jù)庫驅(qū)動摆寄。下面的例子中主要是采用Mysql數(shù)據(jù)庫來演示的,所以在這里引入MysqL的JDBC驅(qū)動(點(diǎn)擊下載)坯门。
(三)提供核心配置文件hibernate.cfg.xml文件(在src文件夾下即可)微饥。其中的配置如下(針對mysql)
[html]view plaincopy
"-//Hibernate/Hibernate?Configuration?DTD?3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/hibernate_first
root
root
org.hibernate.dialect.MySQLDialect
該實例的目錄結(jié)構(gòu)如下
說明:最后一個HIBERNATE3里面包含了所有的需要引用的jar包
1古戴、新建一個普通的java項目欠橘,按照上面的步驟引入相關(guān)的jar包和配置文件
2、建立User實體類
[java]view plaincopy
importjava.util.Date;
publicclassUser?{
privateString?id;
privateString?username;
privateString?password;
privateDate?createTime;
privateDate?expireTime;
publicString?getId()?{
returnid;
}
publicvoidsetId(String?id)?{
this.id?=?id;
}
publicString?getUsername()?{
returnusername;
}
publicvoidsetUsername(String?userName)?{
this.username?=?userName;
}
publicString?getPassword()?{
returnpassword;
}
publicvoidsetPassword(String?password)?{
this.password?=?password;
}
publicDate?getCreateTime()?{
returncreateTime;
}
publicvoidsetCreateTime(Date?createTime)?{
this.createTime?=?createTime;
}
publicDate?getExpireTime()?{
returnexpireTime;
}
publicvoidsetExpireTime(Date?expireTime)?{
this.expireTime?=?expireTime;
}
}
2现恼、提供User.hbm.xml文件肃续,完成實體類的映射
[html]view plaincopy
"-//Hibernate/Hibernate?Mapping?DTD?3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
其中的property標(biāo)簽是將要生成是數(shù)據(jù)庫表中的字段,在這里不用關(guān)心各個字段是什么類型的叉袍。因為Hibernate會根據(jù)上面的實體類中屬性的類型來決定將來表中字段的類型
3始锚、配置hibernate.cfg.xml文件
[html]view plaincopy
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/hibernate_first
root
root
org.hibernate.dialect.MySQLDialect
注意:必須是“/”而不能是“.”。
4喳逛、生成表:編寫工具類ExoprtDB.java,將hbm生成ddl
[java]view plaincopy
importorg.hibernate.cfg.Configuration;
importorg.hibernate.tool.hbm2ddl.SchemaExport;
/**
*?將hbm生成ddl
*?@author?BCH
*
*/
publicclassExoprtDB?{
publicstaticvoidmain(String[]?args)?{
//默認(rèn)讀取hibernate.cfg.xml文件
Configuration?cfr?=newConfiguration().configure();
SchemaExport?export?=newSchemaExport(cfr);
export.create(true,true);
}
}
到這里就可以生成User表了瞧捌,但是如果直接運(yùn)行ExoprtDB.java文件是不能生成User表的。因為在mysql數(shù)據(jù)中還沒有建立數(shù)據(jù)庫Hibernate-first润文。所以在mysql控制臺中通過create database hibernate-first; use hibernate-first;之后再執(zhí)行ExoprtDB.java文件就可以生成表了察郁。
5、向表中添加數(shù)據(jù)
[java]view plaincopy
importjava.util.Date;
importorg.hibernate.Session;
importorg.hibernate.SessionFactory;
importorg.hibernate.cfg.Configuration;
publicclassClient?{
publicstaticvoidmain(String[]?args)?{
//讀取配置文件
Configuration?cfg?=newConfiguration().configure();
SessionFactory?factory?=?cfg.buildSessionFactory();
Session?session?=null;
try{
session?=?factory.openSession();
//開啟事務(wù)
session.beginTransaction();
User?user?=newUser();
user.setUsername("用戶名");
user.setPassword("123");
user.setCreateTime(newDate());
user.setExpireTime(newDate());
session.save(user);
//提交事務(wù)
session.getTransaction().commit();
}catch(Exception?e){
e.printStackTrace();
//回滾事務(wù)
session.getTransaction().rollback();
}finally{
if(session?!=null){
if(session.isOpen()){
//關(guān)閉session
session.close();
}
執(zhí)行該java文件就可以完成向表中增加數(shù)據(jù)了,效果如下
通過上面的代碼我們可以看出转唉,在代碼中沒有涉及到任何有關(guān)JDBC的代碼皮钠,作為開發(fā)人員只需要寫好相應(yīng)的實體類,然后通過配置就可以實現(xiàn)了表的建立以及向表中實現(xiàn)數(shù)據(jù)的插入赠法。
在代碼中有許多Hibernate的核心對象麦轰,例如Configuration、SessionFactory砖织、Session等等款侵。這些內(nèi)容將在以后介紹.
更多詳細(xì)源碼參考來源:http://minglisoft.cn/technology歡迎大家一起學(xué)習(xí)研究相關(guān)技術(shù),源碼獲取請加求求(企鵝):2042849237