//设定需要设定的字段
String[] name = new String[]{
"usersLoginid","usersPwd","usersName"
};
//设置的属性
Criterion criterion = null;
Criteria cr = session.createCriteria(Users.class);
for (int i=0;i<name.length;i++) {
if("usersLoginid".equals(name[i])) {
criterion = or(criterion, Restrictions.like ("usersLoginid","%cus%"));
} else if("usersPwd".equals(name[i])) {
criterion = or(criterion, Restrictions.like ("usersPwd","%1234%"));
} else if("usersName".equals(name[i])) {
criterion = or(criterion, Restrictions.like ("usersName","%ewq%"));
}
}
cr.add(criterion);
List<Users> list = cr.addOrder(Order.desc("usersId"))
.setFirstResult(8)
.setMaxResults(4)
.list();
另外一个重要方法or如下:
public Criterion or(Criterion crit1, Criterion crit2) {
if (crit1 == null) {
return crit2;
}
if (crit2 == null) {
return crit1;
}
return Restrictions.or(crit1, crit2);
}