Hibernate中query的常用方法

hibernate中的Query接口方法 hibernate中的Query接口方法Query的获取Query的方法参数绑定方法setXxx(int var1, Xxx var2)setXxx(String var1, Xxx var2)setParameterxxx()获取查询结果方法list()iterate()uniqueResult()语句执行方法executeUpdate()分页方法setFirstResult(int var1)setMaxResults(int var1) Query的获取 Query q = session.creat 阅读详情
/** 
* 添加 
*/ 
public void save(Stu stu){ 
   try { 
    tran=this.GetSession().beginTransaction(); 
    this.GetSession().save(stu); 
    tran.commit(); 
   } 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 (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; 
   }finally{ 
    this.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; 
   }finally{ 
    this.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; 
   }finally{ 
    this.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; 
   }finally{ 
    this.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; 
   }finally{ 
    this.CloseSession(); 
   } 
   return count; 
} 

   /** 
* 条件统计 
*/ 
public int CountByWhere(String sex){ 
   int count=0; 
   try { 
    Query query=this.GetSession().createQuery("select count(*) from Stu where SSex=:sex"); 
    query.setString("sex", sex); 
    count=(Integer)query.uniqueResult(); 
   } catch (HibernateException e) { 
    throw e; 
   }finally{ 
    this.CloseSession(); 
   } 
   return count; 
} 

/** 
* 统计平均值 
*/ 
public float VagAge(){ 
   float vag=0; 
   try { 
    Query query=this.GetSession().createQuery("select avg(SAge) from Stu"); 
    vag=(Float)query.uniqueResult(); 
   } catch (HibernateException e) { 
    throw e; 
   }finally{ 
    this.CloseSession(); 
   } 
   return vag; 
} 

/** 
* 求和函数 
*/ 
public int sumage(){ 
   int sum=0; 
   try { 
    Query query=this.GetSession().createQuery("select sum(SAge) from Stu"); 
    sum=(Integer)query.uniqueResult(); 
   } catch (HibernateException e) { 
    throw e; 
   }finally{ 
    this.CloseSession(); 
   } 
   return sum; 
}
时间序列预测 | Python实现ARIMAX和VARMAX时间序列数据预测 时间序列预测 | Python实现ARIMAX和VARMAX时间序列数据预测 目录时间序列预测 | Python实现ARIMAX和VARMAX时间序列数据预测基本介绍ARIMAXVARVARMAVARMAXSESHWES参考资料 基本介绍 本文介绍ARIMAX、VAR、VARMA和VARMAX基本概念及其Python程序设计。 ARIMAX 具有外源回归量的自回归整合移动平均值(ARIMAX)是 ARIMA 模型的扩展,其还包括外生变量的建模。 外生变量也称为协变量,可以被认为是并行输入序列,其在与 阅读详情

相关推荐

抖音批量取消关注神器:5行JS代码搞定,告别手动操作(附详细步骤)

本文介绍了一种利用JS脚本在抖音创作者平台批量取消关注的高效方法。通过浏览器开发者工具执行简单的JavaScript代码,即可自动化模拟点击操作,快速清理大量关注列表,告别繁琐的手动操作。文中提供了详细的步骤和核心代码,适合有一定技术基础的用户参考。

weixin_29009971的博客 594

Hibernate中的Query一些基本用法

/*** 添加*/public void save(Stu stu){   try {    tran=this.GetSession().beginTransaction();    this.GetSession().save(stu);    tran.commit();   } catch (HibernateException e) {    throw e;   }finally{  ...

我的IT技术杂谈 318

CIMPro孪大师,支持 3DGS 高斯泼溅的一站式零代码数字孪生平台

高斯泼溅导入数字孪生开发引擎

2501_90821319的博客 1108

hibernate 查询返回列表

1. 使用SQLQuery对原生SQL查询执行的控制是通过SQLQuery接口进行的,通过执行Session.createSQLQuery()获取这个接口。最简单的情况下,我们可以采用以下形式: List cats = sess.createSQLQuery( " select * from cats " ).addEntity(Cat. class ).list(); 这个查询指定了:...

wangzhantao的专栏 977

Visual Studio调试时遇到ntdll.dll的PDB文件缺失?别慌,这3个方法帮你搞定(附详细步骤)

本文详细解析了Visual Studio调试时ntdll.dll的PDB文件缺失问题,提供了3种实用解决方案:调整调试设置、配置符号服务器与缓存目录、手动下载管理符号文件。通过分步指导和高级技巧,帮助开发者快速恢复完整的调试功能,特别适用于Windows平台下的C++项目开发。

weixin_42537919的博客 513

【Python爬虫(1)】专栏开篇:夯实Python基础

要想熟练掌握 Python 爬虫技术,扎实的 Python 基础是必不可少的。这就好比建造高楼大厦,Python 基础就是那稳固的基石。只有打好了这个基础,我们才能在爬虫的学习道路上稳步前行,理解并运用各种爬虫技术和框架。

邓邓子的博客 2025

getSession().createQuery__getHibernateTemplate().find

getSession()是会自动释放连接的。但是在一个线程内getSession会get很多个session(就是开很多个会话、连接),很可能导致数据库连接超过上限。所以推荐使用getHibernateTemplate。”通过继承HibernateDaoSupport我们有两个选择: getSession().createQuery("from Users"); getHibernateTempla

qq_36047372的博客 791

Android SELinux——neverallow问题处理(十六)

遇到 neverallow 规则问题,千万别急着去注释/剔除里面原有的规则(原生的尽量别动)。增加 allow 规则是常见的解决办法,但是随着 android 版本的升级,系统对 SELinux 的管控越来越严,增加了大量的 neverallow。一般情况下,向默认标签授予权限的做法是错误的,其中许多权限都是 neverallow 规则所不允许的。按照上面方法去添加 SELinux 可能会违反了 neverallow 规则,编译时候会报 neverallow 相关的错误。

小旭的专栏 2658

getSession().createQuery(hql)带来莫名其妙的bug

使用SSH2框架开发了一套业务应用网站,在开发环境下运行正常,没暴露任何问题,这几天安排测试,安装了一台服务器,结果,四人测试一会就没响应了,由正常进入死锁状态有时时间长一点,有时时间短些,没有规律,也没发现具体引起错误的业务操作,故障发生的很偶然,很随机,顿时感到茫然,束手无措无措。 经过长时间反复改变测试方法,多人多次到单人单业务,逐个尝试,最后发现的规律是,某个报表连续点击保存按钮7次后,

li_hai的专栏 1万+

北斗导航 | 从北一到北二再到北三,你应该知道的北斗导航系统

博主github:https://github.com/MichaelBeechan 博主优快云:https://blog.csdn.net/u011344545 一、引言 我国高度重视北斗系统的建设和发展。自20世纪80年代开始探索适合我们国家国情的卫星导航系统发展道路,形成了“三步走”的发展战略:2000年底,建成北斗一号导航系统,向我国广大用户提供服务;2012年底,建成北斗二号导航系统,...

尘世冰封的专栏 3078

Open3D——RANSAC拟合球面【2026最新版】

RANSAC三维点云球面拟合,博客长期更新,本文最新更新时间为:2026年3月5日。

点云侠的博客 4083

Mysql语句大全及用法收集

通过 depth 与 parentID 调取全局一级分类方法 @Override /** 通过 depth 与 parentID 调取全局一级分类方法 */ public List getNameByParent(int depth, String id){ String hql="SELECT * FROM category WHERE depth=? AND parentID=? AND isValid=1 AND isLock=0 ORDER BY createdTime DESC".

Riqk_Qin的专栏 555

STM32声源定位:实现声源定位的嵌入式解决方案

通过以上代码,我们可以获取麦克风音频信号样本,并使用交叉相关法计算声源的位置。然而,我可以帮助您解答有关STM32声源定位的问题,提供相关的代码和指导。获取了麦克风音频信号后,我们可以使用信号处理算法来计算声源的位置。该算法通过计算不同麦克风之间的信号延迟来确定声源的方向。首先,我们需要获取每个麦克风的音频信号。声源定位是一项重要的技术,可以帮助我们确定声音的来源位置。在嵌入式系统中,使用STM32微控制器可以实现精确的声源定位。声源定位的基本原理是通过多个麦克风阵列接收到的声音信号来计算声源的位置。

UplKubernetes的博客 2581

Hibernate 中Query的list方法和iterate方法

Hibernate中用hql通过query进行查询的时候,通常会用list或者iterate取得查询到的结果.在此简单说明一下这两个方法的不同之处和适用场景. List:查询到的结果会被完整地放进内存(session的缓存),实体会处于持久状态.如果用迫切连接(fetch)模式,取得的结果可能会存在主表对应的一条记录会在list里存在多个实例(准确的讲是实例引用),多次出现但是指向同一个对

交换一个思想,能得到俩思想 9916

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

Hibernate:Query中list、iterate和uniqueResult三个方法的区别

想看下org.hibernate.Query接口中定义的三个方法(list、iterate和uniqueResult)有什么区别。  通过追踪地看一相关源码后,发现情况是这样的。 简单说来,它们的特点是这样的:     1,list方法是把所有满足条件的结果集都解析转换成Model对象,再把这些对象放到List实例中。     2,iterate调用了ResultSet的next...

rmn190 245

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

Hibernate的 SessionFactory,Configuration,Query ,Criteria接口简介,及Query的查询方法

关于SessionFactory接口 我们从SessionFactory工厂类中获取session实例, 事实上当我们的项目中只对应一个数据库时就不需要重复的创建session实例。 因为一个数据库对应一个session实例,SessionFactory在Hibernate中起到了一个缓冲区的作用,缓冲了一些 Hibernate自动生成的sql语句,一些映射数据和可能重复利用的数据。

sinat_35821285的博客 438

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

Hibernate 中 load方法与get方法以及Query查询与Criteria查询

2019独角兽企业重金招聘Python工程师标准>>> ...

weixin_34099526的博客 197

Hibernate之Query接口的uniqueResult 方法

Hibernate之Query接口的uniqueResult 方法

ytfghytf的博客 258

Hibernate里Query的list()和iterate()方法的区别

1.Query接口的list与iterate方法: iterate()方法: Return the query results as anIterator. Ifthe query contains multiple results pre row, the results are returned in aninstance ofObject[].Entities returned as ...

weixin_30413739的博客 191

Hibernate的Query的setProperties(Map map)方法的使用

Query的setProperties(Map map)的参数map,map的key要与HQL语句中对应实体类的相应属性名,例如下面的例子中的bid和bname的参数名是需要与Book类中的bid和bname属性对应的,这是对该map的要求。value是要查询的参数的值。简单地说就是Map的键和值要与实体类的属性名和值一一对应,才能成功的传入参数 使用Query的setPropert...

莫负时光 7591

Hibernate中设置query参数的方法

1.用hibernate建议的命名参数方法(推荐): List list = session.createQuery("from Student where dept=:sdept and age>:sage and sex=:sex") .setString("sdept", "计算机学院") .setParameter("sex", "男") .setInte

hom_va的专栏 4609

Hibernate之Query接口的uniqueResult()方法

如果查询返回多个值用list()方法 public void testQuery(){ Configuration config = new Configuration().configure(); SessionFactory factory = config.buildSessionFactory(); //创建SessionFactory Session session

itmyhome的专栏 3万+
上一篇: Spring集合类型值注入
下一篇: struts2整合json出现no result type defined for type 'json'的解决方法
张征宇
博客等级 码龄14年 4粉丝 8原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值