Eclipse3.0+Myeclipse3.8.1GA+MYSQL开发hibernate

类别:Java 点击:0 评论:0 推荐:

   经过几天的努力和烦闷之后,终于在eclipse中使用myeclipse3.8插件成功开发了使用hibernate进行持久层的应用程序!在JBX中可以很轻松的进行开发,可是在eclipse中老是出问题,可能是自己太笨,今天终于搞定了这个基本问题,为了不让和我一般的初学者走弯路,也为了履行我在《Eclipse3.0+Myeclipse3.8.1GA+Tomcat5.0+MYSQL开发JSP》文章中给大家的承诺,现将我的操作步骤以及应该注意的问题记录如下:(注:我的开发环境见《Eclipse3.0+Myeclipse3.8.1GA+Tomcat5.0+MYSQL开发JSP》)
   1.建立java project--->hibtest;
   2.给hibtest新建两个目录src和ado;
   3.右键单击hibtest为工程添加hibernate属性,出现对话框,建立PersonSessionFactary将目录选为/src,一路点击完成!自动生成PersonSessionFactary.java和hibernate.cfg.xml;
   4.Window->show view->other->myeclipse_>DBbrowse,就会在主窗体显示DBbrowse,点NEW,出现Create new profile对话框,填写你的MYSQL相应信息,OK,然后右击你建立的profile,选择Open connection将会出现MYSQL中的数据库以及数据表,然后右键单击create hibernate Mapping file,出现对话框,建立Person类,将会自动生成AbstractPerson.java,Person.java,Person.hbm.xml(这些文件均放在/src下);
   5.在/dao目录下建立测试类Insert.java和Queryhib.java
  Insert.java
package ado;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import src.Person;
import src.PersonSessionFactory;

/**
 * @author 杨强
 *
 */
  public class Insert {
    static Session s=null;
      public static void main(String[] args) throws Exception{
    try{
        s = PersonSessionFactory.currentSession();
             Person yuj = new Person();
             yuj.setName("sfdhh");
             yuj.setAddress("sfhhf");
                    Person x = new Person();
             x.setName("sfdhhfd");
             x.setAddress("fshdfhd");
                    //持久化
             s.save(yuj);
             s.save(x);
      s.flush();
      System.out.print("success");
    }catch (HibernateException e){
   System.err.println("Hibernate Exception" + e.getMessage());
   throw new RuntimeException(e);
  }
   }
      }
Queryhib.java
package ado;


import java.util.Iterator;


import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;

import src.PersonSessionFactory;
import src.Person;
/**
 * @author 杨强
 *
 */
public class Queryhib {
 public Iterator getPerson()
 {
  /*
   * Use the ConnectionFactory to retrieve an open
   * Hibernate Session.
   *
   */
  Session session = null;

  try
  {
   session = PersonSessionFactory.currentSession();
   /*
   * Build HQL (Hibernate Query Language) query to retrieve a list
   * of all the items currently stored by Hibernate.
    */
   Query query =
    session.createQuery(
     "select person from Person person ");
   return query.iterate();

  }
  catch (HibernateException e)
  {
   System.err.println("Hibernate Exception" + e.getMessage());
   throw new RuntimeException(e);
  }
  /*
   * Regardless of whether the above processing resulted in an Exception
   * or proceeded normally, we want to close the Hibernate session.  When
   * closing the session, we must allow for the possibility of a Hibernate
   * Exception.
   *
   */
  
 }
 public static void main(String[] args) throws Exception{
  try{
  Queryhib q=new Queryhib(); 
  Iterator it=q.getPerson();
  while(it.hasNext())
  {
  Person temp=(Person)it.next();
  System.out.println(temp.getId());
  System.out.println(temp.getName());
  System.out.println(temp.getAddress());
  }
  }
 catch (Exception ex)
 {
  System.err.println("Hibernate Exception" + ex.getMessage());
  throw new RuntimeException(ex);
 }
 
  }

}
  6.运行Insert.java对数据库进行插入操作;
  7.运行Queryhib对数据库数据进行查询;

      后注:是不是运行时会出现问题,那是因为没有配置log4j,可以将Hibernate下的log4j.properties放入该工程中一切OK了!没有贴图,请大家见谅!

本文地址:http://com.8s8s.com/it/it13491.htm