已阅读5页,还剩16页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
/* 添加*/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;finallythis.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;finallythis.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;finallythis.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;finallythis.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;finallythis.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(obj0.toString();s.setSSex(obj1.toString();arr.add(s); catch (HibernateException e) this.CloseSession();return arr;/* 查询一个属性*/public List getoneProperty()List arr=new ArrayList();try String hql=select s.SName from Stu as s;Query query=this.GetSession().createQuery(hql);Iterator iter=query.iterate();while(iter.hasNext()Object obj=(Object) iter.next();Stu s=new Stu();s.setSName(obj.toString();arr.add(s); catch (HibernateException e) this.CloseSession();return arr;/*查询一个对象一个属性值*/public Object getonlyProprotyValue(int s_id)Object obj=null;try String hql=select s.SName from Stu as s where s.SId=?;Query query=this.GetSession().createQuery(hql);query.setInteger(0, s_id);obj=query.uniqueResult(); catch (HibernateException e) throw e;finallythis.CloseSession();return obj;/* SQL查询*/public List getallBYSQL()List arr=null;try String sql=select c.* from stu as c;SQLQuery sqlquery=this.GetSession().createSQLQuery(sql);sqlquery.addEntity(c,Stu.class);arr=sqlquery.list(); catch (HibernateException e) throw e;finallythis.CloseSession();return arr;/* 根据对象查询*/public List getallByObject(Stu s)List arr=null;try String hql=from Stu as s where s=:stuentity;/或者/String hql=from Stu as s where s.SId=:stuentity;Query query=this.GetSession().createQuery(hql);query.setEntity(stuentity, s);arr=query.list(); catch (HibernateException e) throw e;finallythis.CloseSession();return arr;/* 模糊查询*/public List getallQueryLike(String name)List arr=null;try String hql=from Stu as s where s.SName like :name;Query query=this.GetSession().createQuery(hql);query.setString(name, %+name+%);/不能/query.setString(name, %+name+%);arr=query.list(); catch (HibernateException e) throw e;finallythis.CloseSession();return arr;/* 统计函数*/public int CountStu()int count=0;try String hql=select count(*) from Stu;Query query=this.GetSession().createQuery(hql);count=(Integer) query.uniqueResult(); catch (HibernateException e) throw e;finallythis.CloseSession();return count;finallythis.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;finallythis.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;finallythis.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;finallythis.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;finallythis.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(obj0.toString();s.setSSex(obj1.toString();arr.add(s); catch (HibernateException e) this.CloseSession();return arr;/* 查询一个属性*/public List getoneProperty()List arr=new ArrayList();try String hql=select s.SName from Stu as s;Query query=this.GetSession().createQuery(hql);Iterator iter=query.iterate();while(iter.hasNext()Object obj=(Object) iter.next();Stu s=new Stu();s.setSName(obj.toString();arr.add(s); catch (HibernateException e) this.CloseSession();return arr;/*查询一个对象一个属性值*/public Object getonlyProprotyValue(int s_id)Object obj=null;try String hql=select s.SName from Stu as s where s.SId=?;Query query=this.GetSession().createQuery(hql);query.setInteger(0, s_id);obj=query.uniqueResult(); catch (HibernateException e) throw e;finallythis.CloseSession();return obj;/* SQL查询*/public List getallBYSQL()List arr=null;try String sql=select c.* from stu as c;SQLQuery sqlquery=this.GetSession().createSQLQuery(sql);sqlquery.addEntity(c,Stu.class);arr=sqlquery.list(); catch (HibernateException e) throw e;finallythis.CloseSession();return arr;/* 根据对象查询*/public List getallByObject(Stu s)List arr=null;try String hql=from Stu as s where s=:stuentity;/或者/String hql=from Stu as s where s.SId=:stuentity;Query query=this.GetSession().crea
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 少儿舞蹈启蒙老师岗位招聘考试试卷及答案
- 桥梁检测工程师考试试卷及答案
- 英国脱欧最好的协议书
- 爬虫数据爬取效率优化课程设计
- 基金产品保本保收益协议书
- 签署战略协议书中科海讯
- 婚前房产公证离婚协议书
- 高压配电室代管协议书
- 音乐作品分发使用协议
- 签了保密协议书需要多久
- 2025-2026学年北师大版八年级数学下学期期末测试卷(含答案)
- 2025年苏州市社区工作者招聘考试笔试试题及答案解析
- 2026年嘉兴市卫生行政系统事业单位人员招聘考试备考试题及答案详解
- 2026陕西西安市事业单位招聘高层次及紧缺特殊专业人才352人笔试备考题库及答案解析
- 高速公路日常养护作业操作规程养护施工工艺
- 2026年宁夏电投永利能源有限公司公开招聘笔试参考题库及答案详解
- 海姆立克急救技术操作流程及评分标准(2026版)
- 2026年安徽省淮南市重点学校小升初数学考试题库及答案
- 2026年中考生物常考知识点精简版
- 东航机考笔试行测题库
- 设备润滑管理规定培训
评论
0/150
提交评论