- 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
您可能关注的文档
最近下载
- 安全标志GB+2894-2025 - 副本.docx VIP
- 二维转三维.doc VIP
- 四4班 四年级德育核心素养提升训练(第12周)测试卷及答案.docx VIP
- 15ZJ201_平屋面.pdf VIP
- 基于PLC的自动化生产线设计毕业设计论文.docx VIP
- 两票三制培训精要.pptx
- 伟创(VEICH)AC100系列矢量型变频调速器使用说明书V1.2.pdf
- 标准图集-11G101-2_混凝土结构施工图平面整体表示方法制图规则和构造详图(现浇混凝土板式楼梯)(替代03G101-2).pdf VIP
- 2.4 植物的生长变化(分层作业)科学冀人版三年级下册2026.pdf VIP
- (2026年)成人腰大池引流护理PPT课件.pptx VIP
原创力文档

文档评论(0)