上篇博客已經把Hibernate概念和其中的核心介面介紹,下面舉個實例添加用戶來介紹Hibernate如何使用.
1.創建普通的java項目.
因為Hibernate是一個輕量級的框架,不像servlet,還必須需要tomcat的支持,Hibernate只要jdk支持即可.
2.引入jar包.
可以在項目中直接引入jar包,在:項目--->屬性--->然後如下圖:
另一種辦法就是引入庫,相當於一個文件夾,把所有的jar包放到自己新建的文件夾中.在:窗體-->選項-->然後如下圖:
3.提供Hibernate的配置文件.hibernate.cfg.xml文件.完成相應的配置.
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_first</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">bjpowernode</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
</session-factory>
</hibernate-configuration>
在這裡連接mysql資料庫,解釋一下上面的標籤.按照順序來依次解釋:第一個是連接mySql的驅動;第二個是連接的url;url後面的hibernate_first是資料庫名字;第三個是和第四個分別是用戶名和密碼.第五個是方言.因為 hibernate對資料庫封裝,對不同的資料庫翻譯成不同的形式,比如drp中的分頁,若是使用oracle資料庫,則翻譯成sql語句三層嵌套.若是使用mySql資料庫,則翻譯成limit語句.
4.建立實體User類:
package com.bjpowernode.hibernate;
import java.util.Date;
public class User {
private String id;
private String name;
private String password;
private Date createTime;
private Date expireTime;
public String getId() {
return id;
}
public void setId(String id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public Date getCreateTime()
{
return createTime;
}
public void setCreateTime(Date createTime)
{
this.createTime = createTime;
}
public Date getExpireTime()
{
return expireTime;
}
public void setExpireTime(Date expireTime)
{
this.expireTime = expireTime;
}
}
5.建立User.hbm.xml,此文件用來完成對象與資料庫表的欄位的映射.也就是實體類的那些欄位需要映射到資料庫表中呢.
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping>
<class name="com.bjpowernode.hibernate.User">
<id name="id">
<generator class="uuid"/>
</id>
<property name="name"/>
<property name="password"/>
<property name="createTime"/>
<property name="expireTime"/>
</class> </hibernate-mapping>
6.我們也映射完畢了,但是hibernate怎麼知道我們映射完了呢,以及如何映射的呢?這就需要我們把我們自己的映射文件告訴hibernate,即:在hibernate.cfg.xml配置我們的映射文件.
<mapping resource="com/bjpowernode/hibernate/User.hbm.xml"/>
7.生成資料庫表.大家也看到了我們上述還沒有新建數據表呢,在第三步我們只是新建了資料庫而已.按照我們普通的做法,我們應該新建數據表啊,否則實體存放何處啊.這個別急,資料庫表這個肯定是需要有的,這個毋庸置疑,但是這個可不像我們原來需要自己親自動手建立哦,現在hibernate需要幫我們實現哦,如何實現嗯,hibernate會根據配置文件hibernate.cfg.xml和我們的映射文件User.hbm.xml會自動給我們生成相應的表,並且這個表的名字也給我們取好:默認是User.那如何生成表呢?
//默認讀取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
export.create(true, true);
8.那我們就開始進行操作啦,我們添加一個用戶對象,看看hibernate是如何添加的呢?跟我們以前的做法有什麼不同呢?
public class Client {
public static void main(String[] args)
{
//讀取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure();
//建立SessionFactory
SessionFactory factory = cfg.buildSessionFactory();
//取得session
Session session = null;
try {
session = factory.openSession();
//開啟事務
session.beginTransaction();
User user = new User();
user.setName("張三");
user.setPassword("123");
user.setCreateTime(new Date());
user.setExpireTime(new Date());
//保存User對象
session.save(user);
//提交事務
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
//回滾事務
session.getTransaction().rollback();
}finally {
if (session != null)
{
if (session.isOpen())
{
//關閉session
session.close();
}
}
}
}
}
第八步,我們可以看到,沒有我們熟悉的insert into表的sql語句了,那怎麼添加進去的呢,到底添加了沒?讓我真實滴告訴你,確實添加進去了,不信的,可以自己嘗試哦,這也是hibernate的優點,對jdbc封裝的徹底,減少了我們對數據的操作時間哈.
這就是真切滴介紹了hibernate的基本用法,其中好多優點等待我們自己去發現哦,比如hibernate中的緩存機制,映射方案哦.
[火星人
]
八步詳解Hibernate的搭建及使用已經有765次圍觀
http://coctec.com/docs/java/show-post-59818.html