Hibernate學習10——Hibernate 查詢方式
阿新 • • 發佈:2017-07-30
9.png property ice pub gets reat sed desc ring
本章主要是以查詢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() {View Codereturn 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"View Code></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] */ }View Code
第三節: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] */ } }View Code
第四節: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); } } }View Code
Hibernate學習10——Hibernate 查詢方式