hibernate之13.hql(一)
来源:互联网 发布:linux查看目录结构命令 编辑:程序博客网 时间:2024/06/05 00:12
查询所有数据:
@Testpublic void queryAllTest(){Session session=null;try{session=HibernateUtil.openSession();String hql="from Student";Query query=session.createQuery(hql);List<Student> list=query.list();for(Student student: list){System.out.println(student);}}finally{session.close();}}
基于参数查询:
@Testpublic void queryWithParamTest(){Session session=null;try{session=HibernateUtil.openSession();String hql="from Student where studentId=?";Query query=session.createQuery(hql);query.setInteger(0, 83);List<Student> list=query.list();for(Student student: list){System.out.println(student);}}finally{session.close();}}
基于命令参数查询
@Testpublic void queryWithEqualsNamedParamTest(){Session session=null;try{session=HibernateUtil.openSession();String hql="from Student where studentId=:studentId";Query query=session.createQuery(hql);query.setInteger("studentId", 83);List<Student> list=query.list();for(Student student: list){System.out.println(student);}}finally{session.close();}}
Like:
@Testpublic void queryWithLikeTest(){Session session=null;try{session=HibernateUtil.openSession();String hql="from Student where studentName like ?";Query query=session.createQuery(hql);query.setString(0, "%王%");List<Student> list=query.list();for(Student student: list){System.out.println(student);}}finally{session.close();}}
in:
@Testpublic void queryWithInTest(){Session session=null;try{session=HibernateUtil.openSession();String hql="from Student where studentId in (:stuIds)";Query query=session.createQuery(hql);query.setParameterList("stuIds", new Integer[]{63,83});List<Student> list=query.list();for(Student student: list){System.out.println(student);}}finally{session.close();}}
查询部份字段:
@Testpublic void queryColumnsTest(){Session session=null;try{session=HibernateUtil.openSession();String hql="select studentId,studentName from Student where studentName like ?";Query query=session.createQuery(hql);query.setString(0, "%王%");List<Object[]> list=query.list();for(Object[] objs: list){System.out.println(objs[0]+" "+objs[1]);}}finally{session.close();}}
查询单个字段:
@Testpublic void querySingleColumnTest(){Session session=null;try{session=HibernateUtil.openSession();String hql="select studentName from Student where studentName like ?";Query query=session.createQuery(hql);query.setString(0, "%王%");List<String> list=query.list();for(String name: list){System.out.println(name);}}finally{session.close();}}
查询统计:
@Testpublic void queryCountTest(){Session session=null;try{session=HibernateUtil.openSession();String hql="select count(*) from Student where studentName like ?";Query query=session.createQuery(hql);query.setString(0, "%王%");List<Long> list=query.list();for(Long cnt: list){System.out.println(cnt);}}finally{session.close();}}
单个结果集
@Testpublic void queryUniqueResultTest(){Session session=null;try{session=HibernateUtil.openSession();String hql="select stu from Student stu where stu.studentId=?";Query query=session.createQuery(hql);query.setInteger(0, 83);Student stu=(Student)query.uniqueResult();System.out.println(stu);}finally{session.close();}}
分页查询:
@Testpublic void queryPagerTest(){Session session=null;try{session=HibernateUtil.openSession();String hql="select stu from Student stu where studentName like ?";Query query=session.createQuery(hql);//从第几条记录开始query.setFirstResult(1);//每页大小query.setMaxResults(20);query.setString(0, "%王%");List<Student> list=query.list();for(Student stu: list){System.out.println(stu);}}finally{session.close();}}
0 0
- hibernate之13.hql(一)
- hibernate之13.hql(二)
- Hibernate之HQL总结
- Hibernate之HQL妙用
- Hibernate之HQL查询
- hibernate之hql总结
- Hibernate 之 HQL
- Hibernate之HQL总结
- hibernate之HQL学习
- Hibernate之HQL总结
- Hibernate之HQL总结
- Hibernate之HQL总结
- Hibernate之HQL总结
- Hibernate 之 HQL
- 【JAVA】hibernate之HQL
- Hibernate之HQL总结
- Hibernate之HQL查询
- hibernate之hql语
- spring配置文件详解
- UML五种关系
- IOS代理 protocol最简单的实例,没有之一。。
- 初学C51编程的误区
- Web安全 Buffer Overflow Attack实验
- hibernate之13.hql(一)
- Appdelegate中查看所有的View-递归
- 笔试以及面试题1
- 点滴记录——Ubuntu 自动挂载windows分区
- ASM tools used by Support : KFOD, KFED, AMDU (Doc ID 1485597.1)
- 软件工程第二次作业————结对编程项目
- 比酒量
- mysql备份和恢复笔记
- 装饰设计模式