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() {
}
}
生成的表结构
手动插入几条数据
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结果:
testSQLQuery2结果:
4.HQL查询方式
是面向对象的查询语句言,是使用最广泛的语言,以后一般用HQL
数据库:
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.查询所有数据
2.带条件的查询
3.使用别名
4.按照年龄降序排序
5.分页
6.查询单个对象
7.链式写法
5.QBC查询方式(Query By Criteria)是一套用接口查询的方式
数据库:
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.普通查询
2.带条件查询
3.对结果排序(年龄降序)
4.分页
5.查询单个对象
6.链式写法