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);
}
}
}