那位高手 给我点hibernate 的实例来!
主要是分页的,越多实例越好
package com;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
public abstract class HibernateBase
{
protected SessionFactory sessionFactory;//会话工厂,用于创建会话
protected Session session;//hibernate会话
protected Transaction transaction; //hiberante事务
/**
*开始一个hibernate事务
*/
protected void beginTransaction()
throws HibernateException {
// 帮助方法
session = SessionFactory3.currentSession();
transaction = session.beginTransaction();
}
/**
*结束一个hibernate事务。
*/
protected void endTransaction(boolean commit)
throws HibernateException {
if (commit) {
transaction.commit();
} else {
//如果是只读的操作,不需要commit这个事务。
transaction.rollback();
}
SessionFactory3.closeSession();
}
}
package com;
import org.hibernate.*;
import java.util.*;
import org.hibernate.Query;
import com.HibernateBase;
import org.hibernate.HibernateException;
import vo.customervo;
public class Dao extends HibernateBase{
public Dao() {
super();
// TODO Auto-generated constructor stub
}
/**
* @param args
*/
public void getCustomer() {
session = SessionFactory3.currentSession();
Query query = session.createQuery("from customervo as a");
//List list = query.list();
Iterator it = query.iterate();
while (it.hasNext()) {
customervo temp = (customervo) it.next();
System.out.println(" " + temp.getCustomerId()+" "+temp.getCustomerName()+" "+temp.getCustomerAge()+" "+temp.getCustomerBirth());
}
SessionFactory3.closeSession();//
}
//按那个ID 更新
public void upadaCustomter(String id)
{ beginTransaction();
customervo Customer=(customervo)session.load(customervo.class,id);
Customer.setCustomerName("panxi");
Customer.setCustomerAge(new Long(50));
Customer.setCustomerBirth(new Date(2005-05-06));
session.save(Customer);
endTransaction(true);
}
/**
*删除给定ID的course
*/
public void deleteCustomer(String customerID)throws HibernateException
{
beginTransaction();
customervo customer=(customervo)session.load(customervo.class,customerID);
session.delete(customer);
endTransaction(true);
}
/**
/**
*增加一个Course
*/
public void addCustomer(customervo st)throws HibernateException
{
beginTransaction();
session.save(st);
endTransaction(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Dao dao=new Dao();
dao.getCustomer();
dao.upadaCustomter("3");
System.out.println("更新以后: ");
dao.getCustomer();
customervo temp=new customervo();
temp.setCustomerId("009");
temp.setCustomerAge(new Long(100));
temp.setCustomerName("xiao");
temp.setCustomerBirth(new Date(2006-01-22));
dao.addCustomer(temp);
System.out.println("添加的以后: ");
dao.getCustomer();
dao.deleteCustomer("009");
System.out.println("删除以后: ");
dao.getCustomer();
}
}