(十)Hibernate 查询方式

 所有项目导入对应的hibernate的jar包、mysql的jar包和添加每次都需要用到的HibernateUtil.java

第一节:Hibernate 查询方式简介

 

1,导航对象图查询方式;

2,OID 查询方式;

3,本地SQL 查询方式;

4,HQL 查询方式;

5,QBC 查询方式;(Query By Criteria)

 

 第二节:本地SQL 查询方式

 Student.java

 1 package com.wishwzp.model;
 2 
 3 public class Student {
 4 
 5     private int id;
 6     private String name;
 7     private int age;
 8     
 9     public int getId() {
10         return id;
11     }
12     public void setId(int id) {
13         this.id = id;
14     }
15     public String getName() {
16         return name;
17     }
18     public void setName(String name) {
19         this.name = name;
20     }
21     public int getAge() {
22         return age;
23     }
24     public void setAge(int age) {
25         this.age = age;
26     }
27     @Override
28     public String toString() {
29         return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
30     }
31     
32 }

 

Student.hbm.xml

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 5 
 6 <hibernate-mapping package="com.wishwzp.model">
 7 
 8     <class name="Student" table="t_student">
 9         <id name="id" column="stuId">
10             <generator class="native"></generator>
11         </id>
12         
13         <property name="name" column="stuName"></property>
14         <property name="age" column="stuAge"></property>
15     </class>
16 
17 </hibernate-mapping>

 

hibernate.cfg.xml

 1 <?xml version='1.0' encoding='utf-8'?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 5 
 6 <hibernate-configuration>
 7 
 8     <session-factory>
 9 
10         <!--数据库连接设置 -->
11         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
12         <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
13         <property name="connection.username">root</property>
14         <property name="connection.password">123456</property>
15 
16        
17         <!-- 方言 -->
18         <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
19     
20         <!-- 控制台显示SQL -->
21         <property name="show_sql">true</property>
22 
23         <!-- 自动更新表结构 -->
24         <property name="hbm2ddl.auto">update</property>
25         
26           <mapping resource="com/wishwzp/model/Student.hbm.xml"/>
27           
28           
29 
30     </session-factory>
31 
32 </hibernate-configuration>

 

StudentTest.java

 1 package com.wishwzp.service;
 2 
 3 
 4 import java.util.Iterator;
 5 import java.util.List;
 6 
 7 import org.hibernate.Query;
 8 import org.hibernate.Session;
 9 import org.hibernate.SessionFactory;
10 import org.junit.After;
11 import org.junit.Before;
12 import org.junit.Test;
13 
14 import com.wishwzp.model.Student;
15 import com.wishwzp.util.HibernateUtil;
16 
17 public class StudentTest {
18 
19     private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
20     private Session session;
21     
22     @Before
23     public void setUp() throws Exception {
24         session=sessionFactory.openSession(); // 生成一个session
25         session.beginTransaction(); // 开启事务
26     }
27 
28     @After
29     public void tearDown() throws Exception {
30          session.getTransaction().commit(); // 提交事务
31          session.close(); // 关闭session
32     }
33 
34     @Test
35     public void testSQLQuery() {
36             
37     }
38     
39     
40 }

 

运行结果生成了数据库信息:

 

 

 我们插入一些数据:

 

 再次写代码查询里面的数据:

StudentTest.java

 1 package com.wishwzp.service;
 2 
 3 
 4 import java.util.Iterator;
 5 import java.util.List;
 6 
 7 import org.hibernate.Query;
 8 import org.hibernate.Session;
 9 import org.hibernate.SessionFactory;
10 import org.junit.After;
11 import org.junit.Before;
12 import org.junit.Test;
13 
14 import com.wishwzp.model.Student;
15 import com.wishwzp.util.HibernateUtil;
16 
17 public class StudentTest {
18 
19     private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
20     private Session session;
21     
22     @Before
23     public void setUp() throws Exception {
24         session=sessionFactory.openSession(); // 生成一个session
25         session.beginTransaction(); // 开启事务
26     }
27 
28     @After
29     public void tearDown() throws Exception {
30          session.getTransaction().commit(); // 提交事务
31          session.close(); // 关闭session
32     }
33 
34     @Test
35     public void testSQLQuery() {
36         String sql="select * from t_student";
37         Query query=session.createSQLQuery(sql).addEntity(Student.class);
38         List studentList=query.list();
39         Iterator it=studentList.iterator();
40         while(it.hasNext()){
41             Student s=(Student)it.next();
42             System.out.println(s);
43         }        
44     }
45     
46 }

 

运行结果显示:

 

我们写一个带参数的查询语句查询:

StudentTest.java

 1 package com.wishwzp.service;
 2 
 3 
 4 import java.util.Iterator;
 5 import java.util.List;
 6 
 7 import org.hibernate.Query;
 8 import org.hibernate.Session;
 9 import org.hibernate.SessionFactory;
10 import org.junit.After;
11 import org.junit.Before;
12 import org.junit.Test;
13 
14 import com.wishwzp.model.Student;
15 import com.wishwzp.util.HibernateUtil;
16 
17 public class StudentTest {
18 
19     private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
20     private Session session;
21     
22     @Before
23     public void setUp() throws Exception {
24         session=sessionFactory.openSession(); // 生成一个session
25         session.beginTransaction(); // 开启事务
26     }
27 
28     @After
29     public void tearDown() throws Exception {
30          session.getTransaction().commit(); // 提交事务
31          session.close(); // 关闭session
32     }
33     
34     @Test
35     public void testSQLQuery2() {
36         String sql="select * from t_student where stuName like :stuName and stuAge=:stuAge";
37         Query query=session.createSQLQuery(sql).addEntity(Student.class);
38         query.setString("stuName", "张%");
39         query.setInteger("stuAge", 10);
40         List studentList=query.list();
41         Iterator it=studentList.iterator();
42         while(it.hasNext()){
43             Student s=(Student)it.next();
44             System.out.println(s);
45         }        
46     }
47     
48 }

 

运行结果显示:

 

第三节:HQL 查询方式

 

HQL(Hibernate Query Language)是面向对象的查询语言;是使用最广的一种查询方式;

1,普通查询;

2,带条件查询;

3,使用别名;

4,对结果排序;

5,分页查询;

6,查询单个对象;

7,链式写法;

 

1,普通查询;

StudentTest.java

 1 package com.wishwzp.service;
 2 
 3 
 4 import java.util.Iterator;
 5 import java.util.List;
 6 
 7 import org.hibernate.Query;
 8 import org.hibernate.Session;
 9 import org.hibernate.SessionFactory;
10 import org.junit.After;
11 import org.junit.Before;
12 import org.junit.Test;
13 
14 import com.wishwzp.model.Student;
15 import com.wishwzp.util.HibernateUtil;
16 
17 public class StudentTest {
18 
19     private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
20     private Session session;
21     
22     @Before
23     public void setUp() throws Exception {
24         session=sessionFactory.openSession(); // 生成一个session
25         session.beginTransaction(); // 开启事务
26     }
27 
28     @After
29     public void tearDown() throws Exception {
30          session.getTransaction().commit(); // 提交事务
31          session.close(); // 关闭session
32     }
33     
34     @Test
35     public void testHQLQuery() {
36         String hql="from Student";
37         Query query=session.createQuery(hql);
38         List<Student> studentList=(List<Student>)query.list();
39         Iterator it=studentList.iterator();
40         while(it.hasNext()){
41             Student s=(Student)it.next();
42             System.out.println(s);
43         }        
44     }
45     
46 }

 

运行结果显示:

 

2,带条件查询;

StudentTest.java

 1 package com.wishwzp.service;
 2 
 3 
 4 import java.util.Iterator;
 5 import java.util.List;
 6 
 7 import org.hibernate.Query;
 8 import org.hibernate.Session;
 9 import org.hibernate.SessionFactory;
10 import org.junit.After;
11 import org.junit.Before;
12 import org.junit.Test;
13 
14 import com.wishwzp.model.Student;
15 import com.wishwzp.util.HibernateUtil;
16 
17 public class StudentTest {
18 
19     private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
20     private Session session;
21     
22     @Before
23     public void setUp() throws Exception {
24         session=sessionFactory.openSession(); // 生成一个session
25         session.beginTransaction(); // 开启事务
26     }
27 
28     @After
29     public void tearDown() throws Exception {
30          session.getTransaction().commit(); // 提交事务
31          session.close(); // 关闭session
32     }
33     
34     @Test
35     public void testHQLQuery2() {
36         String hql="from Student where name like :stuName and age=:stuAge";
37         Query query=session.createQuery(hql);
38         query.setString("stuName", "张%");
39         query.setInteger("stuAge", 10);
40         List<Student> studentList=(List<Student>)query.list();
41         Iterator it=studentList.iterator();
42         while(it.hasNext()){
43             Student s=(Student)it.next();
44             System.out.println(s);
45         }        
46     }
47     
48 }

 

 

运行结果显示:

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]

 

3,使用别名;

StudentTest.java

 1 package com.wishwzp.service;
 2 
 3 
 4 import java.util.Iterator;
 5 import java.util.List;
 6 
 7 import org.hibernate.Query;
 8 import org.hibernate.Session;
 9 import org.hibernate.SessionFactory;
10 import org.junit.After;
11 import org.junit.Before;
12 import org.junit.Test;
13 
14 import com.wishwzp.model.Student;
15 import com.wishwzp.util.HibernateUtil;
16 
17 public class StudentTest {
18 
19     private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
20     private Session session;
21     
22     @Before
23     public void setUp() throws Exception {
24         session=sessionFactory.openSession(); // 生成一个session
25         session.beginTransaction(); // 开启事务
26     }
27 
28     @After
29     public void tearDown() throws Exception {
30          session.getTransaction().commit(); // 提交事务
31          session.close(); // 关闭session
32     }
33     
34     @Test
35     public void testHQLQuery3() {
36         String hql="from Student as s where s.name like :stuName and s.age=:stuAge";
37         Query query=session.createQuery(hql);
38         query.setString("stuName", "张%");
39         query.setInteger("stuAge", 10);
40         List<Student> studentList=(List<Student>)query.list();
41         Iterator it=studentList.iterator();
42         while(it.hasNext()){
43             Student s=(Student)it.next();
44             System.out.println(s);
45         }        
46     }
47     
48     
49 }

 

 

运行结果显示:

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]

 

4,对结果排序;

StudentTest.java

 1 package com.wishwzp.service;
 2 
 3 
 4 import java.util.Iterator;
 5 import java.util.List;
 6 
 7 import org.hibernate.Query;
 8 import org.hibernate.Session;
 9 import org.hibernate.SessionFactory;
10 import org.junit.After;
11 import org.junit.Before;
12 import org.junit.Test;
13 
14 import com.wishwzp.model.Student;
15 import com.wishwzp.util.HibernateUtil;
16 
17 public class StudentTest {
18 
19     private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
20     private Session session;
21     
22     @Before
23     public void setUp() throws Exception {
24         session=sessionFactory.openSession(); // 生成一个session
25         session.beginTransaction(); // 开启事务
26     }
27 
28     @After
29     public void tearDown() throws Exception {
30          session.getTransaction().commit(); // 提交事务
31          session.close(); // 关闭session
32     }
33     
34     @Test
35     public void testHQLQuery4() {
36         String hql="from Student order by age desc";
37         Query query=session.createQuery(hql);
38         List<Student> studentList=(List<Student>)query.list();
39         Iterator it=studentList.iterator();
40         while(it.hasNext()){
41             Student s=(Student)it.next();
42             System.out.println(s);
43         }        
44     }
45     
46     
47 }

 

运行结果显示:

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]

 

5,分页查询;

StudentTest.java

 1 package com.wishwzp.service;
 2 
 3 
 4 import java.util.Iterator;
 5 import java.util.List;
 6 
 7 import org.hibernate.Query;
 8 import org.hibernate.Session;
 9 import org.hibernate.SessionFactory;
10 import org.junit.After;
11 import org.junit.Before;
12 import org.junit.Test;
13 
14 import com.wishwzp.model.Student;
15 import com.wishwzp.util.HibernateUtil;
16 
17 public class StudentTest {
18 
19     private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
20     private Session session;
21     
22     @Before
23     public void setUp() throws Exception {
24         session=sessionFactory.openSession(); // 生成一个session
25         session.beginTransaction(); // 开启事务
26     }
27 
28     @After
29     public void tearDown() throws Exception {
30          session.getTransaction().commit(); // 提交事务
31          session.close(); // 关闭session
32     }
33     
34     @Test
35     public void testHQLQuery5() {
36         String hql="from Student";
37         Query query=session.createQuery(hql);
38         query.setFirstResult(1);
39         query.setMaxResults(2);
40         List<Student> studentList=(List<Student>)query.list();
41         Iterator it=studentList.iterator();
42         while(it.hasNext()){
43             Student s=(Student)it.next();
44             System.out.println(s);
45         }        
46     }
47 
48 }

 

运行结果显示:

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]

 

6,查询单个对象;

StudentTest.java

 1 package com.wishwzp.service;
 2 
 3 
 4 import java.util.Iterator;
 5 import java.util.List;
 6 
 7 import org.hibernate.Query;
 8 import org.hibernate.Session;
 9 import org.hibernate.SessionFactory;
10 import org.junit.After;
11 import org.junit.Before;
12 import org.junit.Test;
13 
14 import com.wishwzp.model.Student;
15 import com.wishwzp.util.HibernateUtil;
16 
17 public class StudentTest {
18 
19     private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
20     private Session session;
21     
22     @Before
23     public void setUp() throws Exception {
24         session=sessionFactory.openSession(); // 生成一个session
25         session.beginTransaction(); // 开启事务
26     }
27 
28     @After
29     public void tearDown() throws Exception {
30          session.getTransaction().commit(); // 提交事务
31          session.close(); // 关闭session
32     }
33     
34     @Test
35     public void testHQLQuery6() {
36         String hql="from Student";
37         Query query=session.createQuery(hql);
38         query.setFirstResult(1);
39         query.setMaxResults(1);
40         Student student=(Student)query.uniqueResult();
41         System.out.println(student);    
42     }
43     
44 }

 

运行结果显示:

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]

 

7,链式写法;

StudentTest.java

 1 package com.wishwzp.service;
 2 
 3 
 4 import java.util.Iterator;
 5 import java.util.List;
 6 
 7 import org.hibernate.Query;
 8 import org.hibernate.Session;
 9 import org.hibernate.SessionFactory;
10 import org.junit.After;
11 import org.junit.Before;
12 import org.junit.Test;
13 
14 import com.wishwzp.model.Student;
15 import com.wishwzp.util.HibernateUtil;
16 
17 public class StudentTest {
18 
19     private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
20     private Session session;
21     
22     @Before
23     public void setUp() throws Exception {
24         session=sessionFactory.openSession(); // 生成一个session
25         session.beginTransaction(); // 开启事务
26     }
27 
28     @After
29     public void tearDown() throws Exception {
30          session.getTransaction().commit(); // 提交事务
31          session.close(); // 关闭session
32     }
33     
34     @Test
35     public void testHQLQuery7() {
36         String hql="from Student as s where s.name like :stuName and s.age=:stuAge";
37         Query query=session.createQuery(hql);
38         List<Student> studentList=(List<Student>)query
39                 .setString("stuName", "张%")
40                 .setInteger("stuAge", 10)
41                 .list();
42         Iterator it=studentList.iterator();
43         while(it.hasNext()){
44             Student s=(Student)it.next();
45             System.out.println(s);
46         }        
47     }
48 }

 

 

运行结果显示:

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]

 

第四节:QBC 查询方式

 

QBC 查询方式(Query By Criteria)是用一套接口来实现的查询方式;

1,普通查询;

2,带条件查询;

3,对结果排序;

4,分页查询;

5,查询单个对象;

6,链式写法;

 

1,普通查询;

StudentTest.java

 1 package com.wishwzp.service;
 2 
 3 
 4 import java.util.Iterator;
 5 import java.util.List;
 6 
 7 import org.hibernate.Criteria;
 8 import org.hibernate.Query;
 9 import org.hibernate.Session;
10 import org.hibernate.SessionFactory;
11 import org.hibernate.criterion.Criterion;
12 import org.hibernate.criterion.Order;
13 import org.hibernate.criterion.Restrictions;
14 import org.junit.After;
15 import org.junit.Before;
16 import org.junit.Test;
17 
18 import com.wishwzp.model.Student;
19 import com.wishwzp.util.HibernateUtil;
20 
21 public class StudentTest {
22 
23     private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
24     private Session session;
25     
26     @Before
27     public void setUp() throws Exception {
28         session=sessionFactory.openSession(); // 生成一个session
29         session.beginTransaction(); // 开启事务
30     }
31 
32     @After
33     public void tearDown() throws Exception {
34          session.getTransaction().commit(); // 提交事务
35          session.close(); // 关闭session
36     }
37 
38     
39     @Test
40     public void testQBCQuery1(){
41         Criteria criteria=session.createCriteria(Student.class);
42         List<Student> studentList=criteria.list();
43         Iterator it=studentList.iterator();
44         while(it.hasNext()){
45             Student s=(Student)it.next();
46             System.out.println(s);
47         }    
48     }
49     
50 }

 

 

运行结果显示:

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]

 

2,带条件查询;

StudentTest.java

 1 package com.wishwzp.service;
 2 
 3 
 4 import java.util.Iterator;
 5 import java.util.List;
 6 
 7 import org.hibernate.Criteria;
 8 import org.hibernate.Query;
 9 import org.hibernate.Session;
10 import org.hibernate.SessionFactory;
11 import org.hibernate.criterion.Criterion;
12 import org.hibernate.criterion.Order;
13 import org.hibernate.criterion.Restrictions;
14 import org.junit.After;
15 import org.junit.Before;
16 import org.junit.Test;
17 
18 import com.wishwzp.model.Student;
19 import com.wishwzp.util.HibernateUtil;
20 
21 public class StudentTest {
22 
23     private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
24     private Session session;
25     
26     @Before
27     public void setUp() throws Exception {
28         session=sessionFactory.openSession(); // 生成一个session
29         session.beginTransaction(); // 开启事务
30     }
31 
32     @After
33     public void tearDown() throws Exception {
34          session.getTransaction().commit(); // 提交事务
35          session.close(); // 关闭session
36     }
37     
38     @Test
39     public void testQBCQuery2(){
40         Criteria criteria=session.createCriteria(Student.class);
41         Criterion c1=Restrictions.like("name", "张%");
42         Criterion c2=Restrictions.eq("age", 10);
43         criteria.add(c1);
44         criteria.add(c2);
45         List<Student> studentList=criteria.list();
46         Iterator it=studentList.iterator();
47         while(it.hasNext()){
48             Student s=(Student)it.next();
49             System.out.println(s);
50         }    
51     }
52     
53 }

 

运行结果显示:

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=1, name=张三, age=10]

 

3,对结果排序;

StudentTest.java

 1 package com.wishwzp.service;
 2 
 3 
 4 import java.util.Iterator;
 5 import java.util.List;
 6 
 7 import org.hibernate.Criteria;
 8 import org.hibernate.Query;
 9 import org.hibernate.Session;
10 import org.hibernate.SessionFactory;
11 import org.hibernate.criterion.Criterion;
12 import org.hibernate.criterion.Order;
13 import org.hibernate.criterion.Restrictions;
14 import org.junit.After;
15 import org.junit.Before;
16 import org.junit.Test;
17 
18 import com.wishwzp.model.Student;
19 import com.wishwzp.util.HibernateUtil;
20 
21 public class StudentTest {
22 
23     private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
24     private Session session;
25     
26     @Before
27     public void setUp() throws Exception {
28         session=sessionFactory.openSession(); // 生成一个session
29         session.beginTransaction(); // 开启事务
30     }
31 
32     @After
33     public void tearDown() throws Exception {
34          session.getTransaction().commit(); // 提交事务
35          session.close(); // 关闭session
36     }
37 
38     
39     @Test
40     public void testQBCQuery3(){
41         Criteria criteria=session.createCriteria(Student.class);
42         criteria.addOrder(Order.desc("age"));
43         List<Student> studentList=criteria.list();
44         Iterator it=studentList.iterator();
45         while(it.hasNext()){
46             Student s=(Student)it.next();
47             System.out.println(s);
48         }    
49     }
50     
51 }

 

运行结果显示:

Hibernate: select this_.stuId as stuId1_0_0_, this_.stuName as stuName2_0_0_, this_.stuAge as stuAge3_0_0_ from t_student this_ order by this_.stuAge desc
Student [id=2, name=李四, age=15]
Student [id=3, name=王五, age=13]
Student [id=1, name=张三, age=10]

 

4,分页查询;

StudentTest.java

 1 package com.wishwzp.service;
 2 
 3 
 4 import java.util.Iterator;
 5 import java.util.List;
 6 
 7 import org.hibernate.Criteria;
 8 import org.hibernate.Query;
 9 import org.hibernate.Session;
10 import org.hibernate.SessionFactory;
11 import org.hibernate.criterion.Criterion;
12 import org.hibernate.criterion.Order;
13 import org.hibernate.criterion.Restrictions;
14 import org.junit.After;
15 import org.junit.Before;
16 import org.junit.Test;
17 
18 import com.wishwzp.model.Student;
19 import com.wishwzp.util.HibernateUtil;
20 
21 public class StudentTest {
22 
23     private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
24     private Session session;
25     
26     @Before
27     public void setUp() throws Exception {
28         session=sessionFactory.openSession(); // 生成一个session
29         session.beginTransaction(); // 开启事务
30     }
31 
32     @After
33     public void tearDown() throws Exception {
34          session.getTransaction().commit(); // 提交事务
35          session.close(); // 关闭session
36     }
37     
38     @Test
39     public void testQBCQuery4(){
40         Criteria criteria=session.createCriteria(Student.class);
41         criteria.setFirstResult(2);
42         criteria.setMaxResults(2);
43         List<Student> studentList=criteria.list();
44         Iterator it=studentList.iterator();
45         while(it.hasNext()){
46             Student s=(Student)it.next();
47             System.out.println(s);
48         }    
49     }
50 
51 }

 

运行结果显示:

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]

 

5,查询单个对象;

StudentTest.java

 1 package com.wishwzp.service;
 2 
 3 
 4 import java.util.Iterator;
 5 import java.util.List;
 6 
 7 import org.hibernate.Criteria;
 8 import org.hibernate.Query;
 9 import org.hibernate.Session;
10 import org.hibernate.SessionFactory;
11 import org.hibernate.criterion.Criterion;
12 import org.hibernate.criterion.Order;
13 import org.hibernate.criterion.Restrictions;
14 import org.junit.After;
15 import org.junit.Before;
16 import org.junit.Test;
17 
18 import com.wishwzp.model.Student;
19 import com.wishwzp.util.HibernateUtil;
20 
21 public class StudentTest {
22 
23     private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
24     private Session session;
25     
26     @Before
27     public void setUp() throws Exception {
28         session=sessionFactory.openSession(); // 生成一个session
29         session.beginTransaction(); // 开启事务
30     }
31 
32     @After
33     public void tearDown() throws Exception {
34          session.getTransaction().commit(); // 提交事务
35          session.close(); // 关闭session
36     }
37     
38     @Test
39     public void testQBCQuery5(){
40         Criteria criteria=session.createCriteria(Student.class);
41         criteria.setFirstResult(2);
42         criteria.setMaxResults(1);
43         Student student=(Student)criteria.uniqueResult();
44         System.out.println(student);
45     }
46     
47 }

 

运行结果显示:

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]

 

6,链式写法;

StudentTest.java

 1 package com.wishwzp.service;
 2 
 3 
 4 import java.util.Iterator;
 5 import java.util.List;
 6 
 7 import org.hibernate.Criteria;
 8 import org.hibernate.Query;
 9 import org.hibernate.Session;
10 import org.hibernate.SessionFactory;
11 import org.hibernate.criterion.Criterion;
12 import org.hibernate.criterion.Order;
13 import org.hibernate.criterion.Restrictions;
14 import org.junit.After;
15 import org.junit.Before;
16 import org.junit.Test;
17 
18 import com.wishwzp.model.Student;
19 import com.wishwzp.util.HibernateUtil;
20 
21 public class StudentTest {
22 
23     private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
24     private Session session;
25     
26     @Before
27     public void setUp() throws Exception {
28         session=sessionFactory.openSession(); // 生成一个session
29         session.beginTransaction(); // 开启事务
30     }
31 
32     @After
33     public void tearDown() throws Exception {
34          session.getTransaction().commit(); // 提交事务
35          session.close(); // 关闭session
36     }
37     
38     @Test
39     public void testQBCQuery6(){
40         Criteria criteria=session.createCriteria(Student.class);
41         List<Student> studentList=criteria
42                 .setFirstResult(0)
43                 .setMaxResults(2)
44                 .list();
45         Iterator it=studentList.iterator();
46         while(it.hasNext()){
47             Student s=(Student)it.next();
48             System.out.println(s);
49         }    
50     }
51     
52 }

 

运行结果显示:

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]

 


 

END

posted @ 2016-05-17 23:43  kika  阅读(251)  评论(0编辑  收藏  举报