Cyh的博客

Email:kissyan4916@163.com
posts - 26, comments - 19, trackbacks - 0, articles - 220

HQL查询语句

Posted on 2009-02-16 19:42 啥都写点 阅读(639) 评论(0)  编辑  收藏 所属分类: J2EE

这是Hibernate官方所推荐的查询语言,接近SQL的语法,并提供更多的特性与封装。

àPerson表查询出年龄(distinct去掉重复的)

String hql = "SELECT distinct p.sex FROM Person AS p" ;

              Query q = session.createQuery(hql) ;

              List all = q.list();

              Iterator iter = all.iterator();

àString hql = "SELECT COUNT(p.id) FROM Person AS p" ;

              Query q = session.createQuery(hql) ;

              List all = q.list();

à按字段查询

String hql = "FROM Person AS p WHERE p.name=?" ;

              Query q = session.createQuery(hql) ;

              q.setString(0, "张三") ;

              List all = q.list();

à更新操作

       String hql = "UPDATE Person SET sex=? WHERE id=?" ;

              Query q = session.createQuery(hql) ;

              q.setString(0, "Ů") ;

              q.setInteger(1, 1) ;

              q.executeUpdate() ;

              session.beginTransaction().commit() ;

à分页

String hql = "FROM Person AS p" ;

              Query q = session.createQuery(hql) ;

              q.setFirstResult(2) ; //从第几行取出

              q.setMaxResults(2); //取多少条

              List all = q.list();

              Iterator iter = all.iterator();



                                                                                                       --    学海无涯
        


只有注册用户登录后才能发表评论。


网站导航: