經過幾天的努力和煩悶之後,終於在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了!沒有貼圖,請大家見諒!