条件查询(Criteria Queries)
@Test public void testCriteriaAnd(){ List<User> users = session.createCriteria(User.class) .add(Restrictions.eq("name", "张三"))//相等 .add(Restrictions.like("name", "王%"))//模糊查询:自己添加模糊通配符 .add(Restrictions.like("name", "王", MatchMode.END))//模糊查询:MatchMode:END、START、ANYWHERE、EXACT .add(Restrictions.gt("age", 24))//大于 .add(Restrictions.lt("age", 28))//小于 .add(Restrictions.between("age", 24, 28))//介于两者之间 .add(Restrictions.in("name", new String[]{"张三","李四"}))//包含 .add(Restrictions.sqlRestriction("name = '张三'"))//添加sql语句 .add(Property.forName("name").in(new String[]{"张三","李四"}))//使用priperty查询 .add(Restrictions.or( Restrictions.eq("name", "张三"), Restrictions.gt("age", 28) ))//or连接 .addOrder(Property.forName("age").asc())//排序 .setFirstResult(0)//分页:起始条数 .setMaxResults(2)//分页:显示几条 .list(); for(User u : users){ System.out.println(u.getName()+" "+u.getAge()); } }