hibernate實戰筆記1---初探

來源:互聯網
上載者:User

由於在學習Spring的時候學到有關資料庫的章節的時候,提及到了hibernate的整合,但是我對hibernate技術幾乎是一點不瞭解,只是知道它是一個orm對象映射架構,所以在初探的章節做一下hibernate的簡單瞭解,以及應用。順便提一句,我用的是maven添加的hibernate開發包

簡單使用hibernate的一般步驟

*建立並且編輯hibernate設定檔

該設定檔主要是用於連結資料庫所用,定義了資料庫的驅動程式和對應檔的位置,如下是一個簡單的配置

<?xml version="1.0" encoding="utf-8" ?><!DOCTYPE hibernate-configuration    PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory ><property name="show_sql">true</property><property name="connection.driver_class">org.gjt.mm.mysql.Driver</property><property name="connection.url">jdbc:mysql://localhost:3306/pgwt</property><property name="connection.username">root</property><property name="connection.password">1234</property><property name="dialect">org.hibernate.dialect.MySQLDialect </property><mapping resource="com/springframework/hibernate/test.hbm.xml" /></session-factory></hibernate-configuration>

根據這個設定檔可以看出,所有的配置資訊都是包含在<hibernate-configureation>標籤之中,該設定檔和Spring的裝配bean差不多,<session-factory>對應一個SessionFactory對象,期中配置的是對象中的屬性,name代表屬性的名稱<property>標籤的內容就是屬性值了,逐一瞭解一下

show_sql屬性是一個boolean值,代表的是在進行資料庫操作的時候是否在控制台輸出sql語句,在調試的時候非常有用

connection.driver_class代表的是資料庫的jdbc驅動程式,我這裡使用的是mysql所以就是org.gjt.mm.mysql.Driver

connection.url代表的是訪問資料庫的url

connection.username 代表的是登入資料庫時候的使用者名稱

connection.password 代表的是登入資料庫時候的密碼

dialect代表的是一種資料庫的方言,可以為資料庫中的屬性設定一些預設值(我目前是這麼理解的,以後再深入研究一下)我知道了一個網頁內容是資料庫對應的方言的類,可以參考一下百度文庫地址

<mapping resource=""/>標籤是用來引用對應檔地址的,至於對應檔的內容,下文會做簡單的介紹


*建立資料庫表

簡單的建立一個user的資料庫表分別有userid,name,password欄位


*建立和資料庫表對應的javaBean類

如下是一個簡單的javaBean

package com.springframework.hibernate;public class User {private String userid;private String name;private String password;public String getUserid() {return userid;}public void setUserid(String userid) {this.userid = userid;}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;}}

*建立對應檔

對應檔中包括了一些配置資訊,代碼之後會逐一介紹一下配置資訊的含義,設定檔如下

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC    "-//Hibernate/Hibernate Mapping DTD//EN"    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping><class name="com.springframework.hibernate.User" table="user"><id name="userid" column="userid"><generator class="assigned" /></id><property name="name" column="name" /><property name="password" column="password" /></class></hibernate-mapping>


在上邊的資料庫表中包括三個欄位,期中userid是主鍵,然後我來逐一說一下設定檔的含義

所有的配置資訊都是寫在<hebernate-mapping>標籤中,期中可以包含多個<class>標籤,目的是為了配置javaBean與資料庫表之間的映射關係

<class>標籤中name屬性代表的是該class標籤對應的javaBean,table屬性代表該class標籤對應的資料庫表

<id> 標籤是用於映射主鍵的,user表的主鍵是userid ,name屬性是代表javaBean中屬性的名稱,column屬性代表的是資料庫表中的欄位

<generator>標籤定義了產生主鍵的方式,叫做產生器,我選擇的方式是assigned,是由程式自由產生主鍵,相對還有其他集中方式,我找了一篇blog裡面說的還挺詳細blog地址

<property>也是像Spring注入javaBean一樣,定義屬性所對應的資料庫表欄位的引用column屬性代表的是資料庫表中的欄位


*建立一個情境類(主函數)

代碼如下

package com.springframework.hibernate;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;public class Client {public static void main(String[] args) {try {SessionFactory sf = new Configuration().configure().buildSessionFactory();Session session = sf.openSession();Transaction tx = session.beginTransaction();for (int i = 0; i < 100; i++) {User newuser = new User();newuser.setUserid("userid" + i);newuser.setName("name" + i);newuser.setPassword("password" + i);session.save(newuser);}tx.commit();session.close();} catch (Exception e) {e.printStackTrace();}}}

這類似是一個樣板是代碼,應該是可以由Spring模板來替換的。

hibernate是通過SessionFactory來啟動hibernate(通過設定檔與資料連線),session對象當中包含一些資料庫操作的方法


以上是一些使用hibernate的基本方法,瞭解了以上的知識就可以繼續往下學習Spring了,以後再來完善有關hibernate的實戰筆記。


相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.