hibernate查询方式
hibernate查询方式:1.本地SQL查询 2.HQL查询 3.QBC查询
HQL查询:是面向对象的查询语言,是使用最广的一种查询方法
QBC查询:Query by Criteria是一套接口来实现的查询方式

StudentTest.java:
package service;
import java.util.Iterator;
import java.util.List;
import model.Student;
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 util.HibernateUtil;
public class StudentTest {
SessionFactory sessionfactory=HibernateUtil.getSessionFactory();//获取Session工厂
private Session session;
@Before
public void setUp() throws Exception {
session=sessionfactory.openSession();//生成一个新的session
session.beginTransaction();//开启事务
}
@After
public void tearDown() throws Exception {
session.getTransaction().commit();//提交事务
session.close();//关闭session
}
@Test
public void testSQLQuery(){
String sql="select * from t_student";
Query query=session.createSQLQuery(sql).addEntity(Student.class);
List studentList=query.list();
Iterator it=studentList.iterator();
while(it.hasNext()){
Student s=(Student) it.next();
System.out.println(s);
}
}
@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);
query.setString("stuName","11%");
query.setInteger("stuAge",1);
List studentList=query.list();
Iterator it=studentList.iterator();
while(it.hasNext()){
Student s=(Student) it.next();
System.out.println(s);
}
}
@Test
public void testHQLQuery(){
String hql="from Student";
Query query=session.createQuery(hql);
List<Student> studentList=(List<Student>)query.list();
Iterator it=studentList.iterator();
while(it.hasNext()){
Student s=(Student) it.next();
System.out.println(s);
}
}
@Test
public void testHQLQuery2(){
String hql="from Student as s where s.name like :stuName and s.age=:stuAge";
Query query=session.createQuery(hql);
query.setString("stuName","11%");
query.setInteger("stuAge",1);
List<Student> studentList=(List<Student>)query.list();
Iterator it=studentList.iterator();
while(it.hasNext()){
Student s=(Student) it.next();
System.out.println(s);
}
}
@Test
public void testHQLQuery3(){
String hql="from Student order by age desc";
Query query=session.createQuery(hql);
List<Student> studentList=(List<Student>)query.list();
Iterator it=studentList.iterator();
while(it.hasNext()){
Student s=(Student) it.next();
System.out.println(s);
}
}
@Test
public void testHQLQuery4(){
String hql="from Student";
Query query=session.createQuery(hql);
query.setFirstResult(1);
query.setMaxResults(2);
List<Student> studentList=(List<Student>)query.list();
Iterator it=studentList.iterator();
while(it.hasNext()){
Student s=(Student) it.next();
System.out.println(s);
}
}
@Test
public void testHQLQuery5(){
String hql="from Student";
Query query=session.createQuery(hql);
query.setFirstResult(1);
query.setMaxResults(1);
Student student=(Student) query.uniqueResult();
System.out.println(student);
}
@Test
public void testHQLQuery6(){
String hql="from Student as s where s.name like :stuName and s.age=:stuAge";
Query query=session.createQuery(hql);
List<Student> studentList=(List<Student>)query.setString("stuName","11%").setInteger("stuAge",1).list();
Iterator it=studentList.iterator();
while(it.hasNext()){
Student s=(Student) it.next();
System.out.println(s);
}
}
@Test
public void testQBCQuery1(){
Criteria criteria=session.createCriteria(Student.class);
List<Student> studentList=criteria.list();
Iterator it=studentList.iterator();
while(it.hasNext()){
Student s=(Student) it.next();
System.out.println(s);
}
}
@Test
public void testQBCQuery2(){
Criteria criteria=session.createCriteria(Student.class);
Criterion c1=Restrictions.like("name","22%");
Criterion c2=Restrictions.eq("age",2);
criteria.add(c1);
criteria.add(c2);
List<Student> studentList=criteria.list();
Iterator it=studentList.iterator();
while(it.hasNext()){
Student s=(Student) it.next();
System.out.println(s);
}
}
@Test
public void testQBCQuery3(){
Criteria criteria=session.createCriteria(Student.class);
criteria.addOrder(Order.desc("age"));
List<Student> studentList=criteria.list();
Iterator it=studentList.iterator();
while(it.hasNext()){
Student s=(Student) it.next();
System.out.println(s);
}
}
@Test
public void testQBCQuery4(){
Criteria criteria=session.createCriteria(Student.class);
criteria.setFirstResult(2);
criteria.setMaxResults(2);
List<Student> studentList=criteria.list();
Iterator it=studentList.iterator();
while(it.hasNext()){
Student s=(Student) it.next();
System.out.println(s);
}
}
@Test
public void testQBCQuery5(){
Criteria criteria=session.createCriteria(Student.class);
criteria.setFirstResult(2);
criteria.setMaxResults(1);
Student student=(Student) criteria.uniqueResult();
System.out.println(student);
}
@Test
public void testQBCQuery6(){
Criteria criteria=session.createCriteria(Student.class);
List<Student> studentList=criteria.setFirstResult(0).setMaxResults(2).list();
Iterator it=studentList.iterator();
while(it.hasNext()){
Student s=(Student) it.next();
System.out.println(s);
}
}
}
hibernate查询方式的更多相关文章
- (十)Hibernate 查询方式
所有项目导入对应的hibernate的jar包.mysql的jar包和添加每次都需要用到的HibernateUtil.java 第一节:Hibernate 查询方式简介 1,导航对象图查询方式: 2 ...
- Hibernate查询方式(补)
-----------------siwuxie095 Hibernate 查询方式 1.对象导航查询 根据已经加载的对 ...
- Hibernate学习10——Hibernate 查询方式
本章主要是以查询Student的例子: Student.java: package com.cy.model; public class Student { private int id; priva ...
- Hibernate 查询方式、JPA查询方式
hibernate 查询方式: OID 查询 对象导航查询 HQL 方式查询 QBC方式查询 原生SQL方式查询 JPA 查询方式: OID 查询 对象导航查询 JPQL 方式查询 CriteriaB ...
- Hibernate 查询方式(HQL/QBC/QBE)汇总
作为老牌的 ORM 框架,Hibernate 在推动数据库持久化层所做出的贡献有目共睹. 它所提供的数据查询方式也越来越丰富,从 SQL 到自创的 HQL,再到面向对象的标准化查询. 虽然查询方式有点 ...
- hibernate 查询方式汇总
主要摘自 http://blog.sina.com.cn/s/blog_7ffb8dd501014a6o.html ,http://blog.csdn.net/xingtianyiyun/artic ...
- Redis查询&JDBC查询&Hibernate查询方式的效率比较...
比较三种查询方式查询效率对比...我是用的JavaWeb的方式通过通过JSP页面查询的填写查询的参数...给予反馈.... 整个demo的下载地址:http://files.cnblogs.com/f ...
- Hibernate查询方式&抓取策略
Hibernate的查询方式 1.OID查询 hibernate根据对象的OID(主键)进行检索 使用get方法 Customer customer=session.get(Customer.clas ...
- Hibernate学习笔记(五)—— Hibernate查询方式
一.对象图导航查询 对象图导航查询方式是根据已经加载的对象,导航到他的关联对象.它利用类与类之间的关系来查询对象.比如要查找一个联系人对应的客户,就可以由联系人对象自动导航找到联系人所属的客户对象.当 ...
随机推荐
- ListBox实现拖拽排序功能
1.拖拽需要实现的事件包括: PreviewMouseLeftButtonDown LBoxSort_OnDrop 具体实现如下: private void LBoxSort_OnPreviewMou ...
- ASP.NET和MSSQL高性能分页
首先是存储过程,只取出我需要的那段数据,如果页数超过数据总数,自动返回最后一页的纪录: set ANSI_NULLS ON set QUOTED_IDENTIFIER ON GO -- ======= ...
- 【循序渐进学Python】8.面向对象的核心——类型(下)
1 构造和初始化对象 __init__方法是Python内建众多魔法方法(什么是魔法方法?)中最常见的一个,通过这个方法我们可以定义一个对象的初始操作.当构造函数被调用的时候的任何参数都会传递给__i ...
- VS如何显示行号
1.随便打开一个项目,可以看到代码框内并没有显示行号 2.选择“工具”-“选项”,打开后界面如下 3.选择文本编辑器,找到下图中的“行号”并勾选 4.行号可以显示了
- 重新想象 Windows 8 Store Apps (37) - 契约: Settings Contract
[源码下载] 重新想象 Windows 8 Store Apps (37) - 契约: Settings Contract 作者:webabcd 介绍重新想象 Windows 8 Store Apps ...
- LeetCode130:Surrounded Regions
题目: Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is capt ...
- fibonacci高精度加法
A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first t ...
- NYOJ:题目860 又见01背包
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=860 方法一:不用滚动数组(方法二为用滚动数组,为方法一的简化) 动态规划分析:最少要拿总 ...
- [moka同学摘录]Yii2.0开发初学者必看
想要了解更多YII,PHP方面内容,请关注本博客. 基础总结 1.修改默认控制器/方法 yii默认是site控制器,可以在web.php中设置$config中的'defaultRoute'='xxxx ...
- Mysql的简单使用(三)
接上文Mysql的简单使用(二) mysql中结构相同的两个表进行合并:(注意需要两个表的结构是一样的) 有如下结构的两个表father和person. 合并的步骤为: 1.把person表和fath ...