Criteria对象提供了一种面向对象的方式查询数据库。
Criteria对象需要使用Session对象来获得一个Criteria对象表示对一个
持久化类的查询
查询所有
Session session = HibernateUtil.getSession();
session.beginTransaction();
Criteria cri = session.createCriteria(User.class);
List<User> list = cri.list();
for(User user : list) {
System.out.println(user.getId() + " : " + user.getUsername());
}
session.getTransaction().commit();
where
//1
Session session = HibernateUtil.getSession();
session.beginTransaction();
Criteria cri = session.createCriteria(User.class); //条件
cri.add(Restrictions.eq("username", "u1")); List<User> list = cri.list();
for(User user : list) {
System.out.println(user.getId() + " : " + user.getUsername());
}
session.getTransaction().commit();
//2 Session session = HibernateUtil.getSession();
session.beginTransaction();
Criteria cri = session.createCriteria(User.class); //条件 这是and效果
cri.add(Restrictions.eq("username", "u1"));
cri.add(Restrictions.eq("password","123")); List<User> list = cri.list();
for(User user : list) {
System.out.println(user.getId() + " : " + user.getUsername());
}
session.getTransaction().commit();
//3 Session session = HibernateUtil.getSession();
session.beginTransaction();
Criteria cri = session.createCriteria(User.class); //条件 这是or效果
cri.add(Restrictions.or(Restrictions.eq("username", "u1"),Restrictions.eq("username", "jack"))); List<User> list = cri.list();
for(User user : list) {
System.out.println(user.getId() + " : " + user.getUsername());
}
session.getTransaction().commit(); // like Session session = HibernateUtil.getSession();
session.beginTransaction();
Criteria cri = session.createCriteria(User.class);
/*MatchMode.ANYWHERE任何地方 MatchMode.START从开始 MatchMode.END表示u结尾的*/
cri.add(Restrictions.like("username", "u", MatchMode.ANYWHERE)); List<User> list = cri.list();
for(User user : list) {
System.out.println(user.getId() + " : " + user.getUsername());
}
session.getTransaction().commit();
分页
cri.setFirstResult(0);
cri.setMaxResults(5);
分组与统计
Creteria cri = session createCriteria(User.class);
cri.setProjection(Projections.count("id"));
Long count = (Long)cri.uniqueResult();
syso(count);
多个统计与分组
Creteria cri = session createCriteria(User.class);
ProjectionList pList = Projections.projectionList();
pList.add(Projections.max("id"));
pList.add(Projections.min("id")); cri.setProjection(pList); Object[] array = (Object[]) cri.uniqueResult();
System.out.println(array[0] + " : " +array[1]);
排序
Session session = HibernateUtil.getSession();
session.beginTransaction();
Criteria cri = session.createCriteria(User.class); cri.addOrder(Order.desc("id"));
cri.addOrder(Order.asc("username")); List<User> list = cri.list();
for(User user : list) {
System.out.println(user.getId() + " : " + user.getUsername());
}
session.getTransaction().commit();
------------------------------------------------
------------------------------------------------
当然也可以使用原生sql
Session session = HibernateUtil.getSession();
session.beginTransaction(); String sql = "select * from user";
SQLQuery query = session.createSQLQuery(sql);
List<Object[]> list = query.list(); for(Object[] array : list) {
System.out.println(array[0] + " : " + array[1]);
} String sql = "select * from user";
SQLQuery query = session.createSQLQuery(sql).addEntity(User.class);
List<User> list = query.list(); for(User user : list) {
System.out.println(user.getId() + " : " + user.getUsername());
}