Hibernate 查询方式
Posted kika
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 以上是关于Hibernate 查询方式的主要内容,如果未能解决你的问题,请参考以下文章