常用HQL集锦

时间:2023-03-09 09:17:26
常用HQL集锦

1.根据ID查询某"一个"实体类

方法1:

 String hql = "From ClassEntity as c where c.id=?";
ClassEntity entity = (ClassEntity) getSession().createQuery(hql).setInteger(0, id).uniqueResult();

方法2:

ClassEntity entity=(ClassEntity) getSession().get(ClassEntity.class,id);

2.获取"多个"实体类集合

String hql = "From StudentEntity";
List<StudentEntity> entityList=getSession().createQuery(hql).list();

3.更新或者删除某个实体类

例子1:

String hql = "Delete StudentEntity as s where s.id=?";
getSession().createQuery(hql).setInteger(0, id).executeUpdate();

例子2:

String hql = "Update StudentEntity as s set s.stuClass=?,s.stuName=?,s.birth=? where s.id=?";
getSession().createQuery(hql).setEntity(0, studentEntity.getStuClass()).setString(1, studentEntity.getStuName()).
setDate(2, studentEntity.getBirth()).setInteger(3, studentEntity.getId()).executeUpdate();

关于例子2的说明:

setEntity():此方法可以直接赋值实体类。

4.获得一个聚合函数的值

 String hql = "SELECT COUNT(*) FROM  StudentEntity";
long count = (long) getSession().createQuery(hql).iterate().next();

 或者(推荐下面写法)

  String hql = "SELECT COUNT(*) FROM  StudentEntity";
long count = (long) session.createQuery(hql).uniqueResult();

5.新增一个数据

 getSession().saveOrUpdate(student);

6.模糊查询

String hql = "From StudentEntity as s where s.id= ? or s.stuName like ?";
List<StudentEntity> lists = getSession().createQuery(hql).setString(0, "%" + param + "%").setString(1, "%" + param + "%").list();

7.分页查询

String hql = "From StudentEntity as s order by s.id desc ";
List<StudentEntity> lists= getSession().createQuery(hql).setFirstResult((page - 1) * size).setMaxResults(size).list();

8.多表联合查询(普通SQL不同)

8.1 内连接

    String hql = "From StuInfo as stu Inner Join stu.stuClass as cla where cla.classId=?";
List<Object[]> stuInfos = session.createQuery(hql).setInteger(0, 1).list();
//通过debug可以看出,此时返回值为一个object数组
for (Object[] stuInfo : stuInfos) {
System.out.println("stu:"+stuInfo[0]+" stuClass:"+stuInfo[1]);
}

8.2 左外连接

    String hql = "From StuInfo as stu Left Join stu.stuClass as cla where cla.classId=?";
List<Object[]> stuInfos = session.createQuery(hql).setInteger(0, 1).list();
//通过debug可以看出,此时返回值为一个object数组
for (Object[] stuInfo : stuInfos) {
System.out.println("stu:"+stuInfo[0]+" stuClass:"+stuInfo[1]);
}

8.3 右外连接

    String hql = "From StuInfo as stu Right Join stu.stuClass as cla where cla.classId=?";
List<Object[]> stuInfos = session.createQuery(hql).setInteger(0, 1).list();
//通过debug可以看出,此时返回值为一个object数组
for (Object[] stuInfo : stuInfos) {
System.out.println("stu:"+stuInfo[0]+" stuClass:"+stuInfo[1]);
}

待明确:

1.如果是3张表怎么处理?

2.返回的对象debug截图。

9.OR查询

        Query q = session.createQuery("from Dept d where deptId=:myId or deptName=:name");
q.setParameter("myId", 12);
q.setParameter("name", "财务部");

10.范围查询

        Query q = session.createQuery("from Dept d where deptId between ? and ?");
q.setParameter(0, 1);
q.setParameter(1, 20);

说明:

1.利用between and 优于使用大于小于操作模式。

11.

总结:

.若返回的是一个list集合,那么使用list()。
.若返回的是一个集合,那么使用uniqueResult()。
.若只需要执行"改和删除",那么使用executeUpdate()。
.模糊查询需要在赋值的时候才加入%。
.分页查询需要使用setFirstResult()和setMaxResults()。