Hibernate查询方式简介

1.导航对象查询方式:比如通过学生查找到班级的级联查询

2.OID查询方式:比如get()查询,一次只能查一条数据

Student student=(Student)session.get(Student.class, 1);

student.getName()

3.本地SQL查询方式
 

实例

创建一个简单的学生数据

Student.java

package com.java.model;

public class Student {
	private String name;
	private int id;
	private int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", id=" + id + ", age=" + age + "]";
	}
	
	
	

}

Student.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.java.model"> 
	<class name="Student" table="t_student">
	<id name="id" column="student_id">
		<generator class="native"></generator>
	</id>
	
	<property name="name" column="student_name"></property>
	<property name="age" column="student_age"></property>
	
	</class>


</hibernate-mapping>

hibernate.cfg.xml

<mapping resource="com/java/model/Student.hbm.xml"/>

StudentTest.java

package com.java.service;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.java.util.HibernateUtil;

public class StudentTest {

	private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
	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() {
		

	}
	
	
}

生成的表结构

a0a9e77a085deda7b5f30c2d03b2cb7c229.jpg

手动插入几条数据

b82c5d5c07b23698346fa2ded49740a7278.jpg

StudentTest.java

package com.java.service;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;

import com.java.model.Student;
import com.java.util.HibernateUtil;

public class StudentTest {

	private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
	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";//sql语句
		//.addEntity(Student.class) 要绑定实体,不然遍历不出来
		Query query=session.createSQLQuery(sql).addEntity(Student.class);
		List studentList=query.list();
		Iterator it=studentList.iterator();
		while(it.hasNext()){
			//强制转换为Student类型
			Student s=(Student)it.next();
			System.out.println(s);
		}		
	}
	
	@Test
	public void testSQLQuery2() {
		//模糊查询
		String sql="select * from t_student where student_name like :stuName and student_age=:stuAge";
		Query query=session.createSQLQuery(sql).addEntity(Student.class);
		query.setString("stuName", "张%");
		query.setInteger("stuAge", 13);
		List studentList=query.list();
		Iterator it=studentList.iterator();
		while(it.hasNext()){
			Student s=(Student)it.next();
			System.out.println(s);
		}		
	}
	
	
	
}

testSQLQuery结果:63644d28dfcf3e79106756cb074bedb7e8a.jpg

testSQLQuery2结果:b4001e617d0abe9a5383e5d6ebdb2b85c44.jpg

 

4.HQL查询方式

是面向对象的查询语句言,是使用最广泛的语言,以后一般用HQL

数据库:

cb62471cc7f5e53100f51f00d1ad19723d0.jpg

package com.java.service;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;

import com.java.model.Student;
import com.java.util.HibernateUtil;

public class StudentTest {

	private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
	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";//sql语句
		//.addEntity(Student.class) 要绑定实体,不然遍历不出来
		Query query=session.createSQLQuery(sql).addEntity(Student.class);
		List studentList=query.list();
		Iterator it=studentList.iterator();
		while(it.hasNext()){
			//强制转换为Student类型
			Student s=(Student)it.next();
			System.out.println(s);
		}		
	}
	
	@Test
	public void testSQLQuery2() {
		//模糊查询
		String sql="select * from t_student where student_name like :stuName and student_age=:stuAge";
		Query query=session.createSQLQuery(sql).addEntity(Student.class);
		query.setString("stuName", "张%");
		query.setInteger("stuAge", 13);
		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 where name like :stuName and age=:stuAge";
		Query query=session.createQuery(hql);
		query.setString("stuName", "张%");
		query.setInteger("stuAge", 13);
		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 as s where s.name like :stuName and s.age=:stuAge";
		Query query=session.createQuery(hql);
		query.setString("stuName", "张%");
		query.setInteger("stuAge", 13);
		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 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 testHQLQuery5() {
		//分页
		String hql="from Student";
		Query query=session.createQuery(hql);
		//从0开始
		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 testHQLQuery6() {
		//查询单个对象
		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 testHQLQuery7() {
		//链式写法
		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", "张%")
				.setInteger("stuAge", 13)
				.list();
		Iterator it=studentList.iterator();
		while(it.hasNext()){
			Student s=(Student)it.next();
			System.out.println(s);
		}		
	}

	
	
}

1.查询所有数据

056c805c0075a2c17e441ec245c3828cff9.jpg

 

2.带条件的查询bde78029631b1b6160b5ce1055109ad3f72.jpg

3.使用别名2f10e0be4d8491dd9b3f5bb35b14083d7c9.jpg

4.按照年龄降序排序

39df85616171dc7ffc30129f92d7e74e2a3.jpg

5.分页

21be6570eedef52e07c8db461d15bff4ffe.jpg

6.查询单个对象

5859ea4100bba4ed389c067b1028cbb4274.jpg

7.链式写法

5fb7150bea752280b6cfe85f957240789d6.jpg

 

5.QBC查询方式(Query By Criteria)是一套用接口查询的方式

数据库:

cb62471cc7f5e53100f51f00d1ad19723d0.jpg

package com.java.service;
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 java.util.Iterator;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;

import com.java.model.Student;
import com.java.util.HibernateUtil;

public class StudentTest {

	private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
	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 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", "张%");
		Criterion c2=Restrictions.eq("age", 13);
		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);
		}	
	}

	
	
}

1.普通查询

eb3e482384bd66bb295af060fa04c9f3e56.jpg

2.带条件查询

679c7888eab0df9c2eb0606221784e89f10.jpg

3.对结果排序(年龄降序)

a88463befa50935995346860fb02427691f.jpg

4.分页

53575f6e16a35c8080bc69d882f1880218e.jpg

5.查询单个对象

8e344d53b76cd983e61ea00908addfb7a50.jpg

6.链式写法

cfe49f340e5b41b2f6a314d590680c1414d.jpg

转载于:https://my.oschina.net/u/3848699/blog/2243759

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值