八步詳解Hibernate的搭建及使用
上篇博客已經把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"
- "http://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中的緩存機制,映射方案哦。
原文鏈接:http://blog.csdn.net/llhhyy1989/article/details/7299619
【編輯推薦】