简单查询,使用的是Criteria接口
List 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 list = criteria.list();
for(Linkman linkman :list){
System.out.println(linkman);
}
tr.commit();
条件查询(Criteria是查询条件的接口,Restriction类是Hibernate框架提供的工具类,使用该工具类设置查询条件)
条件查询使用Criteria接口的add方法,用来传入条件。
使用Restrictions的添加条件的方法,来添加条件,例如:
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 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 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 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 params = new ArrayList();
params.add(1L);
params.add(2L);
params.add(7L);
criteria.add(Restrictions.in("lkm_id",params));
List 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 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 list = criteria.list();
for(Linkman linkman : list){
System.out.println(linkman);
}
tr.commit();
}
Projection的聚合函数的接口,而Projections是Hibernate提供的工具类,使用该工具类设置聚合函数查询
Session session = HibernateUtils.getCurrentSession();
Transaction tr = session.beginTransaction();
Criteria criteria = session.createCriteria(Linkman.class);
criteria.setProjection(Projections.rowCount());
List 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 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 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 list = add.getExecutableCriteria(session).list();
for(Linkman linkman : list){
System.out.println(linkman);
}
tr.commit();
}
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 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