67,516
社区成员
发帖
与我相关
我的任务
分享
/**
* 使用对象数组的查询方法
*
* @param c:查询的类
* @param obj:查询的对象
* @param orber:按那个字段排序
* @param row:每页多少条记录
* @param page:第几页
* @return List: 集合
*/
public List find(Class c, Object[] obj, String[] order, int row, int page) {
// 用spring的类创建session
Session session = this.getSession();
List list = null;
int cou=1;
try {
Criteria criteria=null;
if (obj != null) {
for (int i = 0; i < obj.length; i++) {
if (obj[i]!=null) {
Class c1=obj[i].getClass();
criteria=session.createCriteria(c1);
Example example = Example.create(obj[i]);
//example.enableLike(MatchMode.ANYWHERE);// 匹配模式,使用模糊查询必填项。
// example.excludeNone();//空的不做查询条件
example.excludeZeroes();// 0不要查询
example.ignoreCase();
criteria.add(example);
cou++;
}
}
}
if(cou==1)
criteria=session.createCriteria(c);
if (row != 0 && page != 0) {
criteria.setMaxResults(row);// 最大显示记录数
criteria.setFirstResult((page - 1) * row);// 从第几条开始
}
// 判断是否有排序请求,如果有加入到排序方法中
if (order != null) {
for (int i = 0; i < order.length; i++)
criteria.addOrder(Order.desc(order[i]));
}
list = criteria.list();
} catch (HibernateException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} finally {
session.close();
}
return list;
}
public List listUser() throws DataAccessException, HibernateException {
return (List) getHibernateTemplate().find("select u.username, r.roomname from User u,Room r where u.userid=r.userid");
}