posts - 32,comments - 8,trackbacks - 0
           

          Oops! Eclipse + Hibernate Quick Start

          Purpose:

          學會使用Hibernate

          Precondition:

          eclipse-java-europa-win32.zip

          hibernate-3.2.5.ga.zip

          mysql-5.0.45-win32.zip

          Quick Start:

          mySql數據庫里面添加一張表。



          對應的
          sql語句是:

          CREATE TABLE CUSTOMER(

          CID INTEGER,

          USERNAME VARCHAR(12) NOT NULL,

          PASSWORD VARCHAR(12)

          );

          ALTER TABLE CUSTOMER ADD CONSTRAINT PK PRIMARY KEY(CID);


          eclipse里面新建一個java project, 項目名為:Oops_hibernate


          新建一個
          lib目錄,在lib目錄下面添加以下jar包,全部可以在hibernate.zip文件里面找到


          選擇
          project – properties – java build path – libraries – add jars

          Oops_hibernate目錄下面的所有lib加進來



          src目錄下面添加以下文件:


          Customer.hbm.xml

          <?xml version="1.0"?>

          <!DOCTYPE hibernate-mapping PUBLIC

              "-//Hibernate/Hibernate Mapping DTD//EN"

              "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"
          >

          <hibernate-mapping>

              
          <class name="Customer" table="CUSTOMER">

                  
          <id name="id" column="CID">

                      
          <generator class="increment" />

                  
          </id>

                  
          <property name="username" column="USERNAME" />

                  
          <property name="password" column="PASSWORD" />

              
          </class>

          </hibernate-mapping>


          Customer.java


          public class Customer {

              

              
          private int id;

              
          private String username;

              
          private String password;

              
          public int getId() {

                  
          return id;

              }

              
          public String getPassword() {

                  
          return password;

              }

              
          public String getUsername() {

                  
          return username;

              }

              
          public void setId(int id) {

                  
          this.id = id;

              }

              
          public void setPassword(String password) {

                  
          this.password = password;

              }

              
          public void setUsername(String username) {

                  
          this.username = username;

              }

          }


          hibernate.cfg.xml
          ,注意紅色部分要和數據庫對應。

                   <?xml version="1.0" encoding="utf-8" ?>

          <!DOCTYPE hibernate-configuration

              PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"

              "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

          <hibernate-configuration>

             

              <session-factory name="java:/hibernate/HibernateFactory">

                 

                  <property name="show_sql">true</property>

                  <property name="connection.driver_class">

                      com.mysql.jdbc.Driver

                  </property>

                  <property name="connection.url">

                      jdbc:mysql://localhost:3306/test

                  </property>

                  <property name="connection.username">

                      root

                  </property>

                  <property name="connection.password">

                      admin

                  </property>

                  <property name="dialect">

                      org.hibernate.dialect.MySQLDialect

                  </property>

                 

                  <mapping resource="Customer.hbm.xml" />

                 

              </session-factory>

             

          </hibernate-configuration>

          Test.java


          import org.hibernate.*;

          import org.hibernate.cfg.*;

          public class Test {

              
          public static void main(String[] args) {

                  
          try {

                      SessionFactory sf 
          =

                          
          new Configuration().configure().buildSessionFactory();

                      Session session 
          = sf.openSession();

                     Transaction tx 
          = session.beginTransaction();

                      
          for (int i = 0; i < 200; i++) {

                          Customer customer 
          = new Customer();

                          customer.setUsername(
          "customer" + i);

                          customer.setPassword(
          "customer");

                          session.save(customer);

                      }

                      tx.commit();

                      session.close();

                  } 
          catch (HibernateException e) {

                      e.printStackTrace();

                  }

              }

          }



          右鍵點擊項目,Run as – java application



          在窗口選擇
          Test





          運行,完成!


          posted on 2007-09-01 14:57 張辰 閱讀(457) 評論(0)  編輯  收藏 所屬分類: Dr. Oops
          主站蜘蛛池模板: 民权县| 安岳县| 高青县| 泰兴市| 宝丰县| 尖扎县| 白城市| 南安市| 遂平县| 桦南县| 五指山市| 防城港市| 郸城县| 新干县| 饶河县| 靖边县| 象州县| 宁陵县| 阿荣旗| 罗江县| 阿拉善左旗| 乐安县| 新河县| 玉山县| 土默特右旗| 错那县| 霸州市| 张家港市| 个旧市| 津南区| 本溪市| 黑河市| 连城县| 宜章县| 松滋市| 阳西县| 新泰市| 富阳市| 安新县| 曲松县| 五指山市|