Criteria查询

1.忽略查询条件大小写

//根据用户名搜索(忽略用户名大小写)
Criteria criteria = HibernateSessionFactory.getSession()
			.createCriteria(User.class);
criteria.add(Restrictions.eq("username", username).ignoreCase());
List<User> userList = criteria.list();

执行后的SQL语句为:select * from user where Lower(username)=Lower(name)

 

2.LIMIT

Criteria criteria = HibernateSessionFactory.getSession()
		.createCriteria(User.class);
criteria.setFirstResult(开始的条数);
criteria.setMaxResults(限制返回的条数);
 

执行后的SQL语句为:select * from user LIMIT staNo,count

 

如果LIMIT只有一个值,则:

Criteria criteria = HibernateSessionFactory.getSession()
	.createCriteria(User.class);
criteria.setMaxResults(count);

 执行后的SQL语句为:select * from user LIMIT count

 

3.count 返回符合查询条件的条数

Criteria criteria = HibernateSessionFactory.getSession()
	.createCriteria(User.class);
criteria.setProjection(Projections.rowCount());
int count = Integer.parseInt(criteria.uniqueResult().toString());

  执行后的SQL语句为:select count(*) from user

 

4.排序

Criteria criteria = HibernateSessionFactory.getSession()
	.createCriteria(User.class);
criteria.addOrder(Order.asc("属性"));//升序
criteria.addOrder(Order.desc("属性"));//降序
  执行后的SQL语句为:select * from user order by 字段 asc|desc

你可能感兴趣的:(sql,Hibernate,Criteria)