hibernate中的检索方式
HQL(*) hibernate query language
代码块
package cn.feicui.hql;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
import cn.feicui.domain.Customer;
import cn.feicui.utils.HibernateUtils;
//测试HQL
public class Demo {
@Test
//查询列表信息
public void fun1(){
//1 获得session
Session session = HibernateUtils.openSession();
//2 控制事务
Transaction tx = session.beginTransaction();
//3执行操作
//1)书写HQL语句,hibernate中采用的是面向对象的操作
//cn.feicui.domain.Customer
String hql =”from Customer”;
//2)根据 HQL语句创建对象
Query query = session.createQuery(hql);
//3)根据查询对象获得查询结果
List<Customer> list = query.list();
System.out.println(list);
//查询一条数据
//Customer c = (Customer)query.uniqueResult();
//4提交事务.关闭资源
tx.commit();
session.close();
}
@Test
//条件查询
public void fun2(){
//1 获得session
Session session = HibernateUtils.openSession();
//2 控制事务
Transaction tx = session.beginTransaction();
//3执行操作
//1)书写HQL语句,hibernate中采用的是面向对象的操作
//cn.feicui.domain.Customer
String hql =”from Customer where cust_id=’4028832e5e02c671015e02c674680000’”;
//2)根据 HQL语句创建对象
Query query = session.createQuery(hql);
//3)根据查询对象获得查询结果
//查询一条数据
Customer c = (Customer)query.uniqueResult();
System.out.println(c);
//4提交事务.关闭资源
tx.commit();
session.close();
}
@Test
//条件查询中的占位符
public void fun3(){
//1 获得session
Session session = HibernateUtils.openSession();
//2 控制事务
Transaction tx = session.beginTransaction();
//3执行操作
//1)书写HQL语句,hibernate中采用的是面向对象的操作
//cn.feicui.domain.Customer
String hql =”from Customer where cust_id= ? “;
//2)根据 HQL语句创建对象
Query query = session.createQuery(hql);
query.setString(0, "4028832e5e02c671015e02c674680000");
//query.setParameter("", "");
//3)根据查询对象获得查询结果
//查询一条数据
Customer c = (Customer)query.uniqueResult();
System.out.println(c);
//4提交事务.关闭资源
tx.commit();
session.close();
}
@Test
//条件查询中的命名占位符
public void fun4(){
//1 获得session
Session session = HibernateUtils.openSession();
//2 控制事务
Transaction tx = session.beginTransaction();
//3执行操作
//1)书写HQL语句,hibernate中采用的是面向对象的操作
//cn.feicui.domain.Customer
String hql =”from Customer where cust_id= :cust_id “;
//2)根据 HQL语句创建对象
Query query = session.createQuery(hql);
//query.setString(0, "4028832e5e02c671015e02c674680000");
query.setParameter("cust_id", "4028832e5e02c671015e02c674680000");
//3)根据查询对象获得查询结果
//查询一条数据
Customer c = (Customer)query.uniqueResult();
System.out.println(c);
//4提交事务.关闭资源
tx.commit();
session.close();
}
@Test
//分页查询
public void fun5(){
//1 获得session
Session session = HibernateUtils.openSession();
//2 控制事务
Transaction tx = session.beginTransaction();
//3执行操作
//1)书写HQL语句,hibernate中采用的是面向对象的操作
//cn.feicui.domain.Customer
String hql =”from Customer “;
//2)根据 HQL语句创建对象
Query query = session.createQuery(hql);
//从哪开始查询
query.setFirstResult(2);
//向后找几条
query.setMaxResults(2);
//3)根据查询对象获得查询结果
List<Customer> list = query.list();
System.out.println(list);
//4提交事务.关闭资源
tx.commit();
session.close();
}
}
QBC(*)
代码块
package cn.feicui.criteria;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.junit.Test;
import cn.feicui.domain.Customer;
import cn.feicui.utils.HibernateUtils;
//测试criteria
public class Demo {
@Test
// 基本查询
public void fun1() {
// 1 获得session
Session session = HibernateUtils.openSession();
// 2 控制事务
Transaction tx = session.beginTransaction();
// 3执行操作
Criteria criteria = session.createCriteria(Customer.class);
List<Customer> list = criteria.list();
System.out.println(list);
// 返回一条数据
// criteria.uniqueResult();
// 4提交事务.关闭资源
tx.commit();
session.close();
}
// 条件查询
/*
* > gt < lt >= ge <= le == eq != ne in in between and between like like is
* null is Null is not null is Not Null or or and and
*
*/
@Test
public void fun2() {
// 1 获得session
Session session = HibernateUtils.openSession();
// 2 控制事务
Transaction tx = session.beginTransaction();
// 3执行操作
Criteria criteria = session.createCriteria(Customer.class);
// criteria.add(Restrictions.eq("cust_id",
// "4028832e5e02c671015e02c674680000"));
// criteria.add(Restrictions.isNotNull("cust_id"));
criteria.add(Restrictions.like("cust_name", "%百渡%"));
// where cust_name like '%cust_name%'
// where cust_id = 4028832e5e02c671015e02c674680000
// 返回一条数据
Customer c = (Customer) criteria.uniqueResult();
System.out.println(c);
// 4提交事务.关闭资源
tx.commit();
session.close();
}
@Test
// 分页查询
public void fun3() {
// 1 获得session
Session session = HibernateUtils.openSession();
// 2 控制事务
Transaction tx = session.beginTransaction();
// 3执行操作
Criteria criteria = session.createCriteria(Customer.class);
// 从哪开始查询
criteria.setFirstResult(0);
criteria.setMaxResults(2);
List<Customer> list = criteria.list();
System.out.println(list);
// 4提交事务.关闭资源
tx.commit();
session.close();
}
@Test
// 获取总记录数
public void fun4() {
// 1 获得session
Session session = HibernateUtils.openSession();
// 2 控制事务
Transaction tx = session.beginTransaction();
// 3执行操作
Criteria criteria = session.createCriteria(Customer.class);
// 设置查询的聚合函数
criteria.setProjection(Projections.rowCount());
Long count = (Long) criteria.uniqueResult();
System.out.println(count);
// 4提交事务.关闭资源
tx.commit();
session.close();
}
}
SQL(*)
代码
package cn.feicui.sql;
import java.util.Arrays;
import java.util.List;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
import cn.feicui.domain.Customer;
import cn.feicui.utils.HibernateUtils;
//测试原生态的SQL
public class Demo {
@Test
//基本查询
public void fun1(){
//1 获得session
Session session = HibernateUtils.openSession();
//2 控制事务
Transaction tx = session.beginTransaction();
//3执行操作
String sql = “select * from cst_customer”;
//创建SQL查询对象
SQLQuery sq = session.createSQLQuery(sql);
List