常用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.
總結:
1.若返回的是一個list集合,那么使用list()。 2.若返回的是一個集合,那么使用uniqueResult()。 3.若只需要執行"改和刪除",那么使用executeUpdate()。 4.模糊查詢需要在賦值的時候才加入%。 5.分頁查詢需要使用setFirstResult()和setMaxResults()。?
轉載于:https://www.cnblogs.com/LiuChunfu/p/4936910.html
總結
- 上一篇: 【c++】标准模板库STL入门简介与常见
- 下一篇: 清理AD过期对象,并将结果发送给指定管理