hibernatehql查询:简述Hibernate在HQL中查询例子

  我们学习Hibernate查询是知道HQL支持条件查询分为好多方面这里我们就具体Hibernate查询用例子来加以介绍说明希望对读者学习带来帮助

  1、支持串方式参数传递查询:

  例子:

List students = session.createQuery("select s.id,s.name from Student s where s.name like '%1' ").list;  
            for (Iterator iter=students.iterator; iter.hasNext;) {  
                Object o = (Object)iter.next;  
                .out.prln(o[0]+","+o[1]);  
            } 


  由于涉及到多个属性查询所以返回是Object类型

  2、支持类似于PrepareStatement方式?传递参数查询

  例子:

List students = session.createQuery("select s.id,s.name from Student s where s.name like :goodname ")  
                                                .Parameter("goodname", "%1%").list;  
            for (Iterator iter=students.iterator; iter.hasNext;) {  
               Object o = (Object)iter.next;  
               .out.prln(o[0]+","+o[1]);  
           } 


  要设置参数从0开始见第2行

  3、去变量方式参数传递

  例子:

List students = session.createQuery("select s.id,s.name from Student s where s.name like :goodname ")  
                                                .Parameter("goodname", "%1%").list;  
            for (Iterator iter=students.iterator; iter.hasNext;) {  
               Object o = (Object)iter.next;  
                .out.prln(o[0]+","+o[1]);  
           } 


  这个跟2中相似只是多了参数可以归为

  4、支持多参数传递

  例子:

List students = session.createQuery("select s.id, s.name from Student s where s.id in(:myids)")  
            .ParameterList("myids",  Object{1, 2, 3, 4, 5,32,13,14})  
                .list;  
            for (Iterator iter=students.iterator; iter.hasNext;) {  
                Object o = (Object)iter.next;  
                .out.prln(o[0]+","+o[1]);  
            } 


  注意思路方法是ParameterList

  5、数据库里面

  例子:

List students = session.createQuery("select s.id, s.name from Student s where date_format(s.createTime, '%Y-%m')=?")  
           .Parameter(0, "2008-10")  
                .list;  
           for (Iterator iter=students.iterator; iter.hasNext;) {  
                Object o = (Object)iter.next;  
               .out.prln(o[0]+","+o[1]);  
           } 


  数据库date_format

  6、直接支持sql查询

  例子:

List students = session.createSQLQuery("select *from t_student").list;  
            for (Iterator iter=students.iterator; iter.hasNext;) {  
               Object o = (Object)iter.next;  
                .out.prln(o[0]+","+o[1]);  
           } 




  只需要session里面createSQLQuery思路方法即可

  7、进行导航查询

  例子:

List students = session.createQuery("select s.name from Student s where s.es.name like '%1%'").list;  
            for (Iterator iter=students.iterator; iter.hasNext;) {  
               String s = (String)iter.next;  
                .out.prln(s);  
            } 


  这种查询很方便

  8、分页查询

  例子:

List students = session.createQuery("from Student").FirstResult(0).MaxResults(3).list;  
            for (Iterator iter=students.iterator; iter.hasNext;) {  
                Student student = (Student)iter.next;  
                .out.prln(student.getName);  
            } 


  这是很简单分页查询分页查询实现比这个复杂多了分页查询采用相当于数据库里面 limit 来限制次查询显示条数

  注:还有些其他就不列举了Hibernate查询在Hibernate使用中HQL查询语言如果用得好会给工作带来比较高效率比较重要而且还有个查询效率问题这涉及到缓存Cache等方面以后会更新这方面文章



Tags:  hibernatehql函数 hibernatehql语法 hibernatehql hibernatehql查询

延伸阅读

最新评论

发表评论