**********************************************************
//增加一条记录
Session s = HibernateSessionFactory.currentSession();
Products p=new Products();
p.setProductName("apple");//设置属性值
Byte b=new Byte("0");
p.setDiscontinued(b);
Transaction tx=session.beginTransaction();
session.save(p);
tx.commit();
session.close();
**********************************************************
//查询
Session s = HibernateSessionFactory.currentSession();
Products p=(Products)s.load(Products.class,new Integer(20));// 按主键查询
// Criteria Query查询方式
Criteria cr=session.createCriteria(Products.class);
cr.add(Expression.eq("productId",new Integer(10));
List list=cr.list();
Iterator i=list.iterator();
while(i.hasNext()){
Products p=(Products)i.next();
}
sesssin.close();
// Expression.eq() =
// Expression.gt() >
// Expression.ge() >=
// Expression.lt() <
// Expression.le() <=
// Expression.between()
// Expression.like()
// Expression.and(Expression.eq(),Expression.eq())且
// Expression.or(Expression.eq(),Expression.eq())或
**********************************************************
// Query查询方式
Session s = HibernateSessionFactory.currentSession();
String sql="select p from Products as p where p.productId>?";
Query q=s.createQuery(sql);
q.setInteger(0,10);// 属性值从0开始
List l=q.list();
**********************************************************
分页:
Session s = HibernateSessionFactory.currentSession();
Criteria c=s.createCriteria(Products.class);
c.add(Expression.gt("productId",new Integer(2)));
c.setFirstResult(10);
c.setMaxResults(10);
// String hql="from Products ";
// Query q=s.createQuery(hql);
//
// q.setFirstResult(0);
// q.setMaxResults(10);
List list=c.list();
for(int i=0;i<list.size();i++){
Products p=(Products)list.get(i);
System.out.println(p.getProductId());
}
**********************************************************
更新
Session sss = HibernateSessionFactory.currentSession();
Products pp=(Products)sss.load(Products.class,new Integer(3));
System.out.println(""+pp.getProductId()+"\t"+pp.getUnitPrice());
pp.setUnitPrice(new Double(50.0));
Transaction t=sss.beginTransaction();
sss.update(pp);
t.commit();
**********************************************************
load方法和get方法
get档不存在时,不会抛异常
//增加一条记录
Session s = HibernateSessionFactory.currentSession();
Products p=new Products();
p.setProductName("apple");//设置属性值
Byte b=new Byte("0");
p.setDiscontinued(b);
Transaction tx=session.beginTransaction();
session.save(p);
tx.commit();
session.close();
**********************************************************
//查询
Session s = HibernateSessionFactory.currentSession();
Products p=(Products)s.load(Products.class,new Integer(20));// 按主键查询
// Criteria Query查询方式
Criteria cr=session.createCriteria(Products.class);
cr.add(Expression.eq("productId",new Integer(10));
List list=cr.list();
Iterator i=list.iterator();
while(i.hasNext()){
Products p=(Products)i.next();
}
sesssin.close();
// Expression.eq() =
// Expression.gt() >
// Expression.ge() >=
// Expression.lt() <
// Expression.le() <=
// Expression.between()
// Expression.like()
// Expression.and(Expression.eq(),Expression.eq())且
// Expression.or(Expression.eq(),Expression.eq())或
**********************************************************
// Query查询方式
Session s = HibernateSessionFactory.currentSession();
String sql="select p from Products as p where p.productId>?";
Query q=s.createQuery(sql);
q.setInteger(0,10);// 属性值从0开始
List l=q.list();
**********************************************************
分页:
Session s = HibernateSessionFactory.currentSession();
Criteria c=s.createCriteria(Products.class);
c.add(Expression.gt("productId",new Integer(2)));
c.setFirstResult(10);
c.setMaxResults(10);
// String hql="from Products ";
// Query q=s.createQuery(hql);
//
// q.setFirstResult(0);
// q.setMaxResults(10);
List list=c.list();
for(int i=0;i<list.size();i++){
Products p=(Products)list.get(i);
System.out.println(p.getProductId());
}
**********************************************************
更新
Session sss = HibernateSessionFactory.currentSession();
Products pp=(Products)sss.load(Products.class,new Integer(3));
System.out.println(""+pp.getProductId()+"\t"+pp.getUnitPrice());
pp.setUnitPrice(new Double(50.0));
Transaction t=sss.beginTransaction();
sss.update(pp);
t.commit();
**********************************************************
load方法和get方法
get档不存在时,不会抛异常