欢迎光临
我们一直在努力

hibernate 三种查询方式-JSP教程,Java技巧及代码

建站超值云服务器,限时71元/月

(一)hql

hql:hibernate qusery language,如果你已经熟悉它,就会发现它跟sql非常相像。不过 你不要被表面的假象迷惑,hql是面向对象的(oo,用生命的眼光看待每一个对象,他们是如此 鲜活)。如果你对java和sql语句有一定了解的话,那么hql对你简直易如反掌,你完全可以利用在公车上的时间掌握它。以下从几个方面进行慢慢深入:1。大小些敏感大家知道sql-92 query是对大小写不敏感的,但是在hql(前面提到它是oo的)中对对象类的名称和属性确实大小写敏感的(符合java编程语法)。

hql 子句本身大小写无关,但是其中出现的类名和属性名必须注意大小写区分如:select cat.name from cat as cat和select cat.name from cat as cat是一样的但是:select cat.name from cat as cat和select cat.name from cat as cat确实不一样的。2。from语句最简单的:from eg.cat 它只是简单的返回所有eg.cat的实例,通常我们此时会为eg.cat其个别名,因为在query的其余部分可能会用到(参看上边关于大小写敏感时的例子情形),如:from eg.cat as cat 这里as可以省略。

上边只是单表查询,多表的情况如下写法:from eg.cat, eg.dogfrom eg.cat as cat, eg.dog as dog3。join相关(inner) joinleft (outer) joinright (outer) joinfull joinhql同样对sql中的这些特性支持下面插播一个小话题,关于上边的那些特性,我一直都没怎么用,今天既然说到这里,就想把上边的几个特性的用法说一下,也算对自己的一个补充:

假设有两个表:部门、员工,下面列举一些数据:员工(employee):  id     name    depno  001   jplateau    01  002    jony        01  003   camel      02

部门(department):  id   name  01   研发部  02   营销部在hibernate中我们操纵的都是对象,所以我们操纵的是部门类和员工类

 

 

 

 

1).(inner) joinselect employee.id as id1,employee.name as name1,

department.id as id2,department.name as name2  from employee as employee

 join  department as department on employee.depno=department.id (注意到条件语句我用on 没有用where)那么执行结果是什么呢?id1 name1 id2 name2++++++++++++++++++++++++++++++++++++++001 jplateau 01 研发部002 jony 01 研发部2).left (outer) joinselect employee.id as id1,employee.name as name1,department.id as id2,department.nameas name2 from employee as employee left join department as department on employee.depno=department.id 那么执行结果又该是什么呢?id1 name1 id2 name2++++++++++++++++++++++++++++++++++++++001 jplateau 01 研发部002 jony 01 研发部 003 camel null null {就是说此时我要已第一个表的记录多少为准,第二个表中没有相应纪录的时候填充null} 3). right (outer) joinselect employee.id as id1,employee.name as name1,department.id as id2,department.nameas name2 from employee as employee right join department as department on employee.depno=department.id 那么执行结果又该是什么呢?id1 name1 id2 name2++++++++++++++++++++++++++++++++++++++001 jplateau 01 研发部002 jony 01 研发部 null null 02 营销部 {就是说此时我要已第二个表的记录多少为准,第一个表中没有相应纪录的时候填充null} 4。select语句就是要确定你要从查询中返回哪些对象或者哪些对象的属性。写几个例子吧:select employee form employee as employee select employee form employee as employee where employee.name like j%select employee.name form employee as employee where employee.name like j%select employee.id as id1,employee.name as name1,department.id as id2,department.nameas name2 from employee as employee right join department as department on employee.depno=department.id select elements(employee.name) from employee as employee (不明白elements到底是做什么用的?望给于说明) 等等

5。数学函数jdo目前好像还不支持此类特性。avg(…), sum(…), min(…), max(…) count(*) count(…), count(distinct …), count(all…) 其用法和sql基本相同select distinct employee.name from employee as employee select count(distinct employee.name),count(employee) from employee as employee 6。polymorphism (暂时不知道如何解释?)from com.test.animal as animal不光得到所有animal得实例,而且可以得到所有animal的子类(如果我们定义了一个子类cat)一个比较极端的例子from java.lang.object as o可以得到所有持久类的实例7。where语句定义查询语句的条件,举几个例子吧:from employee as employee where employee.name=jplateaufrom employee as employee where employee.name like j%from employee as employee where employee.name like %u在where语句中“=”不光可以比较对象的属性,也可以比较对象,如:select animal from com.test.animal as animal where animal.name=dog8。表达式在sql语句中大部分的表达式在hql中都可以使用:mathematical operators +, -, *, / binary comparison operators =, >=, <=, <>, !=, like logical operations and, or, not string concatenation || sql scalar functions like upper() and lower() parentheses ( ) indicate grouping in, between, is null jdbc in parameters ? named parameters :name, :start_date, :x1 (这种应该是另一种"?"的变通解决方法)sql literals foo, 69, 1970-01-01 10:00:01.0 java public static final constants eg.color.tabby 其他不必解释了,在这里我只想对查询中的参数问题说明一下:大家知道在sql中进行传递参数进行查询的时候,我们通常用preparedstatement,在语句中写一大堆的“?”,在hql中也可以用这种方法,如:list mates = sess.find("select employee.name from employee as employee " +"where employee.name=? ",name,hibernate.string);(说明:上面利用session里的find方法,在hibernate的api session中重载了很多find方法,它可以满足你多种形式的查询)上边是一个参数的情形,这种情况下紧接着引入参数和定义参数的类型,当为多个参数,调用另一个find方法,它的后两个参数都是数组的形式。还有另外一种方法来解决上边的问题,jdo也有这样的方法,不过和hibernate的表现形式上有差别,但他们两个骨子里却是一样的,如:query q = sess.createquery("select employee.name from employee as employee where employee.name=:name");q.setstring("name", "jplateau");//当有多个参数的时候在此逐一定义iterator employees = q.iterate(); 9。order 语句和sql语句没什么差别,如:select employee.name from employee as employee where employee.name like j% order by employee.id desc (或者asc)10。group by 语句同样和sql语句没什么差别,如:select employee.name,employee.depno from employee as employee group by employee.depnoselect foo.id, avg( elements(foo.names) ), max( indices(foo.names) ) from eg.foo foo group by foo.id{note: you may use the elements and indices constructs inside a select clause, even on databases with no subselects.}谁帮我解释一下上边两句,谢过!11。子查询hibernate同样支持子查询,写几个例子:from eg.cat as fatcat where fatcat.weight > ( select avg(cat.weight) from eg.domesticcat cat )(二)条件查询criteria  query
。数学函数jdo目前好像还不支持此类特性。avg(…), sum(…), min(…), max(…) count(*) count(…), count(distinct …), count(all…) 其用法和sql基本相同select distinct employee.name from employee as employee select count(distinct employee.name),count(employee) from employee as employee 6。polymorphism (暂时不知道如何解释?)from com.test.animal as animal不光得到所有animal得实例,而且可以得到所有animal的子类(如果我们定义了一个子类cat)一个比较极端的例子from java.lang.object as o可以得到所有持久类的实例7。where语句定义查询语句的条件,举几个例子吧:from employee as employee where employee.name=jplateaufrom employee as employee where employee.name like j%from employee as employee where employee.name like %u在where语句中“=”不光可以比较对象的属性,也可以比较对象,如:select animal from com.test.animal as animal where animal.name=dog8。表达式在sql语句中大部分的表达式在hql中都可以使用:mathematical operators +, -, *, / binary comparison operators =, >=, <=, <>, !=, like logical operations and, or, not string concatenation || sql scalar functions like upper() and lower() parentheses ( ) indicate grouping in, between, is null jdbc in parameters ? named parameters :name, :start_date, :x1 (这种应该是另一种"?"的变通解决方法)sql literals foo, 69, 1970-01-01 10:00:01.0 java public static final constants eg.color.tabby 其他不必解释了,在这里我只想对查询中的参数问题说明一下:大家知道在sql中进行传递参数进行查询的时候,我们通常用preparedstatement,在语句中写一大堆的“?”,在hql中也可以用这种方法,如:list mates = sess.find("select employee.name from employee as employee " +"where employee.name=? ",name,hibernate.string);(说明:上面利用session里的find方法,在hibernate的api session中重载了很多find方法,它可以满足你多种形式的查询)上边是一个参数的情形,这种情况下紧接着引入参数和定义参数的类型,当为多个参数,调用另一个find方法,它的后两个参数都是数组的形式。还有另外一种方法来解决上边的问题,jdo也有这样的方法,不过和hibernate的表现形式上有差别,但他们两个骨子里却是一样的,如:query q = sess.createquery("select employee.name from employee as employee where employee.name=:name");q.setstring("name", "jplateau");//当有多个参数的时候在此逐一定义iterator employees = q.iterate(); 9。order 语句和sql语句没什么差别,如:select employee.name from employee as employee where employee.name like j% order by employee.id desc (或者asc)10。group by 语句同样和sql语句没什么差别,如:select employee.name,employee.depno from employee as employee group by employee.depnoselect foo.id, avg( elements(foo.names) ), max( indices(foo.names) ) from eg.foo foo group by foo.id{note: you may use the elements and indices constructs inside a select clause, even on databases with no subselects.}谁帮我解释一下上边两句,谢过!11。子查询hibernate同样支持子查询,写几个例子:from eg.cat as fatcat where fatcat.weight > ( select avg(cat.weight) from eg.domesticcat cat )(二)条件查询criteria  query
criteria criteria = session.createcriteria(tuser.class);criteria.add(expression.eq("name",  "erica"));criteria.add(expression.eq("sex",  new integer(1)));

(三)原生sql语句查询

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » hibernate 三种查询方式-JSP教程,Java技巧及代码
分享到: 更多 (0)

相关推荐

  • 暂无文章