posts - 66,comments - 41,trackbacks - 0
             今天終于搞了個(gè)簡單的Hibernate的入門,這個(gè)Hibernate使用過程大概如下,DB建表->JavaBean->創(chuàng)建映射文件->Hibernate配置文件->編寫代碼。
          建表和JavaBean就不說了,還是說說配置文件吧,下面是一個(gè)映射文件的標(biāo)準(zhǔn)配置文件(Standard.hbm.xml):

           1 <?xml version="1.0" encoding="UTF-8"?>
           2 
           3 <!-- 
           4     所有的XML映射文件都需要定義如下所示的DOCTYPE。
           5     Hibernate會先在它的類路徑(classpath)中搜索DTD文件
           6 -->
           7 <!DOCTYPE hibernate-mapping PUBLIC
           8      "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
           9         "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
          10 
          11 <!-- 
          12     hibernate-mapping有如下可選屬性:
          13     1.Schema 屬性指明了這個(gè)映射的表所在的schema名稱
          14     2.default-cascade屬性指定了默認(rèn)的級聯(lián)風(fēng)格 可取值有none、save、update
          15     3.auto-import屬性默認(rèn)讓我們在查詢語言中可以使用非全限定的類名 可取值有true、false.
          16     4.package屬性指定一個(gè)包前綴。
          17 -->
          18 
          19 <hibernate-mapping schema="schemaName" default-cascade="none"
          20     auto-import="true" package="packageName">
          21 
          22     <!-- 用class元素來定義一個(gè)持久化類 -->
          23     <class name="People" table="person">
          24         <!-- id元素定義了屬性到數(shù)據(jù)庫表主鍵字段的映射 -->
          25         <id name="id">
          26             <!-- 用來為持久化類的實(shí)例生成唯一的標(biāo)識 -->
          27             <generator class="navive" />
          28         </id>
          29 
          30         <!-- discriminator識別器 是一種定義繼承關(guān)系的映射方法 -->
          31         <discriminator column="subclass" type="character" />
          32 
          33         <!-- property 元素為類聲明了一個(gè)持久化的、JavaBean風(fēng)格的屬性-->
          34         <property name="name" type="String">
          35             <column name="name" length="64" not-null="true" />
          36         </property>
          37 
          38         <property name="sex" not-null="true" update="false" />
          39 
          40         <!-- 多對一映射關(guān)系 -->
          41         <many-to-one name="friend" column="friend_id" update="false" />
          42 
          43         <!-- 設(shè)置關(guān)聯(lián)關(guān)系 -->
          44         <set name="friends" inverse="true" order-by="id">
          45             <key column="friend_id" />
          46             <!-- 一對多映射 -->
          47             <one-to-many class="Cat" />
          48         </set>
          49 
          50     </class>
          51 
          52 </hibernate-mapping>
          53 

                Hibernate的配置文件分為兩種:一種為hibernate.cfg.xml,還有一種為hibernate.properties文件,下面分別給出標(biāo)準(zhǔn)配置
          hibernate.cfg.xml:
          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE hibernate-configuration PUBLIC 
                     "http://Hibernate/Hibernate Configuration DTD 3.0//EN" 
                     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"
          >

          <hibernate-configuration>
              
          <session-factory>

                  
          <!-- 顯示執(zhí)行的SQL語句 -->
                  
          <property name="show_sql">true</property>

                  
          <!-- 連接字符串 -->
                  
          <property name="connection.url">
                      jdbc:mysql://localhost3306/stu
                  
          </property>

                  
          <!-- 數(shù)據(jù)庫用戶名 -->
                  
          <property name="connection.username">root</property>

                  
          <!-- 數(shù)據(jù)庫用戶密碼 -->
                  
          <property name="connection.password">password</property>

                  
          <!-- 數(shù)據(jù)庫驅(qū)動-->
                  
          <property name="connection.driver_class">
                      com.mysql.jdbc.Driver
                  
          </property>

                  
          <!-- 選擇使用的方言 -->
                  
          <property name="dialect">
                      org.hibernate.dialect.MySQLDialect
                  
          </property>

                  
          <!-- 映射文件 -->
                  
          <mapping resource="com/stuman/domain/Admin.hbm.xml" />

                  
          <mapping resource="com/stuman/domain/Student.hbm.xml" />

              
          </session-factory>
          </hibernate-configuration>

          hibernate.properties:
          #指定數(shù)據(jù)庫使用的驅(qū)動類
          hibernate.connection.driver_class=com.mysql.jbc.Driver r

          #指定數(shù)據(jù)庫連接串
          hibernate.connection.url=jdbc:mysql://localhost:3306/db

          #指定數(shù)據(jù)庫連接的用戶名
          hibernate.connection.username=username

          #指定數(shù)據(jù)庫連接的密碼
          hibernate.connection.password=password

          #指定數(shù)據(jù)庫使用的方言
          hibernate.dialect=net.sf.hibernate.dialect.MySQLDialect

          #指定是否打印SQL語句
          hibernate.show_sql=true/false

          下面是一個(gè)測試類,HOHO
           1 package Hibernate;
           2 
           3 import org.hibernate.*;
           4 import org.hibernate.cfg.*;
           5 
           6 public class Test {
           7     public static void main(String args[]) {
           8        try {
           9            //常見session工廠
          10            SessionFactory sf=new Configuration().configure().buildSessionFactory();
          11            //獲取session實(shí)例
          12            Session session=sf.openSession();
          13            //開始事務(wù)
          14            Transaction tx=session.beginTransaction();
          15            //創(chuàng)建一個(gè)User對象
          16            User user=new User();
          17            //為對象賦值
          18            user.setUsername("Hibernate");
          19            user.setPassword("123");
          20            //調(diào)用save()方法保存user實(shí)例到DB
          21            session.save(user);
          22            tx.commit();
          23            //關(guān)閉Session
          24            session.close();
          25        }catch (HibernateException e) {
          26            e.printStackTrace();
          27        }
          28    }
          29 }
          30 




          MSN:
          posted on 2007-12-17 21:20 kylixlu 閱讀(166) 評論(0)  編輯  收藏

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 陵川县| 广南县| 南郑县| 合作市| 盐亭县| 阿荣旗| 广宗县| 封丘县| 临猗县| 象州县| 板桥市| 扎兰屯市| 游戏| 安福县| 定结县| 布拖县| 米脂县| 页游| 遵义县| 勐海县| 开封市| 龙泉市| 洪洞县| 西城区| 汉源县| 武陟县| 大城县| 隆子县| 十堰市| 唐海县| 衡东县| 吉林省| 东乡族自治县| 集安市| 新野县| 云阳县| 富裕县| 吉水县| 宁阳县| 阿图什市| 通州市|