分頁
@SuppressWarnings("unchecked")
public List
Criteria criteria = ss.createCriteria(User.class);
criteria.add(Restrictions.eq("userId", userId));
criteria.add(Restrictions.eq("state", state));
criteria.setFirstResult(first);
criteria.setMaxResults(size);
return criteria.list();
}
排序
@SuppressWarnings("unchecked")
public List
Criteria criteria = ss.createCriteria(User.class);
criteria.add(Restrictions.eq("userId", userId));
criteria.add(Restrictions.eq("state", state));
criteria.addOrder(Order.desc("updateTime"));
criteria.setFirstResult(first);
criteria.setMaxResults(size);
return criteria.list();
}
使用add()方法加入條件時, 預設是使用and來組合條件, 若要用or
Criteria criteria = ss.createCriteria(User.class);
criteria.add(Restrictions.eq("userId", userId));
criteria.add(Restrictions.or(
Restrictions.eq("state", new Integer(1) ),
Restrictions.isNull("state")
));
return criteria.list();
Like
Criteria criteria = ss.createCriteria(User.class);
criteria.add(Restrictions.eq("userId", userId));
criteria.add(Restrictions.like("phone", "%" + phone + "%"));
criteria.add(Restrictions.eq("userId", userId));
criteria.add(Restrictions.or(
Restrictions.eq("state", new Integer(1) ),
Restrictions.isNull("state")
));
return criteria.list();
Like
Criteria criteria = ss.createCriteria(User.class);
criteria.add(Restrictions.eq("userId", userId));
criteria.add(Restrictions.like("phone", "%" + phone + "%"));
return criteria.list();
Restrictions
Restrictions.eq
Restrictions.allEq (參數為map)
Restrictions.gt
Restrictions.ge
Restrictions.lt
Restrictions.le
Restrictions.between
Restrictions.like
Restrictions.in
Restrictions.and
Restrictions.or
Restrictions.isNull
Restrictions.isNotNull
Restrictions.sqlRestriction
Order.asc
Order.descMatchMode.EXACT = "like 'value'"
MatchMode.ANYWHERE = "like '%value%'"
MatchMode.START = like 'value%'"
MatchMode.END = "like '%value'"
Disjunction
Criteria criteria = ss.createCriteria(User.class);
criteria.add(Restrictions.eq("userId", userId));
Criterion phoneCriterion = Restrictions.like("phone", phone, MatchMode.ANYWHERE);
Criterion mobileCriterion = Restrictions.like("mobile", mobile, MatchMode.ANYWHERE);
Disjunction disjunction = Restrictions.disjunction();
disjunction.add(phoneCriterion);
disjunction.add(mobileCriterion);
criteria.add(disjunction);
return criteria.list();
Count
public int countByUserIdAndState(int userId, int state) {
Criteria criteria = ss.createCriteria(User.class);
criteria.add(Restrictions.eq("userId", userId));
criteria.add(Restrictions.eq("state", state));
criteria.setProjection(Projections.rowCount());
return (Integer) criteria.uniqueResult();
}
group by
ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.groupProperty("area"), "area");
projectionList.add(Projections.rowCount(), "userId");
Criteria criteria = ss.createCriteria(User.class);
criteria.add(Restrictions.eq("userId", userId));
criteria.setProjection(projectionList);
return criteria.list();
回傳結果
public List<HashMap<String, Integer>> listUser(int userId) {
ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.groupProperty("area"), "area");
projectionList.add(Projections.rowCount(), "userId");
Criteria criteria = ss.createCriteria(User.class);
criteria.add(Restrictions.eq("userId", userId));
criteria.setProjection(projectionList);
criteria.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
return criteria.list();
}
Example 用目前的物件查詢
User user = new User();
user.setAge(new Integer(30));
Criteria criteria = session.createCriteria(User.class);
criteria.add(Example.create(user));
List users = criteria.list();
Hibernate: select this_.id as id0_0_, this_.name as name0_0_, this_.age as age0_0_ from T_USER this_ where (this_.age=?)
sqlRestriction - 直接下SQL
Hibernate: select this_.id as id0_0_, this_.name as name0_0_, this_.age as age0_0_ from T_USER this_ where (this_.age=?)
sqlRestriction - 直接下SQL
criteria.add(Restrictions.sqlRestriction(
"{alias}.name LIKE (?)", "todd%", Hibernate.STRING));
List users = criteria.list();
HQL
String hql = "select * from user";
Query query = session.createQuery(hql);
list = query.list();
沒有留言:
張貼留言