hql Hibernate.gethibernatetemplate()
1. find(String hql); //普通查询
示例:this.gethibernateTemplate().find("from User");
2. find(String hql,Object value);//一个查询条件
示例:this.gethibernateTemplate().find("from User u where u.name=?","test");
3. find(String hql,Object[] values);// 多个查询条件
示例:this.gethibernateTemplate().find("from User u where u.name=? and u.pwd=?",new String[]{"test","123"});
4. findByExample(Object exampleEntity,int firstResult, int maxResults)//分页使用
示例:
User user= new User(); u.setActive("Active");
List list=this.getHibernateTemplate().findByExample(user,firstResult,maxResults);
查询结果:状态为Active的用户(对象从0到20 计数)
5. findByNamedParam(String hql,String paramName,Object value); //一个查询条件
示例:
hql="from User u where u.name=:parName ";
paramName= "parName";
value="bb"
List list = this.getHibernateTemplate.findByNamedParam(hql,paramName,value);
查询结果:姓名为bb的用户
6. findByNamedParam(String queryString , String[] paramName , Object[] value) //多个查询条件
示例:
hql="from User u where u.name=:myname and u.pwd =:mypwd ";
String[] paramName= new String[]{"myname","mypwd"};
Sring[] value=new Strign[]{"bb","123"};
List list = this.getHibernateTemplate.findByNamedParam(hql,paramName,value);
查询结果:姓名为bb密码为123的用户
7.分页HQL示例
public List excuteHqlPage(final String hqlStr, final int startRow,final int rowCount) throws DaoException {
List<Object[]> list;
try {
list = getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
org.hibernate.Query query = (org.hibernate.Query) session.createQuery(hqlStr);
query.setFirstResult(startRow);// 定义从第几条开始查询
query.setMaxResults(rowCount);// 定义返回的记录数
List list = query.list();
return list;
}
});
} catch (Exception e) {
throw new DaoException(DaoException.ERRORCODE_EXCUTEHQL);
}
return list;
}
8. 根据HQL/SQL 查询
public List queryByHql(final String hql, final Object[] prams,final String sql) {
return (List) getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
if(hql!=null && hql.length()>0){
Query query=session.createQuery(hql);
if(prams!=null && prams.length>0){
for(int i=0;i<prams.length;i++){
query.setParameter(i,prams[i]);
}
}
return query.list();
}else{
SQLQuery sqlquery=session.createSQLQuery(sql);
return sqlquery.list();
}
}
});
}
9. 保存/更新
public String saveOrUpdateObject(ISuperVO vo) throws DaoException {
try {
String id = null;
if (StringUtil.isEmpty(vo.getPid())) {
getHibernateTemplate().save(vo);
} else {
getHibernateTemplate().merge(vo);
}
id = vo.getPid();
return id;
} catch (Exception e) {
e.printStackTrace();
}
}
10. getHibernateTemplate().delete(vo); //删除
11. 根据条件删除
public Integer deleteObjectsByWherePart(final Class voClass,final String wherePart, final Object[] parmaters)throws DaoException {
try {
Integer count = (Integer) getHibernateTemplate().execute( new HibernateCallback() {
public Object doInHibernate(Session session)throws HibernateException, SQLException {
Integer coun = null;
String hql = "delete from " + voClass.getName()+ " where 1=1 ";
if (wherePart != null && wherePart.trim().length() > 0) {
hql = hql + " and " + wherePart;
}
Query query = session.createQuery(hql);
Object obj = null;
if (parmaters != null && parmaters.length > 0) {
for (int i = 0; i < parmaters.length; i++) {
obj = parmaters[i];
query.setParameter(i, obj);
}
}
coun = query.executeUpdate();
return coun;
}
});
return count;
} catch (Exception e) {
e.printStackTrace();
}
}
hql Hibernate.gethibernatetemplate()的更多相关文章
- paip.取当天记录的方法sql跟hql hibernate
		paip.取当天记录的方法sql跟hql hibernate #------两个方法...函数法和日期计算法.. 函数法: DATEDIFF(d,createTime,GETDATE())=0 / ... 
- HQL: Hibernate查询语言
		HQL: Hibernate查询语言 Hibernate配备了一种非常强大的查询语言,这种语言看上去很像SQL.但是不要被语法结构 上的相似所迷惑,HQL是非常有意识的被设计为完全面向对象的查询,它可 ... 
- hql(Hibernate Query Language)
		1.Criteria查询对查询条件进行了面向对象封装,符合编程人员的思维方式,不过HQL(Hibernate Query Language)查询提供了更加丰富的和灵活的查询特性,因此Hibernate ... 
- 类型:。net;问题:HQL;结果:HQL: Hibernate查询语言
		HQL: Hibernate查询语言 Hibernate配备了一种非常强大的查询语言,这种语言看上去很像SQL.但是不要被语法结构 上的相似所迷惑,HQL是非常有意识的被设计为完全面向对象的查询,它可 ... 
- Hibernate查询(HQL——Hibernate Query Language)
		HQL查询 HQL提供了是十分强大的功能,它是针对持久化对象,用取得对象,而不进行update,delete和insert等操作.而且HQL是面向对象的,具备继承,多态和关联等特性. from子句: ... 
- 常用HQL(Hibernate Query Language)查询
		查询一个对象(实体类必须有一个不带参数的构造方法) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 @Test public void test01() ... 
- Hibernate之HQL查询的一些例子
		Hibernate配备了一种非常强大的查询语言,就是HQL(hibernate query language),HQL看上去很像sql,但只是语法结构上相似,HQL是一种面向对象的查询,他可以理解继承 ... 
- SSH整合报错:org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped[......]
		非常诡异的报错,信息如下:org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped [select count(* ... 
- Hibernate学习-Hibernate查询语言HQL
		HQL(Hibernate Query Language)Hibernate查询语言,语法类似于SQL,可以直接使用实体类及属性. 使用HQL 可以避免使用JDBC 查询的一些弊端 不需要再编写繁复的 ... 
随机推荐
- Scala开发入门指南
			作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs 一.下载Scala 当前Scala的最新版本为2.10.2版,Windows有两种发布包: 1)Windo ... 
- 1. CNN卷积网络-初识
			1. CNN卷积网络-初识 2. CNN卷积网络-前向传播算法 3. CNN卷积网络-反向更新 1. 前言 卷积神经网络是一种特殊的深层的神经网络模型,它的特殊性体现在两个方面, 它的神经元间的连接是 ... 
- SHT30 Linux标准 i2c-dev 读取程序
			#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/sta ... 
- [译]Godot系列教程六 - 简单的二维游戏
			Pong Godot自带的Demo中有大量更复杂的示例,但这款叫"Pong"的游戏可以对2D游戏的基本特性做一个介绍. 静态资源 本文所用到的一些资源文件:http://files ... 
- Android——修改Button样式,动态修改Button中的图片大小
			原文地址: http://www.cnblogs.com/gzggyy/archive/2013/05/17/3083218.html http://www.xuebuyuan.com/2173740 ... 
- Linux高频命令精讲(三)
			[教程主题]:2.Linux高频命令精讲 [2.1]Linux的运行方式 图形运行方式 - 本地使用KDE/Gnome集成环境 - 运行X Server远程使用图形环境 命令行(字符运行)方式 - 本 ... 
- Extjs4.2 Grid搜索Ext.ux.grid.feature.Searching的使用
			背景 Extjs4.2 默认提供的Search搜索,功能还是非常强大的,只是对于国内的用户来说,还是不习惯在每列里面单击好几下再筛选,于是相当当初2.2里面的搜索,更加的实用点,于是在4.2里面实现. ... 
- mysql国内镜像下载网址
			http://mirrors.sohu.com/mysql/ http://mirrors.ustc.edu.cn/mysql-ftp/Downloads/ 开源镜像站点汇总 http://segme ... 
- PCL中可用的PointT类型
			PCL中可用的PointT类型: PointXYZ——成员变量:float x,y,z; PointXYZ是使用最常见的一个点数据类型,因为他之包含三维XYZ坐标信息,这三个浮点数附加一个浮点数来满足 ... 
- java生成word的完美解决方案
			http://www.360doc.com/content/13/0731/10/13247663_303740756.shtml —————————————————————————————————— ... 
