欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

多条件查询

程序员文章站 2022-03-08 09:59:08
...

public List findByPage(int offset, String model, String[] propertyName, String[] value, boolean rigor)
 {
  List valueList = new ArrayList();
  StringBuffer baseHql = new StringBuffer();
  String ralation = "like";
  if (rigor)
  {
   ralation = "=";
  }
  baseHql.append("select model.riPrices from" + model + "as model\n");
  List list = new ArrayList();
  boolean first = true;
  for (int i = 0; i < propertyName.length; i++)
  {
   if (value[i] != null)
   {
    if (first)
    {
     baseHql.append("where" + "model." + propertyName[i] + ralation + "?\n");
     list.add(value[i]);
     first = false;
    }
    else
    {
     baseHql.append(" and " + "model." + propertyName[i] + ralation + "?\n");
     list.add(value[i]);
    }
   }
  }
  try
  {
   Session session = this.getSession();
   Query query = session.createQuery(baseHql.toString());
   query.setFirstResult(offset);
   query.setMaxResults(PageModule.getPageSize());

   List listResource = query.list();
   Iterator it = listResource.iterator();
   while (it.hasNext())
   {
    Object[] obj = (Object[]) it.next();
    Float riPrices = (Float) obj[0];
    ResourceItem resourceItem = new ResourceItem();
    resourceItem.setRiPrices(riPrices);
    valueList.add(resourceItem);
   }

  }
  catch (Exception e)
  {
   log.error("分页查询所有资源出错", e);
  }
  return valueList;
 }