常用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()。
常用HQL集锦的更多相关文章
- mysql常用命令集锦
一.DCL语句(数据控制语句) 1.授权远程访问,针对IP和用户.DB的 grant {privilege list} on {dbname}.* to '{user}'@'{ip}' identif ...
- jquery常用代码集锦
1. 如何修改jquery默认编码(例如默认GB2312改成 UTF-8 ) 1 2 3 4 5 $.ajaxSetup({ ajaxSettings : { contentT ...
- Sublime text2 常用插件集锦
No.01 – EmmetEmmet 是一个前端开发的利器,其前身是Zen Coding.它让编写 HTML 代码变得简单.Emmet 的基本用法是:输入简写形式,然后按 Tab 键.关于 Emmet ...
- PHP中的常用正则表达式集锦
PHP中的常用正则表达式集锦: 匹配中文字符的正则表达式: [\u4e00-\u9fa5] 评注:匹配中文还真是个头疼的事,有了这个表达式就好办了 匹配双字节字符(包括汉字在内):[^\x00-\xf ...
- php常用知识集锦
php常用知识集锦 很多位置都有写好的代码,自己做项目的时候可以直接拿来用,而不用自己写,比如现在看到的菜鸟教程. 1.判断是否为空 empty($_POST["name"]) 2 ...
- win10纯净版安装及其常用软件集锦(2020新年湘岳阳万江波整理)
win10纯净版安装及其常用软件集锦 1.安装win10纯净版:链接:https://pan.baidu.com/s/1L9yl-LNxxDQbEN_TGswzcA 提取码:u0pt 2.安装WPS2 ...
- linux 常用命令 集锦
第一章 LINUX简介及安装 1一.LINUX介绍 1二.LINUX安装 2三.LINUX目录 2四.总结来说: 3第二章 常用命令及帐户管理 4一.linux命 ...
- 【linux】常用命令集锦&持续更新...
滴:转载引用请注明哦[握爪]:https://www.cnblogs.com/zyrb/p/9709013.html 对深度学习训练及日常work中的常用linux命令进行整理. [一]screen ...
- Git常用命令集锦
本篇Git命令博客主要是一些Git常用命令,适合于有一定Git或linux基础的小伙伴进行参考 1.新建文件夹 mkdir 文件夹名 2.查看目录机构: pwd 3.将文件添加至Git管理范围:git ...
随机推荐
- Linux网卡配置与绑定
一定要在服务管理中关闭NetworkManager服务并禁用自动启动. 第一步:先查看下本机网卡,使用命令到network-scripts 下 [root@root~]# cd /etc/syscon ...
- url重写步骤
url重写:在global文件中,在application_BeginRequest事件中1:获取URL string url=Request.AppRelativeCurrentExecutionF ...
- js连续赋值、指针
jq的源码中有很多连续赋值,类似这样的: var a = {n:1}; var b = a; // 持有a,以回查 a.x = a = {n:2}; alert(a.x);// --> unde ...
- ubuntu setup adb tool
sudo add-apt-repository ppa:nilarimogard/webupd8sudo apt-get updatesudo apt-get install android-tool ...
- C语言根据函数名调用对应的函数
通过函数指针定义,调用时加上参数 struct Command { const char *name; const char *desc; // return -1 to force monitor ...
- HDU1492/The number of divisors(约数) about Humble Numbers
题目连接 The number of divisors(约数) about Humble Numbers Time Limit: 2000/1000 MS (Java/Others) Memory L ...
- poll和select
都允许进程决定是否可以对一个或者多个打开的文件做非阻塞的读取或写入.这些调用也会阻塞进程,直到给定的文件描述符集合中的任何一个可读取或写入.常常用于那些要使用多个输入或输出流而又不会阻塞与其中任意一个 ...
- [转]View属性 之 paddingStart & paddingEnd
[CAUSE] 在写一个自定义View时, 直接复制了Android-Source的XML布局文件, 默认开发SDK版本是4.2.2(Level-API-17), 后因其他原因将SDK版本改为4.1. ...
- Git学习 -- 标签管理
新建标签 git tag <tagname> 默认为HEAD,也可以指定一个commit id eg. git tag v0.9 git tag v1.0 31aa59c git ...
- 关于VC中的错误处理
include <exception> try {} cache(exception &e) { cout<<e.what()<<endl; } 但 ...