posts - 11, comments - 0, trackbacks - 0, articles - 0

          HIbernate基礎

          Posted on 2011-03-21 20:42 HsiangYu 閱讀(226) 評論(0)  編輯  收藏 所屬分類: Hibernate

              為什么要使用hibernate?舉一個例子:如果你的表中有100個字段,使用JDBC去插入記錄、更新記錄這是非常恐怖的。如果插入或更新記錄出錯了呢?一個一個字段的對?想都不敢想,如果以后數據庫字段要全部添加后綴名呢!~~哇哇哇,受不了了。Okhibernate可以很好的解決JDBC的“漏洞”。

           

                   這個配置文件我把它看成是hibernate的核心!

           

                   下面我來舉個簡單的例子,使用hibernate對一個DOMAIN對象進行自動存取。

           

          1.定義一個Customerdomain,從學習數據庫至今使用顧客與訂單做的講解比較多,因為這種邏輯關系可以很好的闡明多表的關系,我們目前只使用一個表:

          public class Customer {

              private int id;

              private String name;

              private int gender;

              private Date birthday;

              private String email;

              private String phone;

              private String address;

          //下面是gettersetter方法

          }

           

          2.Customer的類路徑下添加hibernate的映射文件(元數據文件),映射文件名為Customer.hbm.xml,這樣在調用“ConfigurationObj. .addClass(Customer.class);hibernate框架可以自動加載它。

          <?xml version="1.0" encoding="UTF-8"?>

          <!DOCTYPE hibernate-mapping PUBLIC

              "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

              "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

           

          <hibernate-mapping>

              <!--

              1.name 指定類名

              2.table 指定表名,如果忽略則默認為類名

               -->

              <class name="cn.itcast.cc.hibernate.persistence.Customer"table="customers" lazy="false">

                 <!--

                 1.name OID的標識名

                 2.column 對應表的主鍵

                 3.type hibernate中的類型,對應JAVA類型為java.lang.Integer/int,對應SQL類型為INTEGER

                  -->

                 <id name="id" column="id" type="integer">

                     <!--

                     1.class 設置id字段為自增、主鍵

                      -->

                     <generator class="increment"></generator>

                 </id>

                 <!--

                 1.name 對應類的屬性

                 2.column 對應表的字段,如果不指定access。此處的name只為hibernate獲取讀取字段的方法。

                     比如name="abcde"hibernate拼出getAbcdesetAbcde,而此時我們可以添加這兩個方法,用于實現特殊功能。

                 3.type hibernate中的類型

                 4.access hibernate直接訪問字段,而不通過gettersetter

                  -->

                 <property name="name" column="name" type="string"access="field"/>

                 <property name="gender" column="gender" type="integer" />

                 <property name="birthday" column="birthday" type="date" />

                 <property name="phone" column="phone" type="string" />

                 <property name="email" column="email" type="string" />

                 <property name="address" column="address" type="string" />

              </class>

          </hibernate-mapping>

           

          3.hibernate的屬性文件,使用時請將里面的中文行全部刪除:

          hibernate.connection.driver_class=com.mysql.jdbc.Driver

          hibernate.connection.url=jdbc:mysql://localhost:3306/test

          hibernate.connection.username=root

          hibernate.connection.password=root

          #hibernate使用的本地化特性,比如MySQLOracle的分頁技術不同。

          #hibernate會根據這項設置,自動完成分頁技術。

          hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

          #回顯SQL語句,hibernate根據元數據文件自動生成SQL語句并執行。

          hibernate.show_sql=true

          #嚴重注意,如果指定為可以自動創建表,那么每次運行hibernate都會刪除并重新創建表。

          hibernate.hbm2ddl.auto=create

           

           

          4.我們需要向工程中導入hibernate目錄下的jar文件。

                 1).hibernate3.jar.

          2).lib/required/*.jar

          3).3rd/*.jar

           

          5.編寫測試類:

          import java.util.Date;

          import org.hibernate.Session;

          import org.hibernate.SessionFactory;

          import org.hibernate.Transaction;

          import org.hibernate.cfg.Configuration;

          import cn.itcast.cc.hibernate.persistence.Customer;

           

          public class Main {

              private static SessionFactory sefac = null;

              // 當類被加載時,獲取SessionFactory

              static {

                 //創建Configuration時,默認加載hibernate.properties文件。

                 //也可以調用addResource方法,將配置文件加載進來。

                 Configuration config = new Configuration();

                 // ORM中的O在此添加到

                 config.addClass(Customer.class);

                 sefac = config.buildSessionFactory();

              }

                

              public static void main(String[] args) {

                 // 創建一個customer對象

                 Customer customer = new Customer();

                 customer.setName("changcheng");

                 customer.setGender(1);

                 customer.setBirthday(new Date());

                 customer.setPhone("13998689955");

                 customer.setEmail("changcheng@google.com");

                 customer.setAddress("China");

                 // 調用保存

                 save(customer);

                 // 調用查詢

                 find(1);

              }

             

              private static void save(Customer customer){

                 //獲取session相當于獲取了一個連接

                 Session session = sefac.openSession();

                 //此處必須使用事務,否則不能保存數據。

                 Transaction tr = session.beginTransaction();

                 //直接保存

                 session.save(customer);

                 //提交事務

                 tr.commit();

                 //關閉會話

                 session.close();

              }  

             

              private static void find(Integer id){

                 //獲取session相當于獲取了一個連接

                 Session session = sefac.openSession();

                 Transaction tra = session.beginTransaction();

                 //直接保存

                 Customer customer = (Customer) session.load(Customer.class, id);

                 tra.commit();

                 //關閉會話

                 session.close();

                 System.out.println(customer);

              }

          }

           

          上面是一個簡單的hibernate例子,今天也有學習hibernate的簡介與Java對象持久化概述。什么是持久化?就是將數據保存在外存里唄。比如硬盤、UHibernate就是從業務邏輯層中抽取出來的持久化層!

           

                   。多對一需要在class元素中添加一個“many-to-one”子元素,用于指定多對一的那個屬性的類型信息等。一對多需要在class元素中添加一個“set”子元素,用于指定一對多的那個set屬性的類型信息等。


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 和平区| 江源县| 武隆县| 无锡市| 武穴市| 肥西县| 唐海县| 会泽县| 措勤县| 米泉市| 宜黄县| 通江县| 永宁县| 平泉县| 繁峙县| 康保县| 恭城| 斗六市| 门源| 千阳县| 广水市| 嵊泗县| 体育| 师宗县| 县级市| 会理县| 金门县| 汉中市| 通山县| 三原县| 封丘县| 鹤庆县| 游戏| 胶州市| 远安县| 雷波县| 玛曲县| 翁牛特旗| 会泽县| 临沧市| 酒泉市|