posts - 70,comments - 408,trackbacks - 0

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

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

          首先要設置方言,方言的意思就是不同的數(shù)據(jù)庫,有不同的語言規(guī)范,就跟我們現(xiàn)實中的北京話于廣東話的差距一樣.
          hibernate.dialect=net.sf.hibernate.dialect.SQLServerDialect
          然后設置數(shù)據(jù)庫的驅(qū)動類.
          hibernate.connection.driver_class=com.microsoft.jdbc.sqlserver.SQLServerDriver
          設置數(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
          最后設置的屬性是是否在控制臺打印SQL語句,這里我們設置為true,因為方便調(diào)試,默認是false
          hibernate.show_sql=true
          備注:在Hibernate軟件包的etc目錄下,有一個hibernate.properties文件,里邊有各種關系型數(shù)據(jù)庫的配置樣例.

                OK,配置文件創(chuàng)建完了.就是這么簡單,現(xiàn)在我們來創(chuàng)建一個持久化類.其實說白了持久化類超級簡單,就是我們曾經(jīng)用的JAVABEAN嘛,寫幾個字段Eclipse都能自動生成相關的方法,不過Hibernate的持久化類要 implements一個接口,就是Sreializable接口.下面是持久化類的例子,這里省略了大多數(shù)的get,set方法,到時候自己加上就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持久化類也一樣簡單,下面我們要做的事情更簡單,就是到我們的數(shù)據(jù)庫中,創(chuà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)建對象-關系映射文件.,Hibernate采用XML格式的文件來指定對象和關系型數(shù)據(jù)庫之間的映射,在運行的時候Hibernate將根據(jù)這個映射文件來生成各種SQL語句.下面我們創(chuàng)建一個Customer.hbm.xml的文件,這個文件和Customer.class放在同一個目錄下.
          <?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 標簽的name屬性指定的是類名,table屬性指定的和這個類映射的表名.<id 標簽的<generator 子標簽指定對象標識符生成器,他負責在對象里產(chǎn)生表的唯一標識符.<property標簽的name屬性指定持久化類中屬性的名字,clumn屬性顧名思義,列的意思,這里是指定了數(shù)據(jù)庫中的字段.type屬性指定Hibernate的映射類型,Hibernate映射類型是JAVA類型和SQL類型之間的橋梁,先簡單的知道JAVA里的String和數(shù)據(jù)庫里的varchar這里配置為string,而JAVA里的int和數(shù)據(jù)庫里的int配置為int.還有N多我們以后深入使用Hibernate的時候在深入的了解就OK了.現(xiàn)在知道簡單的足以.最后說一下<property標簽的not-null屬性是設置這個字段是否為空.值得我們關注的是,在實際的項目開發(fā)中,我們不應該以來Hibernate或者數(shù)據(jù)庫來做數(shù)據(jù)的驗證.全部在業(yè)務羅基層處理好就OK了.下面說一下Hibernate采用XML文件來配置對象-關系映射的優(yōu)點:
             1)Hibernate既不會滲透到上層的業(yè)務模型中,也不會泄漏到下層的數(shù)據(jù)模型中
             2)軟件開發(fā)人員可以獨立設計業(yè)務模型,數(shù)據(jù)庫設計人員可以獨立設計數(shù)據(jù)模型,互相不受拘束.
             2)對象-關系映射不依賴于任何代碼,提高了程序的靈活性,使得維護更加方便.

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

                
          下面看一個重點吧,通過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()+"的個人信息------");
              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()+"的個人信息------"+"<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,請看我的前一篇隨筆,有簡單明了的介紹.然后調(diào)試過這個程序看看結(jié)果.如果通過了,恭喜你,你已經(jīng)進入了"冬天"正式開始了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 我心依舊 閱讀(2221) 評論(3)  編輯  收藏

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

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


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 宁南县| 闸北区| 涞源县| 萍乡市| 津南区| 涞水县| 乐清市| 高要市| 黄陵县| 鄂伦春自治旗| 岚皋县| 枣庄市| 蓬莱市| 孟津县| 西城区| 南平市| 东港市| 彰武县| 卢龙县| 辛集市| 南漳县| 安达市| 赤水市| 阿拉尔市| 郧西县| 崇礼县| 温泉县| 武汉市| 浮山县| 名山县| 西丰县| 渑池县| 潼关县| 安徽省| 东平县| 蓬溪县| 台中县| 晋州市| 敦化市| 寻乌县| 宝丰县|