hibernate HQL语句
来源:互联网 发布:java web即时通讯源码 编辑:程序博客网 时间:2024/04/27 10:32
网上摘录的HQL例子,未验证语法是否正确。为以后面试项目开发做准备。通过例子举一返三。
1、//使用Map查询
Query query=session.createQuery("select new Map(u.id as id,u.username as name) from User as u");
2、//使用distinct查询
Query query=session.createQuery("select distinct username from User");
3、//使用where查询
Query query=session.createQuery("from User where id not between 200 and 2000");
4、//in查询
query=session.createQuery("from User where username in ('Hongten','Hanyuan','dfgd')");
5、 //like查询
query=session.createQuery("from User where username not like 'Hon%'");
6、 //null查询
query=session.createQuery("from User where password is null");
List<User> listnull=query.list();
for(User user:listnull){
System.out.println(user.getId()+user.getUsername());
}
7、//order by
query=session.createQuery("from User order by username,id desc");
8、//使用"?"号 作为参数占位符,一条HQL语句中可以使用多个?
query=session.createQuery("select username from User where username=?");
query.setString(0,"Hongten");
--------------------------------------------------------------------------------------
在这里通过定义了三个类,Special、Classroom、Student来做测试,Special与Classroom是一对多,Classroom与Student是一对多的关系,
Special类:
public class Special{ private int id; private String name; private String type; private Set<Classroom> rooms; ..........}Classroom类:
public class Classroom{ private int id; private String name; private Special special; private Set<Student> students; ............}Student类:
public class Student{ private int id; private String name; private String sex; private Classroom room; ..........}9、
List<Special> specials = (List<Special>)session.createQuery("select spe from Special spe").list();这是hql最基本的查询语句了,作用就是查出所有的Special对象放到一个List当中
10、
/** * 查询中使用?,通过setParameter的方式可以防止sql注入 * jdbc的setParameter的下标从1开始,hql的下标从0开始 */ List<Student> students = (List<Student>)session.createQuery("select stu from Student stu where name like ?") .setParameter(0, "%刘%") .list();
11、基于 :xx 的别名的方式设置参数
/** * 在hql中可以使用别名的方式来查询,格式是 :xxx 通过setParameter来设置别名 */ List<Student> students = (List<Student>)session.createQuery("select stu from Student stu where name like :name and sex like :sex") .setParameter("name", "%王%").setParameter("sex", "%男%") .list();
12、
/** * 基于投影的查询,如果返回多个值,这些值都是保存在一个object[]数组当中 */ List<Object[]> stus = (List<Object[]>)session.createQuery("select stu.name, stu.sex from Student stu where name like :name and sex like :sex") .setParameter("name", "%张%").setParameter("sex", "%男%") .list();
13、
/** * 如果对象中有导航对象,可以直接通过对象导航查询 */ List<Student> stus = (List<Student>)session.createQuery("select stu from Student stu where stu.room.name like :room and sex like :sex") .setParameter("room", "%计算机应用%").setParameter("sex", "%女%") .list();14、
/** * 可以使用in设置基于列表的查询,使用in查询时需要使用别名来进行参数设置, * 通过setParameterList方法即可设置,在使用别名和?的hql语句查询时,?形式的查询必须放在别名前面 */// List<Student> stus = (List<Student>)session.createQuery("select stu from Student stu where sex like ? and stu.room.id in (:room)")// .setParameter(0, "%女%").setParameterList("room", new Integer[]{1, 2})// .list(); List<Student> stus = (List<Student>)session.createQuery("select stu from Student stu where stu.room.id in (:room) and stu.sex like :sex") .setParameterList("room", new Integer[]{1, 2}).setParameter("sex", "%女%") .list();
15、分页查询
/** * 通过setFirstResult(0).setMaxResults(10)可以设置分页查询,相当于offset和pagesize */ List<Student> stus = (List<Student>)session.createQuery("select stu from Student stu where stu.room.name like :room and sex like :sex") .setParameter("room", "%计算机应用%").setParameter("sex", "%女%").setFirstResult(0).setMaxResults(10) .list();
16、内连接查询
/** * 使用对象的导航查询可以完成连接查询,但是使用的是Cross Join(笛卡儿积),效率不高,所以建议使用join来查询 */ List<Student> stus = (List<Student>)session.createQuery("select stu from Student stu join stu.room room where room.id=2") .list();
17、左外连和右外连查询
/** * 左外连和右外连其实是相对的,left join 就是以左边的表为基准, right join 就是以右边的表为基准 */ List<Object[]> stus = (List<Object[]>)session.createQuery("select room.name, count(stu.room.id) from Student stu right join stu.room room group by room.id") .list();18、group having字句
/** * 在hql中不能通过给查询出来的字段设置别名,别名只能设置在from 后面 */ List<Object[]> stus = (List<Object[]>)session.createQuery("select special.name, count(stu.room.special.id) from Student stu right join stu.room.special special group by special.id having count(stu.room.special.id)>150") .list(); // 查询出人数大于150个人的专业 // 查询出每个专业中男生与女生的个数 List<Object[]> stus = (List<Object[]>)session.createQuery("select special.name, stu.sex, count(stu.room.special.id) from Student stu right join stu.room.special special group by special.id,stu.sex") .list();
- hibernate查询语句--HQL
- hibernate查询语句--HQL
- hibernate查询语句--HQL
- Hibernate查询语句+HQL
- hibernate查询语句---HQL
- hibernate查询语句--HQL
- hibernate查询语句--HQL
- hibernate查询HQL语句
- hibernate--HQL查询语句
- Hibernate 中HQL语句
- Hibernate 中HQL语句
- hibernate -- HQL语句总结
- hibernate -- HQL语句总结
- Hibernate中HQL语句
- Hibernate HQL语句总结
- hibernate -- HQL语句总结
- hibernate HQL语句
- hibernate -- HQL语句总结
- 电商文案写作的要诀
- BZOJ 1811 IOI 2005 mea 乱搞
- Unique Binary Search Trees II
- saiku server saiku-server 下载地址
- Multiplying by Rotation——进制循环节
- hibernate HQL语句
- PostgreSQL对接SequoiaDB
- MySQL查询和修改auto_increment
- C语言中int到float的强制类型转换(转)
- Android性能优化典范
- WebAPi
- SQL Server中 ldf 文件过大的解决方法
- unity3d读取plist或xml文件
- 使用 HttpClient 和 HtmlParser 实现简易爬虫