/**
* 使用对象的查询方法
*
* @param c:查询的类
* @param obj:查询的对象
* @param orber:按那个字段排序
* @param row:每页多少条记录
* @param page:第几页
* @return List: 集合
*/
public List find(Class c, Object obj, String[] orber, int row, int page) {
List list = null;
try {
Criteria criteria = HibernateSessionFactory.getSession()
.createCriteria(c);
if (obj != null) {
Example example = Example.create(obj);
example.enableLike(MatchMode.ANYWHERE);// 匹配模式,使用模糊查询必填项。
example.excludeNone();// 空的不做查询条件
example.excludeZeroes();// 0不要查询
example.ignoreCase(); // 不区分大小写
criteria.add(example);
}
if (row > 0 && page > 0) {
criteria.setMaxResults(row);// 最大显示记录数
criteria.setFirstResult((page - 1) * row);// 从第几条开始
}
// 判断是否有排序请求,如果有加入到排序方法中
if (orber != null) {
for (int i = 0; i < orber.length;="" i++)="" .../**="" *="" 使用对象的查询方法="" *="" *="" @param="" c:查询的类="" *="" @param="" obj:查询的对象="" *="" @param="" orber:按那个字段排序="" *="" @param="" row:每页多少条记录="" *="" @param="" page:第几页="" *="" @return="" list:="" 集合="" */="" public="" list="" find(class="" c,="" object="" obj,="" string[]="" orber,="" int="" row,="" int="" page)="" {="" list="" list="null;" try="" {="" criteria="" criteria="HibernateSessionFactory.getSession()" .createcriteria(c);="" if="" (obj="" !="null)" {="" example="" example="Example.create(obj);" example.enablelike(matchmode.anywhere);//="" 匹配模式,使用模糊查询必填项。="" example.excludenone();//="" 空的不做查询条件="" example.excludezeroes();//="" 0不要查询="" example.ignorecase();="" 不区分大小写="" criteria.add(example);="" }="" if="" (row=""> 0 && page > 0) {
criteria.setMaxResults(row);// 最大显示记录数
criteria.setFirstResult((page - 1) * row);// 从第几条开始
}
// 判断是否有排序请求,如果有加入到排序方法中
if (orber != null) {
for (int i = 0; i < orber.length;="" i++)="" criteria.addorder(order.desc(orber[i]));="" }="" list="criteria.list();" return="" list;="" }="" catch="" (hibernateexception="" e)="" {="" e.printstacktrace();="" return="" null;="" }="" finally="" {="" hibernatesessionfactory.closesession();="" }="" }="" *="" *="" 使用hql查询语句="" *="" *="" @param="" hql:hql语言="" *="" @param="" row:每页显示多少记录="" *="" @param="" page:显示第几页="" *="" @param="" params:="" *="" 所传的对应参数="" *="" @return="" list:="" 返回集合="" */="" public="" list="" find(string="" hql,="" int="" row,="" int="" page,="" arraylist="" params)="" {="" try="" {="" query="HibernateSessionFactory.getSession().createQuery(hql);" if="" (null="" !="params)" {//="" 循环把给参数赋值="" for="" (int="" i="0;" i="">< params.size();="" i++)="" {="" query.setparameter(i,="" params.get(i));="" }="" }="" if="" (row=""> 0 && page > 0) {
query.setMaxResults(row);// 每页显示多少条记录
query.setFirstResult((page - 1) * row);// 显示第几条记录
}
return query.list();
} catch (HibernateException e) {
e.printStackTrace();
return null;
} finally {
HibernateSessionFactory.closeSession();
}
}
criteria.addorder(order.asc(fieldname));
用户登录
还没有账号?立即注册
用户注册
投稿取消
文章分类: |
|
还能输入300字
上传中....