日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

常用HQL集锦

發布時間:2025/4/5 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 常用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

總結

以上是生活随笔為你收集整理的常用HQL集锦的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。