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()。

常用HQL集锦的更多相关文章

  1. mysql常用命令集锦

    一.DCL语句(数据控制语句) 1.授权远程访问,针对IP和用户.DB的 grant {privilege list} on {dbname}.* to '{user}'@'{ip}' identif ...

  2. jquery常用代码集锦

    1. 如何修改jquery默认编码(例如默认GB2312改成 UTF-8 ) 1 2 3 4 5 $.ajaxSetup({     ajaxSettings : {         contentT ...

  3. Sublime text2 常用插件集锦

    No.01 – EmmetEmmet 是一个前端开发的利器,其前身是Zen Coding.它让编写 HTML 代码变得简单.Emmet 的基本用法是:输入简写形式,然后按 Tab 键.关于 Emmet ...

  4. PHP中的常用正则表达式集锦

    PHP中的常用正则表达式集锦: 匹配中文字符的正则表达式: [\u4e00-\u9fa5] 评注:匹配中文还真是个头疼的事,有了这个表达式就好办了 匹配双字节字符(包括汉字在内):[^\x00-\xf ...

  5. php常用知识集锦

    php常用知识集锦 很多位置都有写好的代码,自己做项目的时候可以直接拿来用,而不用自己写,比如现在看到的菜鸟教程. 1.判断是否为空 empty($_POST["name"]) 2 ...

  6. win10纯净版安装及其常用软件集锦(2020新年湘岳阳万江波整理)

    win10纯净版安装及其常用软件集锦 1.安装win10纯净版:链接:https://pan.baidu.com/s/1L9yl-LNxxDQbEN_TGswzcA 提取码:u0pt 2.安装WPS2 ...

  7. linux 常用命令 集锦

    第一章  LINUX简介及安装    1一.LINUX介绍    1二.LINUX安装    2三.LINUX目录    2四.总结来说:    3第二章 常用命令及帐户管理    4一.linux命 ...

  8. 【linux】常用命令集锦&持续更新...

    滴:转载引用请注明哦[握爪]:https://www.cnblogs.com/zyrb/p/9709013.html  对深度学习训练及日常work中的常用linux命令进行整理. [一]screen ...

  9. Git常用命令集锦

    本篇Git命令博客主要是一些Git常用命令,适合于有一定Git或linux基础的小伙伴进行参考 1.新建文件夹 mkdir 文件夹名 2.查看目录机构: pwd 3.将文件添加至Git管理范围:git ...

随机推荐

  1. C陷阱与缺陷 第一章

    1. 使用 e1=e2的赋值方式 作为 条件语句内部的判断,请使用显示的判断 不使用: if( x =y ) foo(); 而使用: ) foo(); 2. 注意编码规范,一定要在赋值号 “=”两边, ...

  2. masonry框架的使用之-多个视图的均匀等间距分布

    __weak typeof(self) weakSelf = self; //对self进行weak化,否则造成循环引用无法释放controller UIView * tempView = [[UIV ...

  3. linux 终端相关

    echo cd ~/桌面 >> .bashrc 将终端默认路径设为桌面 -/.bashrc./etc/bash.bashrc./etc/profile这几个文件.这些文件的的作用时机:/e ...

  4. iOS NSString类中获取子字符串

    NSString类中提供了这样三个方法用于获取子字符串: – substringFromIndex://取字符串长度从0开始,当index=str.length时字符串为空"" – ...

  5. 项目总结SpringMVC相关

    流程文字概述1.用户发送请求至前端控制器DispatcherServlet2.DispatcherServlet收到请求调用HandlerMapping处理器映射器.3.处理器映射器找到具体的处理器, ...

  6. 2015年5月9日 student information management system

    /*大作业SIMS*///头文件 #ifndef __FUNC_C__ #define __FUNC_C__ #include <stdio.h> #include <stdlib. ...

  7. CodeForces 687C The Values You Can Make(动态规划)

    这个也可以说是一个01背包了,里面也有一些集合的思想在里面,首先dp方程,dp[i][j]代表着当前数值为i,j能否被构成,如果dp[i][j] = 1,那么dp[i+m][j] 和 dp[i+m][ ...

  8. Linux学习 -- 日志管理

    日志服务 rsyslogd  CentOS6 取代了原来的syslog rsyslogd 默认启动.自启动 常用命令:lastb.lastlog.last.w.who.users. 系统默认日志 和 ...

  9. [IDL入门] 两个PPT,IDL上手

    首先看看IDL能干什么,<Solving Real Problems with Computer Graphics>ppt是英文的,很精彩. 下载地址:http://pan.baidu.c ...

  10. [LeetCode] Magical String 神奇字符串

    A magical string S consists of only '1' and '2' and obeys the following rules: The string S is magic ...