用户登录
用户注册

分享至

hibernatelist排序

  • 作者: 墙-煎-饭
  • 来源: 51数据库
  • 2020-10-11

/**

  * 使用对象的查询方法

  * 

  * @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();

  }

 }


软件
前端设计
程序设计
Java相关