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. Kafka vs RocketMQ—— Topic数量对单机性能的影响

    引言 上一期我们对比了三类消息产品(Kafka.RabbitMQ.RocketMQ)单纯发送小消息的性能,受到了程序猿们的广泛关注,其中大家对这种单纯的发送场景感到并不过瘾,因为没有任何一个网站的业务 ...

  2. JAVA-JSP内置对象

    相关资料:<21天学通Java Web开发> request 请求对象 类型javax.servlet.ServletRequest 作用域Requestresponse 响应对象 类型j ...

  3. iis部署wcf服务

    win8的如下 . 打开iis新建一个应用程序MyWcfTest 检查iis中的处理程序映射,含有svc说明激活了. 然后把svc文件和webconfig放入到指定的目录上. 使用地址http://l ...

  4. 编译 Linux 3.5 内核烧写 Android 4.2.2 到 Tiny4412 开发板

    . . . . . 昨天已经编译了 Android 4.2.2 的源码,详见<Ubuntu 14.04 编译 Android 4.2.2 for Tiny4412>一文. 今天我们继续剩下 ...

  5. APUE信号-程序汇总

    APUE信号-程序汇总      近期重看APUE,发现对于非常多程序的要领还是没有全然理解.所以梳理下便于查看,并且有非常多值得思考的问题. 程序清单10- 1  捕获 SIGUSR1 和 SIGU ...

  6. PHPUnit 在phpstrom中composer项目的应用配置

    在phpstorm的composer搭建的项目调试时出现这种错误时:是其配置的错误 'Cannot create phar '/data/AppStorm/DesignPatternsPHP/vend ...

  7. SQL 增删改查

    create table [表名]([自动编号字段] int IDENTITY (1,1) PRIMARY KEY ,[字段1] nVarChar(50) default \'默认值\' null , ...

  8. awk mysql

    mysqlbinlog --no-defaults --base64-output=decode-rows -v -v mysql-bin.000002 |awk '/###/{if($0~/UPDA ...

  9. node学习笔记2——搭建服务器

    搭建服务器关键词: require('http') —— 请求 node 里面的 http 模块 createServer —— 创建一个服务器 request —— 请求 response—— 响应 ...

  10. jquery 实现下拉菜单

    Jquery 是一个轻量的框架,个人认为非常好用,今天就写一个非常简单的例子,实现下拉菜单功能: 首先肯定要在页面引用jquery.js  版本不限 : 接下来把=================== ...