Java Blog From WeiChunHua

          Java

          常用鏈接

          統計

          develop

          news

          最新評論

          Hibernate入門程序

                 以下程序是Hiberante入門程序:代碼如下:首先說hibernate開發流程.A、準備一個POJO類  B、創建類的映射和配置文件(hibernate.cfg.xml  class.hbm.xml)class.hbm.xml此配置文件是必須與POJO類中的屬性一一對應.
          現在我以我創建的程序為例來進行說明:數據庫為demo,表的名字為admin
          1、POJO類

          package com.wch.pojo;

          public class Admin {
           private int id;
           private String username;
           private String password;
           public int getId() {
            return id;
           }
           public void setId(int id) {
            this.id = id;
           }
           public String getPassword() {
            return password;
           }
           public void setPassword(String password) {
            this.password = password;
           }
           public String getUsername() {
            return username;
           }
           public void setUsername(String username) {
            this.username = username;
           }
          }

          class.hbm.xml映射文件存放位置必須于POJO位置一致.也就是說必須放在同一個目錄.hibernate.cfg.xml放在src根目錄下.
          2、創建hibernate.cfg.xml和class.hbm.xml(class指的是POJO類的名字)
          class.hbm.xml:
          <?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">
          <!--
              Mapping file autogenerated by MyEclipse Persistence Tools
          -->
          <hibernate-mapping>
              <class name="com.wch.pojo.Admin" table="admin">
                  <id name="id" type="integer">
                      <column name="id" />
                      <generator class="assigned"></generator>
                  </id>
                  <property name="username" type="string">
                      <column name="username" length="32" not-null="false" />
                  </property>
                  <property name="password" type="string">
                      <column name="password" length="20" not-null="false" />
                  </property>
              </class>
          </hibernate-mapping>
          hibernate.cfg.xml:

          <?xml version='1.0' encoding='UTF-8'?>
          <!DOCTYPE hibernate-configuration PUBLIC
                    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

          <!-- Generated by MyEclipse Hibernate Tools.                   -->
          <hibernate-configuration>

          <session-factory>
           <property name="dialect">
            org.hibernate.dialect.MySQLDialect
           </property>
           <property name="connection.url">
            jdbc:mysql://localhost:3306/demo
           </property>
           <property name="connection.username">root</property>
           <property name="connection.password">****</property>
           <property name="connection.driver_class">
            com.mysql.jdbc.Driver
           </property>
           <property name="myeclipse.connection.profile">mysql5.0</property>
           <property name="show_sql">true</property>
           <mapping resource="com/wch/pojo/Admin.hbm.xml" />

          </session-factory>

          </hibernate-configuration>


          3、創建應用程序并進行代碼測試:

          package com.wch.op;

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

          import com.wch.pojo.Admin;

          public class UserOperation {
           public static void main(String[] args)throws Exception{
            Configuration config = new Configuration().configure();
            // 創建工廠
            SessionFactory factory = config.buildSessionFactory();
            // 打開session
            Session session = factory.openSession();
            // 事務提交
            try{
             Transaction tx = session.beginTransaction();
             // 創建對象
             Admin hb = new Admin();
             hb.setId(3);
             hb.setUsername("Hibernate3.2");
             hb.setPassword("20081202");
             //hb.setUsername("WCH");
             //hb.setPassword("Hibernate");
             // 執行插入語句,在hibernat中操作的是一個對象
             session.save(hb);
             // 提交事務
             tx.commit();
            }catch(Exception e)
            {
             System.out.println("error Msg:"+e.getMessage());
            }finally{
              // close session
              session.close();
            }
           }
          }

          更為詳細的解釋請參照Hibernate官方網站:www.hibernate.org


          posted on 2008-12-02 10:19 sunny spring 閱讀(280) 評論(0)  編輯  收藏 所屬分類: FrameWork


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


          網站導航:
           
          主站蜘蛛池模板: 甘肃省| 东光县| 宁安市| 铜山县| 平邑县| 库车县| 启东市| 克山县| 佳木斯市| 唐河县| 德化县| 武安市| 萨嘎县| 福安市| 灵山县| 行唐县| 延寿县| 深圳市| 清苑县| 大埔县| 周至县| 滦南县| 朝阳县| 彭泽县| 浦城县| 西华县| 汶上县| 老河口市| 仁怀市| 独山县| 新兴县| 竹山县| 普陀区| 额济纳旗| 湖口县| 湘西| 增城市| 抚顺市| 石柱| 邵武市| 和顺县|