<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    all gone

    all gone

    Eclipse下Hibernate入門


    1.開發環境
         Eclipse 3.2+MySQL 4.0.16+Hibernate3.0
        首先應該安裝好Eclipse和MySQL,此外準備好MySQL的JDBC Driver和Hibernate3.0,相關下載地址如下:
         Eclipse SDK:
            http://www.eclipse.org/downloads/index.php 
           MySQL及MySQL的JDBC Driver:
               http://www.mysql.org
           Hibernate:
              http://www.hibernate.org
           此外我還安裝了Eclipse的一個Hibernate插件:
         Hibernate synchronizer
               http://hibernatesynch.sourceforge.net


           Plugin Search:
             http://eclipse-plugins.2y.net/eclipse/search.jsp 

           Hibernate synchronizer插件的安裝和配置有問題請直接Google。

         在工程中其實只用到了Hibernate synchronizer插件的一部分功能,Hibernate依賴的相關jar包最好還是手動添加,因為最開始用Hibernate synchronizer添加時總是發生錯誤。
         將下載的Mysql driver和Hibernate包解壓縮,我們需要的只是里面相關的jar,在Eclipse中新建Mysql_Driver和Hibernate兩個user library,將mysql-connector-java-3.0.15-ga-bin.jar加入Mysql_Driver中,將hibernate3.jar,
    ,log4j-1.2.11.jar,antlr-2.7.5H3.jar,asm.jar,asm-attrs.jar,cglib-2.1.2.jar,commons-collections-2.1.1.jar,commons-logging-1.0.4.jar,dom4j-1.6.1.jar,ehcache-1.1.jar,jta.jar加入到Hibernate中。

    2.開始
    在Mysql中新建test數據庫(Mysql其實有個空的test數據庫),然后新建下面的Table

    create table user (
     id int(10) not null auto_increment primary key,
     name varchar(20) not null,
     password varchar(20) not null,
     email varchar(50),
     address varchar(100)
    )type=innodb;


    新建Java Project,將Mysql_Driver,Hibernate兩個user library添加到該工程的java build path中。

    新建與數據表對應的POJO類:User和Contact

    /**
     *
     * 
     */
    package com.user;

    /**
     * @author lzy
     *
     */
    public class User{
        private Integer id;
        private String name;
        private String password;
        private Contact contact;
       

     /**
      * @return Returns the id.
      */
     public Integer getId() {
      return id;
     }
     /**
      * @param id The id to set.
      */
     public void setId(Integer id) {
      this.id = id;
     }
     /**
      * @return Returns the name.
      */
     public String getName() {
      return name;
     }
     /**
      * @param name The name to set.
      */
     public void setName(String name) {
      this.name = name;
     }
     /**
      * @return Returns the password.
      */
     public String getPassword() {
      return password;
     }
     /**
      * @param password The password to set.
      */
     public void setPassword(String password) {
      this.password = password;
     }
     /**
      * @return Returns the contact.
      */
     public Contact getContact() {
      return contact;
     }
     /**
      * @param contact The contact to set.
      */
     public void setContact(Contact contact) {
      this.contact = contact;
     }
       
       
    }
    /**
     *
     */
    package com.user;

    /**
     * @author lzy
     *
     */
    public class Contact {
     private String email;
        private String address;

     /**
      * @return Returns the address.
      */
     public String getAddress() {
      return address;
     }
     /**
      * @param address The address to set.
      */
     public void setAddress(String address) {
      this.address = address;
     }
     /**
      * @return Returns the email.
      */
     public String getEmail() {
      return email;
     }
     /**
      * @param email The email to set.
      */
     public void setEmail(String email) {
      this.email = email;
     }
    }

    之后可以用synchronizer插件生成Hibernate配置文件和映射文件(相關過程可以參考http://www.ideagrace.com/html/doc/2005/08/01/00315.html),不過映射文件必須稍作修改。

    hibernate.cfg.xml
    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE hibernate-configuration
        PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
        "

    <hibernate-configuration>
        <session-factory >

      <!-- local connection properties -->
      <property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
      <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="hibernate.connection.username"></property>
      <property name="hibernate.connection.password"></property>
      <!-- property name="hibernate.connection.pool_size"></property -->

      <!-- dialect for MySQL -->
            <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

            <property name="hibernate.show_sql">True</property>
            <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
         <mapping resource="User.hbm.xml"/>


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

    User.hbm.xml

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
     "-//Hibernate/Hibernate Mapping DTD//EN"
     "

    <hibernate-mapping package="com.user">
     <class
      name="User"
      table="user"
     >
      <id
       name="Id"
       type="integer"
       column="id"
      >
       <generator class="native"/>
      </id>

      <property
       name="Name"
       column="name"
       type="string"
       not-null="true"
       length="20"
      />
      <property
       name="Password"
       column="password"
       type="string"
       not-null="true"
       length="20"
      />
      <component name="Contact" class="Contact">
       <property
       name="Email"
       column="email"
       type="string"
       not-null="false"
       length="50"
      />
      <property
       name="Address"
       column="address"
       type="string"
       not-null="false"
       length="100"
      />
      </component>
      


     </class> 
    </hibernate-mapping>

     

    3.測試
    添加一個測試類:HibernateTest

    package com.user;

    import java.util.List;
    import java.util.ListIterator;

    import org.hibernate.*;
    import org.hibernate.cfg.*;

    public class HibernateTest {
        public static void main(String[] args) throws HibernateException {
            SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
           
            //
            //testInsert(sessionFactory);
           
            //
            testQuery(sessionFactory);
           
             
            sessionFactory.close();
           
        }
        public static void testInsert( SessionFactory sessionFactory )throws HibernateException {
         
          Session session = sessionFactory.openSession();
            Transaction tx= session.beginTransaction();
            User user = new User();
            Contact contact=new Contact();
            contact.setEmail("email");
            contact.setAddress("address");
           
            user.setName("caterpillar");
            user.setPassword("password");
            user.setContact(contact);
           
            session.save(user);
            tx.commit();
            session.close();
            System.out.println("OK!");
       }
       
        public static void testQuery( SessionFactory sessionFactory )throws HibernateException {
         
         Session session = sessionFactory.openSession();
            Transaction tx= session.beginTransaction();
            User user = new User();
            Contact contact=new Contact();
                     
            Query query=session.createQuery("from User as user");
            //query.setCharacter(1, 'M');
            List names =query.list();
            for(ListIterator it=names.listIterator();it.hasNext();){
               user= (User)it.next();
               System.out.println("Id: " + user.getId());
                System.out.println("name: " + user.getName());
                System.out.println("password: " + user.getPassword());
                if(user.getContact()!=null){
                 
                 if(user.getContact().getEmail()!=null){
                  System.out.println("Email: " + user.getContact().getEmail());
                 }
                 if(user.getContact().getAddress()!=null){
                  System.out.println("Address: " + user.getContact().getAddress());
                   
                 }
                }
               
               
               
            }
             
          
            tx.commit();
            session.close();
         
        }
    }

     

      

     

    posted on 2005-12-10 12:50 all gone 閱讀(5032) 評論(3)  編輯  收藏 所屬分類: Java

    評論

    # re: Eclipse下Hibernate入門 2005-12-10 12:54 all gone

    總算完成了  回復  更多評論   

    # re: Eclipse下Hibernate入門 2006-05-31 14:51 cellen

    我做了下你說的例子,在用HibernateSyn插件生成的那個hibernate.cfg.xml文件中,我要加上一段<mapping resource ="net/dxj/User.hbm.xml"/>然后在那個,User.hbm.xml文件中ID的生成器上,更改成如此<generator class="native"/>,不過,Constact.java我都沒有用上,我直接把email及address作為user的屬性的,如果不這樣做就報在user.java文件中找不到getter email方法的錯誤!期待你的解決!msn:djpsunday@hotmail.com  回復  更多評論   

    # re: Eclipse下Hibernate入門 2006-05-31 15:10 cellen

    噢,不好意思,我照你貼出來的那個junit代碼重新配置一下,User.hbm.xml文件就成功了!^_^  回復  更多評論   

    主站蜘蛛池模板: 亚洲av日韩av天堂影片精品| 免费人成视网站在线观看不卡| 亚洲av无码国产精品色午夜字幕 | 又粗又长又爽又长黄免费视频 | 亚洲国产综合第一精品小说| 2021国内精品久久久久精免费| 久久青青草原亚洲av无码app| 蜜桃AV无码免费看永久| 亚洲天堂一区二区三区四区| 免费在线观看h片| 天堂亚洲国产中文在线| 日韩一区二区三区免费体验| 在线播放国产不卡免费视频| 亚洲国产成人一区二区三区| 久久久久高潮毛片免费全部播放 | 久久WWW色情成人免费观看| 亚洲日本久久久午夜精品| 免费看香港一级毛片| 一级做a爰片久久毛片免费陪 | 两个人看的www免费| 亚洲激情黄色小说| 搡女人免费视频大全| 曰批全过程免费视频免费看 | 亚洲人成网亚洲欧洲无码久久| 国产精品视频白浆免费视频| 亚洲国产日韩在线成人蜜芽| 男女啪啪永久免费观看网站| 亚洲精品偷拍视频免费观看| 亚洲成a人片在线观看日本| 日本免费网址大全在线观看| 国产成人精品亚洲一区| 久久亚洲综合色一区二区三区| 麻花传媒剧在线mv免费观看| 色妞www精品视频免费看| 亚洲成A人片在线观看WWW| 成人免费午夜在线观看| 国产成人无码精品久久久久免费| 亚洲毛片无码专区亚洲乱| 国产一级淫片a免费播放口之| A片在线免费观看| 亚洲精品无码少妇30P|