|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
但是一些大型开发或者是保密型比较高的项目都会用java,原因有2点,一:java是开源的,不怕别人留后门,偷我工具,.net就不一样了,保持微软的一向风格,源代码不公开
(一)HQL
HQL:HibernateQuseryLanguage,假如你已熟习它,就会发明它跟SQL十分相像。不外你不要被外表的假象利诱,HQL是面向对象的(OO,用性命的目光对待每个对象,他们是云云新鲜)。假如你对JAVA和SQL语句有必定懂得的话,那末HQL对你几乎轻而易举,你完整能够使用在公车上的工夫把握它。以下从几个方面举行渐渐深切:1。巨细些敏感人人晓得SQL-92Query是对巨细写不敏感的,可是在HQL(后面提到它是OO的)中对对象类的称号和属性的确巨细写敏感的(切合java编程语法)。
HQL子句自己巨细写有关,可是个中呈现的类名和属性名必需注重巨细写辨别如:sElectcat.namefromCatascat和selectcat.namefromCatascat是一样的可是:sElectcat.namefromCATascat和selectcat.namefromCatascat的确纷歧样的。2。from语句最复杂的:fromeg.Cat它只是复杂的前往一切eg.Cat的实例,一般我们此时会为eg.Cat其一般名,由于在query的其他部分大概会用到(参看上边关于巨细写敏感时的例子情况),如:fromeg.Catascat这里as能够省略。
上边只是单表查询,多表的情形以下写法:fromeg.Cat,eg.Dogfromeg.Catascat,eg.Dogasdog3。join相干(inner)joinleft(outer)joinright(outer)joinfulljoinHQL一样对SQL中的这些特征撑持上面插播一个小话题,关于上边的那些特征,我一向都没怎样用,明天既然说到这里,就想把上边的几个特征的用法说一下,也算对本人的一个增补:
假定有两个表:部门、员工,上面枚举一些数据:员工(Employee):IDNameDepNo001Jplateau01002Jony01003Camel02
部门(Department):IDName01研发部02营销部在Hibernate中我们利用的都是对象,以是我们利用的是部门类和员工类
1).(inner)joinselectemployee.IDasid1,employee.Nameasname1,
department.IDasid2,department.Nameasname2fromEmployeeasemployee
joinDepartmentasdepartmentonemployee.DepNo=department.ID(注重到前提语句我用on没有效where)那末实行了局是甚么呢?id1name1id2name2++++++++++++++++++++++++++++++++++++++001Jplateau01研发部002Jony01研发部2).left(outer)joinselectemployee.IDasid1,employee.Nameasname1,department.IDasid2,department.Nameasname2fromEmployeeasemployeeleftjoinDepartmentasdepartmentonemployee.DepNo=department.ID那末实行了局又该是甚么呢?id1name1id2name2++++++++++++++++++++++++++++++++++++++001Jplateau01研发部002Jony01研发部003Camelnullnull{就是说此时我要已第一个表的纪录几为准,第二个表中没有响应记录的时分添补null}3).right(outer)joinselectemployee.IDasid1,employee.Nameasname1,department.IDasid2,department.Nameasname2fromEmployeeasemployeerightjoinDepartmentasdepartmentonemployee.DepNo=department.ID那末实行了局又该是甚么呢?id1name1id2name2++++++++++++++++++++++++++++++++++++++001Jplateau01研发部002Jony01研发部nullnull02营销部{就是说此时我要已第二个表的纪录几为准,第一个表中没有响应记录的时分添补null}4。select语句就是要断定你要从查询中前往哪些对象大概哪些对象的属性。写几个例子吧:selectemployeeformEmployeeasemployeeselectemployeeformEmployeeasemployeewhereemployee.NamelikeJ%selectemployee.NameformEmployeeasemployeewhereemployee.NamelikeJ%selectemployee.IDasid1,employee.Nameasname1,department.IDasid2,department.Nameasname2fromEmployeeasemployeerightjoinDepartmentasdepartmentonemployee.DepNo=department.IDselectelements(employee.Name)fromEmployeeasemployee(不分明elements究竟是做甚么用的?看给于申明)等等
5。数学函数JDO今朝仿佛还不撑持此类特征。avg(...),sum(...),min(...),max(...)count(*)count(...),count(distinct...),count(all...)其用法和SQL基础不异selectdistinctemployee.namefromEmployeeasemployeeselectcount(distinctemployee.name),count(employee)fromEmployeeasemployee6。polymorphism(临时不晓得怎样注释?)fromcom.test.Animalasanimal不但失掉一切Animal得实例,并且能够失掉一切Animal的子类(假如我们界说了一个子类Cat)一个对照极度的例子fromjava.lang.Objectaso能够失掉一切耐久类的实例7。where语句界说查询语句的前提,举几个例子吧:fromEmployeeasemployeewhereemployee.Name=JplateaufromEmployeeasemployeewhereemployee.NamelikeJ%fromEmployeeasemployeewhereemployee.Namelike%u在where语句中“=”不但能够对照对象的属性,也能够对照对象,如:selectanimalfromcom.test.Animalasanimalwhereanimal.name=dog8。表达式在SQL语句中年夜部分的表达式在HQL中都可使用:mathematicaloperators+,-,*,/binarycomparisonoperators=,>=,<=,,!=,likelogicaloperationsand,or,notstringconcatenation||SQLscalarfunctionslikeupper()andlower()Parentheses()indicategroupingin,between,isnullJDBCINparameters?namedparameters:name,:start_date,:x1(这类应当是另外一种"?"的变通办理办法)SQLliteralsfoo,69,1970-01-0110:00:01.0Javapublicstaticfinalconstantseg.Color.TABBY其他不用注释了,在这里我只想对查询中的参数成绩申明一下:人人晓得在SQL中举行传送参数举行查询的时分,我们一般用PreparedStatement,在语句中写一年夜堆的“?”,在hql中也能够用这类办法,如:Listmates=sess.find("selectemployee.namefromEmployeeasemployee"+"whereemployee.Name=?",name,Hibernate.STRING);(申明:下面使用Session里的find办法,在hibernate的apiSession中重载了良多find办法,它能够满意你多种情势的查询)上边是一个参数的情况,这类情形下紧接着引进参数和界说参数的范例,当为多个参数,挪用另外一个find办法,它的后两个参数都是数组的情势。另有别的一种办法来办理上边的成绩,JDO也有如许的办法,不外和hibernate的体现情势上有不同,但他们两个骨子里倒是一样的,如:Queryq=sess.createQuery("selectemployee.namefromEmployeeasemployeewhereemployee.Name=:name");q.setString("name","Jplateau");//当有多个参数的时分在此一一界说Iteratoremployees=q.iterate();9。order语句和sql语句没甚么不同,如:selectemployee.namefromEmployeeasemployeewhereemployee.NamelikeJ%orderbyemployee.IDdesc(大概asc)10。groupby语句一样和sql语句没甚么不同,如:selectemployee.name,employee.DepNofromEmployeeasemployeegroupbyemployee.DepNoselectfoo.id,avg(elements(foo.names)),max(indices(foo.names))fromeg.Foofoogroupbyfoo.id{Note:Youmayusetheelementsandindicesconstructsinsideaselectclause,evenondatabaseswithnosubselects.}谁帮我注释一下上边两句,谢过!11。子查询hibernate一样撑持子查询,写几个例子:fromeg.Catasfatcatwherefatcat.weight>(selectavg(cat.weight)fromeg.DomesticCatcat)(二)前提查询CriteriaQuery
。数学函数JDO今朝仿佛还不撑持此类特征。avg(...),sum(...),min(...),max(...)count(*)count(...),count(distinct...),count(all...)其用法和SQL基础不异selectdistinctemployee.namefromEmployeeasemployeeselectcount(distinctemployee.name),count(employee)fromEmployeeasemployee6。polymorphism(临时不晓得怎样注释?)fromcom.test.Animalasanimal不但失掉一切Animal得实例,并且能够失掉一切Animal的子类(假如我们界说了一个子类Cat)一个对照极度的例子fromjava.lang.Objectaso能够失掉一切耐久类的实例7。where语句界说查询语句的前提,举几个例子吧:fromEmployeeasemployeewhereemployee.Name=JplateaufromEmployeeasemployeewhereemployee.NamelikeJ%fromEmployeeasemployeewhereemployee.Namelike%u在where语句中“=”不但能够对照对象的属性,也能够对照对象,如:selectanimalfromcom.test.Animalasanimalwhereanimal.name=dog8。表达式在SQL语句中年夜部分的表达式在HQL中都可使用:mathematicaloperators+,-,*,/binarycomparisonoperators=,>=,<=,,!=,likelogicaloperationsand,or,notstringconcatenation||SQLscalarfunctionslikeupper()andlower()Parentheses()indicategroupingin,between,isnullJDBCINparameters?namedparameters:name,:start_date,:x1(这类应当是另外一种"?"的变通办理办法)SQLliteralsfoo,69,1970-01-0110:00:01.0Javapublicstaticfinalconstantseg.Color.TABBY其他不用注释了,在这里我只想对查询中的参数成绩申明一下:人人晓得在SQL中举行传送参数举行查询的时分,我们一般用PreparedStatement,在语句中写一年夜堆的“?”,在hql中也能够用这类办法,如:Listmates=sess.find("selectemployee.namefromEmployeeasemployee"+"whereemployee.Name=?",name,Hibernate.STRING);(申明:下面使用Session里的find办法,在hibernate的apiSession中重载了良多find办法,它能够满意你多种情势的查询)上边是一个参数的情况,这类情形下紧接着引进参数和界说参数的范例,当为多个参数,挪用另外一个find办法,它的后两个参数都是数组的情势。另有别的一种办法来办理上边的成绩,JDO也有如许的办法,不外和hibernate的体现情势上有不同,但他们两个骨子里倒是一样的,如:Queryq=sess.createQuery("selectemployee.namefromEmployeeasemployeewhereemployee.Name=:name");q.setString("name","Jplateau");//当有多个参数的时分在此一一界说Iteratoremployees=q.iterate();9。order语句和sql语句没甚么不同,如:selectemployee.namefromEmployeeasemployeewhereemployee.NamelikeJ%orderbyemployee.IDdesc(大概asc)10。groupby语句一样和sql语句没甚么不同,如:selectemployee.name,employee.DepNofromEmployeeasemployeegroupbyemployee.DepNoselectfoo.id,avg(elements(foo.names)),max(indices(foo.names))fromeg.Foofoogroupbyfoo.id{Note:Youmayusetheelementsandindicesconstructsinsideaselectclause,evenondatabaseswithnosubselects.}谁帮我注释一下上边两句,谢过!11。子查询hibernate一样撑持子查询,写几个例子:fromeg.Catasfatcatwherefatcat.weight>(selectavg(cat.weight)fromeg.DomesticCatcat)(二)前提查询CriteriaQuery
Criteriacriteria=session.createCriteria(TUser.class);criteria.add(Expression.eq("name","Erica"));criteria.add(Expression.eq("sex",newInteger(1)));
(三)原生SQL语句查询
但是一些大型开发或者是保密型比较高的项目都会用java,原因有2点,一:java是开源的,不怕别人留后门,偷我工具,.net就不一样了,保持微软的一向风格,源代码不公开 |
|