<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 70,comments - 408,trackbacks - 0

          冬眠溫暖,"冬天"快樂,一夜的冬雨在靜靜中把冬衣輕輕換上,Hibernate,就東面一樣,在程序"冬天"里給我們帶來一絲溫暖,Hibernate的名字真的很優(yōu)美,當(dāng)我曾經(jīng)第一天知道有一種技術(shù)叫"冬眠".我就深深的喜歡上了這種技術(shù).所以產(chǎn)生了一種想法,把這種技術(shù)以最簡(jiǎn)單明了的方式記錄自己的Blog上,一方面能幫助一些剛剛學(xué)習(xí)它的朋友,另一方面也能讓自己對(duì)Hibernate的理解更加深刻.希望大家多多支持我,多光臨我的Blog.好了下面言歸正傳,Hibernate是JAVA應(yīng)用和關(guān)系型數(shù)據(jù)庫中間的一座橋梁.它負(fù)責(zé)把JAVA對(duì)象和關(guān)系型數(shù)據(jù)庫映射在一起,其實(shí)簡(jiǎn)單的理解Hibernate可以理解成一種中間件,向下封裝了JDBC對(duì)數(shù)據(jù)庫的操作,向上提供了面向?qū)ο蟮臄?shù)據(jù)庫訪問API.今天我們不談理論,往往學(xué)習(xí)一種技術(shù)進(jìn)來就談一些很深的理論會(huì)讓人學(xué)的暈頭轉(zhuǎn)向,我的學(xué)習(xí)方式向來都是先搞定一個(gè)簡(jiǎn)單明了的小例子,然后在回頭看理論,到時(shí)候總能有事半功倍的感覺.簡(jiǎn)單的說一下Hibernate的流程:
          1)創(chuàng)建Hibernate配置文件
          2)創(chuàng)建持久化類
          3)創(chuàng)建對(duì)象和關(guān)系型數(shù)據(jù)庫的映射文件
          4)通過Hibernate的API訪問數(shù)據(jù)庫

          好了.既然流程知道了.我們就一步一步來做吧,首先創(chuàng)建一個(gè)JAVA工程(JAVA工程和WEB工程相比,更加簡(jiǎn)單直觀),然后我們引入必須要有的包,hibernate2包,log4j的包,dom4j的包,MSSQL的3個(gè)驅(qū)動(dòng)包,然后創(chuàng)建Hibernate的配置文件,和其他的properties一樣,都是以鍵=值的形式表示的.當(dāng)然你也可以選擇用XML的形式來創(chuàng)建hibernate的配置文件,這里用properties方式做演示,名字叫hibernate.properties.為了理解起來容易些,我們使用最簡(jiǎn)單的數(shù)據(jù)庫MS SQL Server 2000:

    首先要設(shè)置方言,方言的意思就是不同的數(shù)據(jù)庫,有不同的語言規(guī)范,就跟我們現(xiàn)實(shí)中的北京話于廣東話的差距一樣.
    hibernate.dialect=net.sf.hibernate.dialect.SQLServerDialect
    然后設(shè)置數(shù)據(jù)庫的驅(qū)動(dòng)類.
    hibernate.connection.driver_class=com.microsoft.jdbc.sqlserver.SQLServerDriver
    設(shè)置數(shù)據(jù)庫的路徑
    hibernate.connection.url=jdbc:sqlserver://127.0.0.1:1433;charset=GBK;selectMethod=cursor;databaseName=TEMPDB
    數(shù)據(jù)庫登錄名
    hibernate.connection.username=sa
    數(shù)據(jù)庫密碼
    hibernate.connection.password=555
    最后設(shè)置的屬性是是否在控制臺(tái)打印SQL語句,這里我們?cè)O(shè)置為true,因?yàn)榉奖阏{(diào)試,默認(rèn)是false
    hibernate.show_sql=true
    備注:在Hibernate軟件包的etc目錄下,有一個(gè)hibernate.properties文件,里邊有各種關(guān)系型數(shù)據(jù)庫的配置樣例.

          OK,配置文件創(chuàng)建完了.就是這么簡(jiǎn)單,現(xiàn)在我們來創(chuàng)建一個(gè)持久化類.其實(shí)說白了持久化類超級(jí)簡(jiǎn)單,就是我們?cè)?jīng)用的JAVABEAN嘛,寫幾個(gè)字段Eclipse都能自動(dòng)生成相關(guān)的方法,不過Hibernate的持久化類要 implements一個(gè)接口,就是Sreializable接口.下面是持久化類的例子,這里省略了大多數(shù)的get,set方法,到時(shí)候自己加上就OK了.
    public class Customer implements Serializable {
      private Long id;
      private String name;
      private String email;
      private String password;
      private int phone;
      public Long getId(){
        return id;
      }
      public void setId(Long id){
        this.id = id;
      }
      ........
    }

          OK持久化類也一樣簡(jiǎn)單,下面我們要做的事情更簡(jiǎn)單,就是到我們的數(shù)據(jù)庫中,創(chuàng)建一個(gè)和持久化類字段對(duì)應(yīng)的表.例子如下:
    create database TEMPDB;

    create table CUSTOMERS (
      ID bigint not null primary key,
      NAME varchar(15) not null,
      EMAIL varchar(128) not null,
      PASSWORD varchar(8) not null, 
      PHONE  int
    )

          數(shù)據(jù)庫搞定了.下面來完成一件比較重要的事情,就是創(chuàng)建對(duì)象-關(guān)系映射文件.,Hibernate采用XML格式的文件來指定對(duì)象和關(guān)系型數(shù)據(jù)庫之間的映射,在運(yùn)行的時(shí)候Hibernate將根據(jù)這個(gè)映射文件來生成各種SQL語句.下面我們創(chuàng)建一個(gè)Customer.hbm.xml的文件,這個(gè)文件和Customer.class放在同一個(gè)目錄下.
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "
    <hibernate-mapping>
      <class name="com.Customer" table="CUSTOMERS">
        <id name="id" column="ID" type="long">
          <generator class="increment"/>
        </id>
        <property name="name"  column="NAME"  type="string" not-null="true" /> 
        <property name="email"     column="EMAIL"     type="string" not-null="true" />
        <property name="password"  column="PASSWORD"  type="string" not-null="true"/>
        <property name="phone"     column="PHONE"     type="int" /> 
      </class>
    </hibernate-mapping>

          首先<class 標(biāo)簽的name屬性指定的是類名,table屬性指定的和這個(gè)類映射的表名.<id 標(biāo)簽的<generator 子標(biāo)簽指定對(duì)象標(biāo)識(shí)符生成器,他負(fù)責(zé)在對(duì)象里產(chǎn)生表的唯一標(biāo)識(shí)符.<property標(biāo)簽的name屬性指定持久化類中屬性的名字,clumn屬性顧名思義,列的意思,這里是指定了數(shù)據(jù)庫中的字段.type屬性指定Hibernate的映射類型,Hibernate映射類型是JAVA類型和SQL類型之間的橋梁,先簡(jiǎn)單的知道JAVA里的String和數(shù)據(jù)庫里的varchar這里配置為string,而JAVA里的int和數(shù)據(jù)庫里的int配置為int.還有N多我們以后深入使用Hibernate的時(shí)候在深入的了解就OK了.現(xiàn)在知道簡(jiǎn)單的足以.最后說一下<property標(biāo)簽的not-null屬性是設(shè)置這個(gè)字段是否為空.值得我們關(guān)注的是,在實(shí)際的項(xiàng)目開發(fā)中,我們不應(yīng)該以來Hibernate或者數(shù)據(jù)庫來做數(shù)據(jù)的驗(yàn)證.全部在業(yè)務(wù)羅基層處理好就OK了.下面說一下Hibernate采用XML文件來配置對(duì)象-關(guān)系映射的優(yōu)點(diǎn):
       1)Hibernate既不會(huì)滲透到上層的業(yè)務(wù)模型中,也不會(huì)泄漏到下層的數(shù)據(jù)模型中
       2)軟件開發(fā)人員可以獨(dú)立設(shè)計(jì)業(yè)務(wù)模型,數(shù)據(jù)庫設(shè)計(jì)人員可以獨(dú)立設(shè)計(jì)數(shù)據(jù)模型,互相不受拘束.
       2)對(duì)象-關(guān)系映射不依賴于任何代碼,提高了程序的靈活性,使得維護(hù)更加方便.

          這里說一下映射文件的類型定義DTD,在我們的Customer.hbm.xml中,文件開頭就聲明了DTD類型,DTD的意思就是對(duì)XML文件的語法做定義,在實(shí)際編程中,我們引入正確的DTD規(guī)范,讓我們編寫XML非常方便.每一種XML文件都有獨(dú)自的DTD文件.Hibernate的DTD文件下載地址是:
    http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd,在Hibernate的軟件包src\net\sf\hibernate目錄下也提供了hibernate-mapping-2.0.dtd文件.

          
    下面看一個(gè)重點(diǎn)吧,通過Hibernate的API操作數(shù)據(jù)庫:
    public class BusinessService{
      public static SessionFactory sessionFactory;
      static{
        try{
          // Create a configuration based on the properties file we've put
          // in the standard place.
          Configuration config = new Configuration();
          config.addClass(Customer.class);
          // Get the session factory we can use for persistence
          sessionFactory = config.buildSessionFactory();
        }catch(Exception e){e.printStackTrace();}
      }

      public void findAllCustomers(ServletContext context,OutputStream out) throws Exception{
        // Ask for a session using the JDBC information we've configured
        Session session = sessionFactory.openSession();
        Transaction tx = null;
        try {
          tx = session.beginTransaction();
          List customers=session.find("from Customer as c order by c.name asc");
          for (Iterator it = customers.iterator(); it.hasNext();) {
             printCustomer(context,out,(Customer) it.next());
          }

          // We're done; make our changes permanent
          tx.commit();

        }catch (Exception e) {
          if (tx != null) {
            // Something went wrong; discard all partial changes
            tx.rollback();
          }
          throw e;
        } finally {
          // No matter what, close the session
          session.close();
        }
      }
      public void saveCustomer(Customer customer) throws Exception{
         // Ask for a session using the JDBC information we've configured
        Session session = sessionFactory.openSession();
        Transaction tx = null;
        try {
          tx = session.beginTransaction();
          session.save(customer);
          // We're done; make our changes permanent
          tx.commit();

        }catch (Exception e) {
          if (tx != null) {
            // Something went wrong; discard all partial changes
            tx.rollback();
          }
          throw e;
        } finally {
          // No matter what, close the session
          session.close();
        }
      }

      public void loadAndUpdateCustomer(Long customer_id,String address) throws Exception{
        // Ask for a session using the JDBC information we've configured
        Session session = sessionFactory.openSession();
        Transaction tx = null;
        try {
          tx = session.beginTransaction();

          Customer c=(Customer)session.load(Customer.class,customer_id);
          c.setAddress(address);
          // We're done; make our changes permanent
          tx.commit();

        }catch (Exception e) {
          if (tx != null) {
            // Something went wrong; discard all partial changes
            tx.rollback();
          }
          throw e;
        } finally {
          // No matter what, close the session
          session.close();
        }
      }
      public void deleteAllCustomers() throws Exception{
        // Ask for a session using the JDBC information we've configured
        Session session = sessionFactory.openSession();
        Transaction tx = null;
        try {
          tx = session.beginTransaction();
          session.delete("from Customer as c");
          // We're done; make our changes permanent
          tx.commit();

        }catch (Exception e) {
          if (tx != null) {
            // Something went wrong; discard all partial changes
            tx.rollback();
          }
          throw e;
        } finally {
          // No matter what, close the session
          session.close();
        }
      }

      private void printCustomer(ServletContext context,OutputStream out,Customer customer)throws Exception{
       if(out instanceof ServletOutputStream)
               printCustomer(context,(ServletOutputStream) out,customer);
           else
              printCustomer((PrintStream) out,customer);
      }
      private void printCustomer(PrintStream out,Customer customer)throws Exception{
       //save photo
        byte[] buffer=customer.getImage();
        FileOutputStream fout=new FileOutputStream("photo_copy.gif");
        fout.write(buffer);
        fout.close();

        out.println("------以下是"+customer.getName()+"的個(gè)人信息------");
        out.println("ID: "+customer.getId());
        out.println("口令: "+customer.getPassword());
        out.println("E-Mail: "+customer.getEmail());
        out.println("電話: "+customer.getPhone());

      }
      private void printCustomer(ServletContext context,ServletOutputStream out,Customer customer)throws Exception{
        //save photo
        byte[] buffer=customer.getImage();
        String path=context.getRealPath("/");
        FileOutputStream fout=new FileOutputStream(path+"photo_copy.gif");
        fout.write(buffer);
        fout.close();

        out.println("------以下是"+customer.getName()+"的個(gè)人信息------"+"<br>");
        out.println("ID: "+customer.getId()+"<br>");
        out.println("口令: "+customer.getPassword()+"<br>");
        out.println("E-Mail: "+customer.getEmail()+"<br>");
        out.println("電話: "+customer.getPhone()+"<br>");
      }
       public void test(ServletContext context,OutputStream out) throws Exception{

        Customer customer=new Customer();
        customer.setName("我心依舊");
        customer.setEmail("it5719@163.com");
        customer.setPassword("666");
        customer.setPhone(5555555);
     
        saveCustomer(customer);

        findAllCustomers(context,out);
        loadAndUpdateCustomer(customer.getId(),"Beijing");
        findAllCustomers(context,out);

        deleteAllCustomers();
      }

      public static void main(String args[]) throws Exception {
        new BusinessService().test(null,System.out);
        sessionFactory.close();
      }

    }

          OK最后一步我們還需要配置LOG4J的屬性文件.下面是LOG4J的配置文件,如果想了解LOG4J,請(qǐng)看我的前一篇隨筆,有簡(jiǎn)單明了的介紹.然后調(diào)試過這個(gè)程序看看結(jié)果.如果通過了,恭喜你,你已經(jīng)進(jìn)入了"冬天"正式開始了Hibernae.
    #init log4j level console file
    log4j.rootLogger=ERROR,console,file

    #create log4j console
    log4j.appender.console=org.apache.log4j.ConsoleAppender
    log4j.appender.console.layout=org.apache.log4j.PatternLayout
    log4j.appender.console.layout.ConversionPattern=%-5p %d [%t] (%F,%L) - %m%n

    #create log4j file
    log4j.appender.file=org.apache.log4j.RollingFileAppender
    log4j.appender.file.File=debug.log
    log4j.appender.file.MaxFileSize=100KB
    log4j.appender.file.MaxBackupIndex=1
    log4j.appender.file.layout=org.apache.log4j.PatternLayout
    log4j.appender.file.layout.ConversionPattern=%-5p %d [%t] (%F,%L) - %m%n

    posted on 2007-05-15 19:33 我心依舊 閱讀(2220) 評(píng)論(3)  編輯  收藏

    FeedBack:
    # re: "冬天"快樂,帶你進(jìn)入Hibernate
    2007-05-16 08:44 | Swing
    孫wq的書吧 挺好的  回復(fù)  更多評(píng)論
      
    # re: "冬天"快樂,帶你進(jìn)入Hibernate
    2007-05-16 09:10 | 劉甘泉
    用spring 控制事務(wù)簡(jiǎn)單的多~~  回復(fù)  更多評(píng)論
      
    # re: "冬天"快樂,帶你進(jìn)入Hibernate[未登錄]
    2007-06-05 17:40 | yy
    Hibernate的數(shù)據(jù)獲取策略有點(diǎn)麻煩。。。  回復(fù)  更多評(píng)論
      

    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲精品无码成人片在线观看| 亚洲综合精品香蕉久久网| 色吊丝免费观看网站| 亚洲精品国产字幕久久不卡| 国产一级高清视频免费看| 日本高清免费中文在线看| 亚洲AV成人片色在线观看高潮| 青青草a免费线观a| www在线观看免费视频| 亚洲国产美女在线观看 | 免费福利在线观看| 亚洲av成人无码久久精品| 日本大片在线看黄a∨免费| 日韩电影免费在线观看网站| 亚洲香蕉在线观看| 国产亚洲精品国看不卡| 久久久久久国产精品免费免费 | 一区二区三区观看免费中文视频在线播放 | 亚洲精品夜夜夜妓女网| 免费看美女裸露无档网站| 人体大胆做受免费视频| 亚洲一区二区三区深夜天堂| 亚洲宅男天堂在线观看无病毒| 在线观看免费人成视频色| 毛片在线播放免费观看| 国产亚洲美女精品久久| 91亚洲精品麻豆| 国产91免费在线观看| 一级做a爱过程免费视频高清 | 波多野结衣免费一区视频 | 无码国产精品一区二区免费I6| 国产人成网在线播放VA免费| 亚洲色大成网站www尤物| 久久91亚洲精品中文字幕| 亚洲人成人无码网www国产| 成人女人A级毛片免费软件| 免费在线中文日本| 一个人晚上在线观看的免费视频| 亚洲精品国产suv一区88| 亚洲导航深夜福利| 久久亚洲中文字幕精品有坂深雪 |