本章主要是以查询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 查询方式简介
导航对象图是说from Student s where s.name = xxx and s.age = xxx这样可以对象.属性这样子;
OID查询方式:比如Session.get(Student.class, 1);根据ID查询这样子;
第二节:本地SQL 查询方式
/** * 本地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); } } }
时间: 2024-10-15 03:31:02