常用的sql語句和hql語句

2021-08-18 19:40:27 字數 1174 閱讀 1004

注:表名為students,持久化類student;

一:查詢

sql="select * from  students";

hql="from student"

二:結果排序

sql="select id from students order by id desc"(查詢學生的id並降序排列)

hql="from student order by id desc"

三:動態引數繫結查詢

sql:"select * from  students where age>? and score<?"(查詢所有學生中年齡大於?分數小於?的所有學生其中?是佔位符)

hql:"from  student where age>? and score<?"

hql:"from  student where age>:myage and score<:myscore"

四:分頁查詢

sql="select * from  students limit ?,?";(第乙個?為startindex第二個?為pagesize)

hql="from student"(同時需要呼叫setfirstresult()和setmaxresult方法)

五:模糊查詢

sql="select * from  students where name like?";(佔位符可輸入%張%,則查詢所有名字當中帶張的同學)

hql="from student where name like?"

六:唯一性查詢

hql="from student where id=?";

sql="select * from  students where name =?";

七:聚合函式查詢

hql="select count(*)from student ";

sql="select count(id)from student ";(id的總數)

八:投影查詢

hql="select new stdent(name,age) from student ";(關於hql投影查詢詳解可參看其他博文)

九:分組查詢

hql=" from student group by age ";

sql="select * from students group by age"(以學生年齡進行分組)

常用的HQL語句

1.hql更新 string hql update phuser set realname int row this.getsession createquery hql setstring 0,小李想 executeupdate phuser 類名 2.hql刪除 string hql delet...

hql使用sql語句的方法

今天在使用hibernate的時候想使用hql實現兩個string型別的字段相減報錯,所以改用sql語句來實現,以下是連線sql語句的方法 public list findbysql final string sql catch dataaccessresourcefailureexception ...

Hql常用語句

hql的特點 1 hql是面向實體類物件的。2 與sql大概相似,sql中的語句在hql中基本都可以用。3 hql的關鍵字不區分大小寫,但是因為是物件導向,所以類名和屬性名區分大小寫。4 在hql中select可以省略。1,簡單的查詢,user為實體名而不是資料庫中的表名 物件導向特性 hql fr...