隨筆-21  評論-29  文章-0  trackbacks-0
          引入
            模型不匹配(阻抗不匹配)
                  Java面向對象語言,對象模型,其主要概念有:繼承、關聯、多態等;
                  數據庫是關系模型,其主要概念有:表、主鍵、外鍵等。
            解決方法:
                 使用JDBC手工轉換;
                 使用ORM(Object Relation Mapping對象關系映射)框架來解決,主流的ORM框架有Hibernate、TopLink、OJB。
          安裝配置
                下載地址:http://www.hibernate.org,本學習課程采用3.2.5.
                 將下載目錄/hibernate3.jar和/lib下的hibernate運行時必須得包加入到classpath中:antlr.jar   cglib.jar    asm.jar   commons-collections.jar  commons-logging.jar   jta.jar   dom4j.jar

                 配置文件hibernate.cfg.xml和hibernate.properties,XML和properties兩種,這兩個文件的作用一樣,提供一個即可,推薦XML格式,下載目錄/etc下是示例配置文件。
                可以再配置文件指定:數據庫的URL、用戶名、密碼、JDBC驅動類、方言等。
                啟動時Hibernate會在CLASSPATH里找這個配置文件。
                 映射文件(hbm.xml,對象模型和關系模型的映射)。在/eg目錄下有完整的Hibernate示例。
           快速開始小例子
          新建一java工程 命名為hibernate。
          新建一User類


          package cn.itcast.hibernate.domain;

          import java.util.Date ;
          public class User {
              
          private int id ;
              
          private String name ;
              
          private Date birthday ;
              
          public int getId() {
                  
          return id;
              }

              
          public void setId(int id) {
                  
          this.id = id;
              }

              
          public String getName() {
                  
          return name;
              }

              
          public void setName(String name) {
                  
          this.name = name;
              }

              
          public Date getBirthday() {
                  
          return birthday;
              }

              
          public void setBirthday(Date birthday) {
                  
          this.birthday = birthday;
              }

              
          }

          導入hibernate相關的jar包,包括hibernate里面的所有包 以及MySQL數據庫相應的驅動包
          cn.itcast.hibernate.domain下新建 User.hbm.xml文件 代碼如下
          <?xml version="1.0"?>
          <!DOCTYPE hibernate-mapping PUBLIC 
              "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
              "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
          >
          <hibernate-mapping package="cn.itcast.hibernate.domain">

              
          <class name="User">
                  
          <id name="id">
                      
          <generator class="native" />
                  
          </id>
                  
                  
          <property name="name"/>
                  
          <property name="birthday"/>
                  
              
          </class>
          </hibernate-mapping>
          在src下新建 hibernate.cfg.xml文件 代碼如下
          <!DOCTYPE hibernate-configuration PUBLIC
              "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
              "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"
          >

          <hibernate-configuration>
              
          <session-factory>
                  
          <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
                  
          <property name="connection.url">jdbc:mysql:///test</property>
                  
          <property name="connection.username">root</property>
                  
          <property name="connection.password">root</property>    
                  
          <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
                  
                  
          <property name="connection.password">root</property>
                  
          <property name="hbm2ddl.auto">create</property>
                      
                  
          <mapping resource="cn/itcast/hibernate/domain/User.hbm.xml"/>
                  
              
          </session-factory>
          </hibernate-configuration>
          cn.itcast.hibernate包下新建 類Base類 代碼如下
          package cn.itcast.hibernate;
          import java.util.Date;

          import org.hibernate.Session;
          import org.hibernate.SessionFactory;
          import org.hibernate.cfg.Configuration;

          import cn.itcast.hibernate.domain.User;
          import org.hibernate.Transaction;
          public class Base {

              
          public static void main(String[] args) {
                  Configuration cfg 
          = new Configuration() ;
                  cfg.configure();
                  SessionFactory sf 
          = cfg.buildSessionFactory();
                  
                  Session s 
          = sf.openSession();
                  Transaction tx 
          = s.beginTransaction();
                  User user 
          = new User();
                  user.setBirthday(
          new Date());
                  user.setName(
          "name");
                  
                  s.save(user);
                  tx.commit();
                  s.clear();
                  System.out.println(
          "end");
              }


          }


          運行 Base類 查看數據庫 證明第一個hibernate程序運行成功!


          大概用了一個下午才運行出這個程序,不大習慣用MYSQL這個數據庫,搞得頭都大了!今天草草結束這個實例,明天再好好分析一下!
          本案例代碼hibernatefile
          posted on 2009-05-03 15:47 特立獨行 閱讀(425) 評論(0)  編輯  收藏 所屬分類: Hibernate框架
          主站蜘蛛池模板: 青冈县| 盐源县| 溆浦县| 油尖旺区| 昭通市| 巴林右旗| 汝城县| 陆良县| 张家口市| 北辰区| 合阳县| 汉阴县| 安丘市| 和顺县| 宜宾市| 新安县| 长岭县| 大悟县| 嘉义县| 安达市| 巴彦淖尔市| 偏关县| 庐江县| 岳普湖县| 吉隆县| 兴和县| 达拉特旗| 武穴市| 连平县| 眉山市| 甘谷县| 平武县| 阆中市| 徐闻县| 嘉鱼县| 龙井市| 卓尼县| 保靖县| 万年县| 苏尼特左旗| 二连浩特市|