Hibernate中的Query一些基本用法.docVIP

  • 1
  • 0
  • 约7.87千字
  • 约 21页
  • 2017-02-13 发布于江苏
  • 举报
Hibernate中的Query一些基本用法

/* ** * 添加 */ public void save(Stu stu){ try { tran=this.GetSession().beginTransaction(); this.GetSession().save(stu); mit(); } catch (HibernateException e) { throw e;/* ** * 添加 */ public void save(Stu stu){ try { tran=this.GetSession().beginTransaction(); this.GetSession().save(stu); mit(); } catch (HibernateException e) { throw e; }finally{ this.CloseSession(); } } /** * 使用HQL全查询 */ public List getallbyHQL(){ List arr=null; try { String hql=from Stu; Query query=this.GetSession().createQuery(hql); arr=query.list(); } catch (HibernateException e) { throw e; }finally{ this.CloseSession(); } return arr; } /** * 根据主键查询 */ public Stu getbyID(int id){ Stu stu=null; try { stu=(Stu) this.GetSession().get(Stu.class, id); } catch (HibernateException e) { throw e; }finally{ this.CloseSession(); } return stu; } /** * 根据对象属性查询(使用Query) */ public List getbyPropertyQuery(String name){ List arr=null; try { //这里不能像SQL语一样select * from Stu where SName=:name,这是不对的。 // Query query=this.GetSession().createQuery(from Stu where SName=:name); // query.setString(name, name); //或者 Query query=this.GetSession().createQuery(from Stu where SName=?); query.setString(0, name); arr=query.list(); } catch (HibernateException e) { throw e; }finally{ this.CloseSession(); } return arr; } /** * 根据对象属性查询(使用Criteria) */ public List getbyPropertyCriteria(String name){ List arr=null; try { Criteria cri=this.GetSession().createCriteria(Stu.class); Criterion c1=Expression.eq(SName, name); cri.add(c1); arr=cri.list(); } catch (HibernateException e) { throw e; }finally{ this.CloseSession(); } return arr; } /** * 查询部分属性 */ public List getProperty(){ List arr=new ArrayList(); try { String hql=select s.SName,s.SSex from Stu as s; Query query=this.GetSession().createQuery(hql); List list=query.list(); Iterator iter=list.iterator(); while(iter.hasNext()){ Object[] obj=(Object[]) iter.next(); Stu s=new Stu(); s.setSName(obj[0].toString()); s.setSSex(obj[1].toString()); arr.add(s); } } catch (HibernateExcepti

文档评论(0)

1亿VIP精品文档

相关文档