已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年江苏省扬中市高三物理下册期末考试模拟考试卷及参考答案
- 洞口县2025届四下数学期末综合测试试题含答案解析
- 洛阳市涧西区2025-2026学年三年级数学第二学期期末统考模拟试题(含答案)
- 泰安市肥城市2025年数学三下期末质量检测试题(含答案解析)
- 农业数字化技术员安全应急强化考核试卷含答案
- 保安员岗前规程考核试卷含答案
- 压延玻璃成型工岗前时间管理考核试卷含答案
- 化学制浆工岗前理论评估考核试卷含答案
- 兴趣点地理信息采集员岗位工作能力考核试卷含答案
- 水产品冻结工岗前技能实操考核试卷含答案
- 中国慢性乙型肝炎功能性(临床)治愈临床实践专家共识课件
- 苏少版美术四年级上册第三课《精彩的表达》教学课件
- 航空文化考试题及答案解析
- 2027物理步步高 大一轮复习第六章 微点突破3 机车启动问题
- 2026年辽宁省中考数学试卷(含答案及解析)
- GB/T 24002.2-2026环境管理体系针对环境主题领域应用GB/T 24001管理环境因素和应对环境状况的指南第2部分:水
- 聚变装置-氚聚变设施和聚变燃料处理设施的密封和通风系统的设计和操作标准标准立项发展报告
- 家装水电施工工艺标准
- 2026年度江西省党史知识竞赛考试复习重点试题(含答案)
- 2026中国气象量子计算技术研发进展与产业化前景
- 赣州市属国企招聘笔试题库2026
评论
0/150
提交评论