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()的更多相关文章

  1. paip.取当天记录的方法sql跟hql hibernate

    paip.取当天记录的方法sql跟hql hibernate #------两个方法...函数法和日期计算法.. 函数法: DATEDIFF(d,createTime,GETDATE())=0   / ...

  2. HQL: Hibernate查询语言

    HQL: Hibernate查询语言 Hibernate配备了一种非常强大的查询语言,这种语言看上去很像SQL.但是不要被语法结构 上的相似所迷惑,HQL是非常有意识的被设计为完全面向对象的查询,它可 ...

  3. hql(Hibernate Query Language)

    1.Criteria查询对查询条件进行了面向对象封装,符合编程人员的思维方式,不过HQL(Hibernate Query Language)查询提供了更加丰富的和灵活的查询特性,因此Hibernate ...

  4. 类型:。net;问题:HQL;结果:HQL: Hibernate查询语言

    HQL: Hibernate查询语言 Hibernate配备了一种非常强大的查询语言,这种语言看上去很像SQL.但是不要被语法结构 上的相似所迷惑,HQL是非常有意识的被设计为完全面向对象的查询,它可 ...

  5. Hibernate查询(HQL——Hibernate Query Language)

    HQL查询 HQL提供了是十分强大的功能,它是针对持久化对象,用取得对象,而不进行update,delete和insert等操作.而且HQL是面向对象的,具备继承,多态和关联等特性. from子句: ...

  6. 常用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() ...

  7. Hibernate之HQL查询的一些例子

    Hibernate配备了一种非常强大的查询语言,就是HQL(hibernate query language),HQL看上去很像sql,但只是语法结构上相似,HQL是一种面向对象的查询,他可以理解继承 ...

  8. SSH整合报错:org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped[......]

    非常诡异的报错,信息如下:org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped [select count(* ...

  9. Hibernate学习-Hibernate查询语言HQL

    HQL(Hibernate Query Language)Hibernate查询语言,语法类似于SQL,可以直接使用实体类及属性. 使用HQL 可以避免使用JDBC 查询的一些弊端 不需要再编写繁复的 ...

随机推荐

  1. django模型创建

    定义模型 模型,属性,表,字段之间的关系 一个模型类在数据库中对应一张表,在模型类中定义的属性,对应该模型对照表中的一个字段 定义属性:见下文 创建模型类 元选项 在模型类中定义Meta类,用于设置元 ...

  2. C# Dictionary通过value获取对应的key值

    1:最直白的循环遍历方法,可以分为遍历key--value键值对以及所有的key两种表现形式 2:用Linq的方式去查询(当然了这里要添加对应的命名空间 using System.Linq) 如下为一 ...

  3. tomcat8.5性能优化

    主要是按照两篇文章,部分参数因适配Tomcat8.5进行了调整 一.有关 JAVA_OPTS http://www.open-open.com/lib/view/open1401931407228.h ...

  4. Facebook的Fairseq模型详解(Convolutional Sequence to Sequence Learning)

    1. 前言 近年来,NLP领域发展迅速,而机器翻译是其中比较成功的一个应用,自从2016年谷歌宣布新一代谷歌翻译系统上线,神经机器翻译(NMT,neural machine translation)就 ...

  5. 未能加载文件或程序集”xxxx”或它的某一个依赖项,试图加载格式不正确的程序。

    通常是因为应用程序编译的目标平台与引用的DLL类库目标平台不一致造成的,如应用程序目标编译为64位,而引用了32位的DLL. 在Visual Studio修改应用程序目标编译平台即可. 更多关于目标编 ...

  6. Selenium (3) —— Selenium IDE + Firefox录制登录脚本(101 Tutorial)

    Selenium (3) -- Selenium IDE + Firefox录制登录脚本(101 Tutorial) selenium IDE版本: 2.9.1 firefox版本: 39.0.3 参 ...

  7. HTML select 选中触发事件

    $(function () { $("#cityidchange").change(function (data) { var cityid = $("#cityidch ...

  8. PCL学习八叉树

    建立空间索引在点云数据处理中有着广泛的应用,常见的空间索引一般 是自顶而下逐级划分空间的各种空间索引结构,比较有代表性的包括BSP树,KD树,KDB树,R树,四叉树,八叉树等索引结构,而这些结构中,K ...

  9. c++ 的makefile文件实例

    首先声明, 感谢九哥的帮助,因为从来没写过makefile, 所以一直是手动编译, 然后有一次写了三个文件, 需要编译, 而我只编译了一个文件, 所以一直出错, 九哥告诉我用makefile更方便, ...

  10. 第三百六十二节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)基本的索引和文档CRUD操作、增、删、改、查

    第三百六十二节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)基本的索引和文档CRUD操作.增.删.改.查 elasticsearch(搜索引擎)基本的索引 ...