常用的sql语句和hql语句

注:表名为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"(以学生年龄进行分组)


猜你喜欢

转载自blog.csdn.net/sasik/article/details/79950141