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


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


          網站導航:
           
          主站蜘蛛池模板: 铁岭县| 平利县| 屯昌县| 穆棱市| 酉阳| 汝南县| 灌阳县| 营山县| 沅江市| 于都县| 乐都县| 江陵县| 澳门| 鄱阳县| 泌阳县| 肃宁县| 南充市| 兰溪市| 仁寿县| 花莲市| 泌阳县| 陇川县| 阿拉尔市| 齐齐哈尔市| 福州市| 红河县| 容城县| 萍乡市| 洞口县| 韶山市| 柳江县| 万州区| 乳山市| 鱼台县| 兖州市| 溧水县| 浦东新区| 专栏| 福贡县| 襄樊市| 和平区|