Hibernate学习10——Hibernate 查询方式
本章主要是以查询Student的例子:
Student.java:
package com.cy.model; public class Student {
private int id;
private String name;
private int age; public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
} }
Student.hbm.xml:
<hibernate-mapping package="com.cy.model"> <class name="Student" table="t_student">
<id name="id" column="stuId">
<generator class="native"></generator>
</id>
<property name="name" column="stuName"></property>
<property name="age" column="stuAge"></property>
</class> </hibernate-mapping>
数据库中student数据:
第一节:Hibernate 查询方式简介
/**
* 本地sql查询,不带参数
*/
@Test
public void testSQLQuery(){
String sql = "select * from t_student";
Query query = session.createSQLQuery(sql).addEntity(Student.class); //查询student,绑定Student实体
List<Student> studentList = query.list();
for(Student s: studentList){
System.out.println(s);
} /**
* Hibernate: select * from t_student
Student [id=1, name=张三, age=10]
Student [id=2, name=李四, age=15]
Student [id=3, name=王五, age=13]
*/
} /**
* 本地sql查询,带参数
* 查询姓名张开头,且10岁
*/
@Test
public void testSQLQuery2(){
String sql = "select * from t_student where stuName like :stuName and stuAge = :stuAge";
Query query = session.createSQLQuery(sql).addEntity(Student.class); //查询student,绑定Student实体
query.setString("stuName", "张%");
query.setInteger("stuAge", 10);
List<Student> studentList = query.list();
for(Student s: studentList){
System.out.println(s);
} /*
* Hibernate: select * from t_student where stuName like ? and stuAge = ?
Student [id=1, name=张三, age=10]
*/
}
第三节:HQL 查询方式
package com.cy.service; import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.cy.model.Student;
import com.cy.util.HibernateUtil; public class StudentTest { private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
private Session session; @Before
public void setUp() throws Exception {
session=sessionFactory.openSession();
session.beginTransaction();
} @After
public void tearDown() throws Exception {
session.getTransaction().commit();
session.close();
} /**
* 本地sql查询,不带参数
*/
@Test
public void testSQLQuery(){
String sql = "select * from t_student";
Query query = session.createSQLQuery(sql).addEntity(Student.class); //查询student,绑定Student实体
List<Student> studentList = query.list();
for(Student s: studentList){
System.out.println(s);
} /**
* Hibernate: select * from t_student
Student [id=1, name=张三, age=10]
Student [id=2, name=李四, age=15]
Student [id=3, name=王五, age=13]
*/
} /**
* 本地sql查询,带参数
* 查询姓名张开头,且10岁
*/
@Test
public void testSQLQuery2(){
String sql = "select * from t_student where stuName like :stuName and stuAge = :stuAge";
Query query = session.createSQLQuery(sql).addEntity(Student.class); //查询student,绑定Student实体
query.setString("stuName", "张%");
query.setInteger("stuAge", 10);
List<Student> studentList = query.list();
for(Student s: studentList){
System.out.println(s);
} /*
* Hibernate: select * from t_student where stuName like ? and stuAge = ?
Student [id=1, name=张三, age=10]
*/
} /**
* HQL查询
*/
@Test
public void testHQLQuery(){
String hql = "from Student";
Query query = session.createQuery(hql);
List<Student> studentList = query.list();
for(Student s : studentList){
System.out.println(s);
} /*
* Hibernate: select student0_.stuId as stuId1_0_, student0_.stuName as stuName2_0_, student0_.stuAge as stuAge3_0_ from t_student student0_
Student [id=1, name=张三, age=10]
Student [id=2, name=李四, age=15]
Student [id=3, name=王五, age=13]
*/
} /**
* HQL 带条件查询
*/
@Test
public void testHQLQuery2(){
String hql = "from Student where name like :stuName and age = :stuAge ";
Query query = session.createQuery(hql);
query.setString("stuName", "张%");
query.setInteger("stuAge", 10);
List<Student> studentList = query.list();
for(Student s : studentList){
System.out.println(s);
} /*
* Hibernate: select student0_.stuId as stuId1_0_, student0_.stuName as stuName2_0_, student0_.stuAge as stuAge3_0_ from t_student student0_ where (student0_.stuName like ?) and student0_.stuAge=?
Student [id=1, name=张三, age=10]
*/
} /**
* HQL 使用别名
* from Student as s
* 或者
* from Student s
*/
@Test
public void testHQLQuery3(){
String hql="from Student as s where s.name like :stuName and s.age=:stuAge";
Query query=session.createQuery(hql);
query.setString("stuName", "张%");
query.setInteger("stuAge", 10);
List<Student> studentList= query.list();
for(Student s : studentList){
System.out.println(s);
}
} /**
* HQL查询 对结果排序
* 按照年龄降序排列
*/
@Test
public void testHQLQuery4(){
String hql = "from Student order by age desc";
Query query = session.createQuery(hql);
List<Student> studentList = query.list();
for(Student s : studentList){
System.out.println(s);
} /*
* Hibernate: select student0_.stuId as stuId1_0_, student0_.stuName as stuName2_0_, student0_.stuAge as stuAge3_0_ from t_student student0_ order by student0_.stuAge desc
Student [id=2, name=李四, age=15]
Student [id=3, name=王五, age=13]
Student [id=1, name=张三, age=10]
*/
} /**
* HQL查询 分页查询
*/
@Test
public void testHQLQuery5(){
String hql="from Student";
Query query=session.createQuery(hql);
//第1条记录开始,取2条;
query.setFirstResult(1);
query.setMaxResults(2);
List<Student> studentList = query.list();
for(Student s : studentList){
System.out.println(s);
} /*
* Hibernate: select student0_.stuId as stuId1_0_, student0_.stuName as stuName2_0_, student0_.stuAge as stuAge3_0_ from t_student student0_ limit ?, ?
Student [id=2, name=李四, age=15]
Student [id=3, name=王五, age=13]
*/
} /**
* HQL查询 查询单个对象
* 查询出一条记录,完全能确定是一条记录,这里就不是list了,uniqueResult
* 比如查询总记录数,就可以用uniqueResult
*/
@Test
public void testHQLQuery6() {
String hql="from Student";
Query query=session.createQuery(hql);
query.setFirstResult(1);
query.setMaxResults(1);
Student s = (Student) query.uniqueResult();
System.out.println(s); /*
* Hibernate: select student0_.stuId as stuId1_0_, student0_.stuName as stuName2_0_, student0_.stuAge as stuAge3_0_ from t_student student0_ limit ?, ?
Student [id=2, name=李四, age=15]
*/
} /**
* HQL 链式写法
*/
@SuppressWarnings("unchecked")
@Test
public void testHQLQuery7() {
String hql = "from Student s where s.name like :stuName and s.age = :stuAge";
List<Student> studentList = session.createQuery(hql)
.setString("stuName", "李%")
.setInteger("stuAge", 15)
.list();
for(Student s : studentList){
System.out.println(s);
} /*
* Student [id=2, name=李四, age=15]
*/
} }
第四节:QBC 查询方式
package com.cy.service; import java.util.List; import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import com.cy.model.Student;
import com.cy.util.HibernateUtil; public class StudentTest { private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
private Session session; @Before
public void setUp() throws Exception {
session=sessionFactory.openSession();
session.beginTransaction();
} @After
public void tearDown() throws Exception {
session.getTransaction().commit();
session.close();
} /**
* QBC查询
*/
@Test
public void testQBCQuery(){
Criteria criteria = session.createCriteria(Student.class);
List<Student> stuList = criteria.list();
for(Student s : stuList){
System.out.println(s);
} /*
* Hibernate: select this_.stuId as stuId1_0_0_, this_.stuName as stuName2_0_0_, this_.stuAge as stuAge3_0_0_ from t_student this_
Student [id=1, name=张三, age=10]
Student [id=2, name=李四, age=15]
Student [id=3, name=王五, age=13]
*/
} /**
* QBC 带条件查询
*/
@Test
public void testQBCQuery2(){
Criteria criteria = session.createCriteria(Student.class);
Criterion c1 = Restrictions.like("name", "李%");
Criterion c2 = Restrictions.eq("age", 15);
criteria.add(c1);
criteria.add(c2);
List<Student> stuList = criteria.list();
for(Student s : stuList){
System.out.println(s);
}
/*
* Hibernate: select this_.stuId as stuId1_0_0_, this_.stuName as stuName2_0_0_, this_.stuAge as stuAge3_0_0_ from t_student this_ where this_.stuName like ? and this_.stuAge=?
Student [id=2, name=李四, age=15]
*/
} /**
* QBC 对结果排序
*/
@Test
public void testQBCQuery3(){
Criteria criteria = session.createCriteria(Student.class);
criteria.addOrder(Order.desc("age"));
List<Student> stuList = criteria.list();
for(Student s : stuList){
System.out.println(s);
} /*
* Student [id=2, name=李四, age=15]
Student [id=3, name=王五, age=13]
Student [id=1, name=张三, age=10]
*/
} /**
* QBC 分页查询
*/
@Test
public void testQBCQuery4(){
Criteria criteria = session.createCriteria(Student.class);
criteria.setFirstResult(0); //从第0条记录开始
criteria.setMaxResults(2); //查询2条
List<Student> stuList = criteria.list();
for(Student s : stuList){
System.out.println(s);
} /*
* Hibernate: select this_.stuId as stuId1_0_0_, this_.stuName as stuName2_0_0_, this_.stuAge as stuAge3_0_0_ from t_student this_ limit ?
Student [id=1, name=张三, age=10]
Student [id=2, name=李四, age=15]
*/
} /**
* QBC 查询单个对象
* 因为这里只取一条记录,uniqueResult
*/
@Test
public void testQBCQuery5(){
Criteria criteria = session.createCriteria(Student.class);
criteria.setFirstResult(2);
criteria.setMaxResults(1);
Student s = (Student) criteria.uniqueResult();
System.out.println(s); /*
* Hibernate: select this_.stuId as stuId1_0_0_, this_.stuName as stuName2_0_0_, this_.stuAge as stuAge3_0_0_ from t_student this_ limit ?, ?
Student [id=3, name=王五, age=13]
*/
} /**
* QBC 链式写法
*/
@SuppressWarnings("unchecked")
@Test
public void testQBCQuery6(){
Criterion c1 = Restrictions.like("name", "李%");
Criterion c2 = Restrictions.eq("age", 15);
List<Student> stuList = session.createCriteria(Student.class)
.add(c1)
.add(c2)
.list();
for(Student s : stuList){
System.out.println(s);
}
} }
Hibernate学习10——Hibernate 查询方式的更多相关文章
- hibernate学习笔记6--Criteria查询方式、完整小练习(开发步骤)
一.Criteria查询方式没有sql语了,因此更加面向对象一些.Criteria是一种比HQL更面向对象的查询方式:Criteria的创建方式: Criteria c = s.createCrite ...
- hibernate的三种查询方式
hibernate的三种查询方式 目录 hibernate的三种查询方式 1.HQL查询 1.1.SQL概述 1.2.实体查询 1.3.带where的查询 1.3.属性查询 1.4.实体的更新和删除 ...
- Hibernate学习笔记-Hibernate HQL查询
Session是持久层操作的基础,相当于JDBC中的Connection,通过Session会话来保存.更新.查找数据.session是Hibernate运作的中心,对象的生命周期.事务的管理.数据库 ...
- Hibernate学习笔记四 查询
HQL语法 1.基本语法 String hql = " from com.yyb.domain.Customer ";//完整写法 String hql2 = " fro ...
- Hibernate学习笔记--------4.查询
一.Get/Load Get方法是立即检索,而load是延迟检索,他们都是根据主键进行查询.在<class>标签中,若把lazy属性改为false,load方法就会立即检索,class中的 ...
- Hibernate的四种查询方式(主键查询,HQL查询,Criteria查询,本地sql查询)和修改和添加
Hibernate的添加,修改,查询(三种查询方式)的方法: 案例演示: 1:第一步,导包,老生常谈了都是,省略: 2:第二步,创建数据库和数据表,表结构如下所示: 3:第三步创建实体类User.ja ...
- Hibernate第四天——查询方式
Hibernate入门最后一天第四天,我们进行查询方式的更进一步的细化: 先看一下大致的Hibernate的提供的查询的方式: 1.对象导航查询 2.OID查询 3.HQL查询 4.QBC查询 5.本 ...
- Hibernate之HQL检索(查询)方式
HQL(Hibernate Query Language)是面向对象的查询语言,与SQL非常相似.在Hibernate中,HQL是使用最广泛的检索方式. 具有下面经常使用功能: (1)在查询语句中,能 ...
- Hibernate的几种查询方式-HQL,QBC,QBE,离线查询,复合查询,分页查询
HQL查询方式 这一种我最常用,也是最喜欢用的,因为它写起来灵活直观,而且与所熟悉的SQL的语法差不太多.条件查询.分页查询.连接查询.嵌套查询,写起来与SQL语法基本一致,唯一不同的就是把表名换成了 ...
随机推荐
- Rsync安装和配置
一.Rsync简介 1.1什么是Rsync Rsync是一款快速的,开源的,多功能的,可以实现全量和增量的远程和本地的数据同步和数据备份的工具. 全量的概念是:全部备份. 增量的概念是:差异化备份.对 ...
- Netlink 基础知识
Netlink 基础知识 Netlink 相对于系统调用,ioctl 以及 /proc 文件系统而言具有以下优点: 1. 用户仅需要在 include/linux/netlink.h 中增加一个新类型 ...
- 如何在非Spring管理的类中使用Spring加载的bean
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> < ...
- 【LeetCode 111_二叉树_遍历】Minimum Depth of Binary Tree
解法一:递归 int minDepth(TreeNode* root) { if (root == NULL) ; if (root->left == NULL) { ; } else if ( ...
- cookie注入原理及注入检测
通常我们的开发人员在开发过程中会特别注意到防止恶意用户进行恶意的注入操作,因此会对传入的参数进行适当的过滤,但是很多时候,由于个人对安全技术了解的不同,有些开发人员只会对get,post这种方式提交的 ...
- linux下如何启动和关闭weblogic .
在你定义的域中可以找到如下命令: /[youHome]/domains/[yourDomain]/startWebLogic.sh /[youHome]/domains/[yourDomain]/st ...
- CUDA Samples: Calculate Histogram(atomicAdd)
以下CUDA sample是分别用C++和CUDA实现的计算一维直方图,并对其中使用到的CUDA函数进行了解说,code参考了<GPU高性能编程CUDA实战>一书的第九章,各个文件内容如下 ...
- python3精简笔记(三)——高级特性
Python中 1行代码能实现的功能,决不写5行代码.请始终牢记,代码越少,开发效率越高. 切片 取一个list或tuple的部分元素是非常常见的操作.Python提供了切片(Slice)操作符 L ...
- HDU 1374
http://acm.hdu.edu.cn/showproblem.php?pid=1374 已知三点坐标,求三点确定的圆的周长 #include <iostream> #include ...
- 实战maven私有仓库三部曲之一:搭建和使用
在局域网内搭建maven私有仓库,可避免每次都从中央仓库下载公共jar包,另外将A模块作为二方库发布到私有仓库后,B模块可以很方便的引用,今天我们就来实战maven私有仓库的搭建和使用: 原文地址:h ...