Hibernate框架查詢方式之QBC查詢
阿新 • • 發佈:2018-12-16
QBC(Query By Criteria)檢索方式
簡單查詢,使用的是Criteria介面
List<Customer> list = session.createCriteria(Customer.class).list();
for(Customer customer : list){
System.out.println(customer);
}
排序查詢
需要使用addOrder()的方法來設定引數,引數使用org.hibernate.criterion.Order物件
Session session = HibernateUtils.getCurrentSession(); Transaction tr = session.beginTransaction(); Criteria criteria = session.createCriteria(Linkman.class); //設定排序 criteria.addOrder(Order.desc("lkm_id")); List<Linkman> list = criteria.list(); for(Linkman linkman :list){ System.out.println(linkman); } tr.commit();
條件查詢(Criteria是查詢條件的介面,Restriction類是Hibernate框架提供的工具類,使用該工具類設定查詢條件)
條件查詢使用Criteria介面的add方法,用來傳入條件。
使用Restrictions的新增條件的方法,來新增條件,例如:
- Restrictions.eq-----相等
- Restrictions.gt-----大於號
- Restrictions.ge-----大於等於
- Restrictions.lt----小於
- Restrictions.le----小於等於
- Restrictions.between-----在…之間
- Restrictions.like----模糊查詢
- Restrictions.in----範圍
- Restrictions.and----並且
- Restrictions.or====或者
Session session = HibernateUtils.getCurrentSession(); Transaction tr = session.beginTransaction(); Criteria criteria = session.createCriteria(Linkman.class); //設定排序 criteria.addOrder(Order.desc("lkm_id")); //設定查詢條件 criteria.add(Restrictions.or(Restrictions.eq("lkm_gender","男"),Restrictions.gt("lkm_id",3L)));
@Test
public void run1(){
Session session =HibernateUtils.getCurrentSession();
Transaction tr = session.beginTransaction();
//建立QBC查詢介面
Criteria critera = session.createCriteria(Customer.class);
List<Customer> list= critera.list();
for(Customer customer : list){
System.out.println(customer);
}
tr.commit();
}
//排序查詢
@Test
public void run2(){
Session session =HibernateUtils.getCurrentSession();
Transaction tr = session.beginTransaction();
//建立QBC查詢介面
Criteria criteria = session.createCriteria(Linkman.class);
//呼叫排序的方法
criteria.addOrder(Order.desc("lkm_id"));
List<Linkman> list = criteria.list();
for(Linkman linkman : list){
System.out.println(linkman);
}
tr.commit();
}
//分頁查詢
@Test
public void run3(){
Session session = HibernateUtils.getCurrentSession();
Transaction tr = session.beginTransaction();
//建立QBC查詢介面
Criteria criteria = session.createCriteria(Linkman.class);
//設定分頁的方法
criteria.setFirstResult(0);
criteria.setMaxResults(3);
List<Linkman> list = criteria.list();
for(Linkman linkman :list){
System.out.println(linkman);
}
tr.commit();
}
//條件查詢
@Test
public void run4(){
Session session = HibernateUtils.getCurrentSession();
Transaction tr = session.beginTransaction();
//建立QBC查詢介面
Criteria criteria = session.createCriteria(Linkman.class);
//使用方法新增條件,兩個條件預設是and連線
// criteria.add(Restrictions.eq("lkm_gender","男"));
// criteria.add(Restrictions.gt("lkm_id",3L));
//between
// criteria.add(Restrictions.between("lkm_id",2L,5L));
//in方法查詢
//SQL :select * from cst_linkman where lkm_id in (1,2,7);
List<Long> params = new ArrayList<Long>();
params.add(1L);
params.add(2L);
params.add(7L);
criteria.add(Restrictions.in("lkm_id",params));
List<Linkman> list = criteria.list();
for(Linkman linkman : list){
System.out.println(linkman);
}
tr.commit();
}
@Test
public void run5(){
Session session = HibernateUtils.getCurrentSession();
Transaction tr = session.beginTransaction();
//建立QBC查詢介面
Criteria criteria = session.createCriteria(Linkman.class);
//SQL:select * from cst_linkman where lkm_gender = ? or lkm_id > ?
//SQL:select * from cst_linkman where lkm_gender = '女' or lkm_id > 3L
criteria.add(Restrictions.or(Restrictions.eq("lkm_gender","女"),Restrictions.gt("lkm_id",3L)));
List<Linkman> list = criteria.list();
for(Linkman linkman : list){
System.out.println(linkman);
}
tr.commit();
}
//判斷值是否為空
@Test
public void run6(){
Session session = HibernateUtils.getCurrentSession();
Transaction tr = session.beginTransaction();
//建立QBC查詢介面
Criteria criteria = session.createCriteria(Linkman.class);
//查詢所以的email為空的值
criteria.add(Restrictions.isNull("lkm_email"));
List<Linkman> list = criteria.list();
for(Linkman linkman : list){
System.out.println(linkman);
}
tr.commit();
}
聚合函式查詢
Projection的聚合函式的介面,而Projections是Hibernate提供的工具類,使用該工具類設定聚合函式查詢
- 使用QBC的聚合函式查詢,需要使用criteria.setProjection()方法
Session session = HibernateUtils.getCurrentSession();
Transaction tr = session.beginTransaction();
Criteria criteria = session.createCriteria(Linkman.class);
criteria.setProjection(Projections.rowCount());
List<Number> list = criteria.list();
Long count = list.get(0).longValue();
system.out.println(count);
tr.commit();
public void run8(){
Session session = HibernateUtils.getCurrentSession();
Transaction tr = session.beginTransaction();
//建立QBC查詢介面
Criteria criteria = session.createCriteria(Linkman.class);
//設定聚合函式的方式
List<Number> list = criteria.setProjection(Projections.count("lkm_id")).list();
Long count = list.get(0).longValue();
System.out.println(count);
//再設定一遍setProjection方法
criteria.setProjection(null);
//繼續查詢所有的聯絡人 select * from 表、
List<Linkman> mans = criteria.list();
for(Linkman linkman : mans){
System.out.println(linkman);
}
tr.commit();
}
離線條件查詢
@Test
public void run10(){
Session session = HibernateUtils.getCurrentSession();
Transaction tr = session.beginTransaction();
//建立離線條件查詢的物件
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Linkman.class);
//新增查詢的條件
DetachedCriteria add = detachedCriteria.add(Restrictions.eq("lkm_gender", "女"));
//查詢
List<Linkman> list = add.getExecutableCriteria(session).list();
for(Linkman linkman : list){
System.out.println(linkman);
}
tr.commit();
}
SQL的查詢方式
Session session = HibernateUtils.getCurrentSession();
Transaction tr = session.beginTransaction();
SQLQuery sqlQuery = session.createSQLQuery("select * from cst_linkman where lkm_gender=?");
sqlQuery.setParameter(0,"男");
sqlQuery.addEntity(Linkman.class);
List<Linkman> list = sqlQuery.list();
System.out.println(list);
tr.commit();
@Test
public void run11(){
Session session = HibernateUtils.getCurrentSession();
Transaction tr = session.beginTransaction();
SQLQuery query = session.createSQLQuery("select * from cst_linkman");
List<Object[]> list = query.list();
for(Object[] objects: list){
System.out.println(objects);
}
tr.commit();
}
@Test
public void run12(){
Session session = HibernateUtils.getCurrentSession();
Transaction tr = session.beginTransaction();
SQLQuery query = session.createSQLQuery("select * from cst_linkman");
query.addEntity(Linkman.class);
List<Linkman> list = query.list();
for(Linkman linkman : list){
System.out.println(linkman);
}
tr.commit();
}