hibernate根据搜索条件查找相应的对象实体

http://www.javaeye.com/post/523791
有一个简单的需求:Product是一个实体类,用户在搜索页面上输入Product的属性值,系统根据这些属性值找到符合条件的Product并列举出来。搜索的代码如下:

Java代码 复制代码
  1. String getLikeCondition(String condition) {  
  2.     return "%" + condition + "%";  
  3. }  
  4.   
  5. @SuppressWarnings("unchecked")  
  6. public List<Product> getProducts(final Map<String, String> conditions) {  
  7.     final StringBuffer hql = new StringBuffer("from Product p where 1=1");  
  8.     for(String key : conditions.keySet()) {  
  9.         if (key.equalsIgnoreCase("name"))  
  10.             hql.append(" and p.name like :name");  
  11.         if (key.equalsIgnoreCase("category"))  
  12.             hql.append(" and p.category.name like :category");  
  13.         if (key.equalsIgnoreCase("priceLowerBound")) {  
  14.             int priceLowerBound = Integer.parseInt(conditions.get(key));  
  15.             if (priceLowerBound > 0)  
  16.                 hql.append(" and p.price > :priceLowerBound");  
  17.         }  
  18.         if (key.equalsIgnoreCase("priceUpperBound")) {  
  19.             int priceUpperBound = Integer.parseInt(conditions.get(key));  
  20.             if (priceUpperBound > 0)  
  21.                 hql.append(" and p.price <= :priceUpperBound");  
  22.         }  
  23.         //Other conditions omitted  
  24.     }  
  25.   
  26.     return (List<Product>) getHibernateTemplate().execute(new HibernateCallback() {  
  27.         public Object doInHibernate(Session session) throws HibernateException {  
  28.             Query query = session.createQuery(hql.toString());  
  29.                     for(String key : conditions.keySet()) {  
  30.                         if (key.equalsIgnoreCase("name"))  
  31.                             query.setParameter("name", getLikeCondition(conditions.get("name")));  
  32.                         if (key.equalsIgnoreCase("category"))  
  33.                             query.setParameter("category", getLikeCondition(conditions.get("category")));  
  34.                         if (key.equalsIgnoreCase("priceLowerBound")) {  
  35.                             int priceLowerBound = Integer.parseInt(conditions.get(key));  
  36.                             if (priceLowerBound > 0)  
  37.                                 query.setParameter("priceLowerBound", (float)priceLowerBound);  
  38.                         }  
  39.                         if (key.equalsIgnoreCase("priceUpperBound")) {  
  40.                             int priceUpperBound = Integer.parseInt(conditions.get(key));  
  41.                             if (priceUpperBound > 0)  
  42.                                 query.setParameter("priceUpperBound", (float)priceUpperBound);  
  43.                         }  
  44.                         //Other conditions omitted  
  45.                     }  
  46.             List<Product> items = (List<Product>)query.list();  
  47.             return items;  
  48.         }  
  49.     });  
  50.   
  51. }  
 String getLikeCondition(String condition) {    return "%" + condition + "%";   }     @SuppressWarnings("unchecked")   public List<Product> getProducts(final Map<String, String> conditions) {    final StringBuffer hql = new StringBuffer("from Product p where 1=1");    for(String key : conditions.keySet()) {     if (key.equalsIgnoreCase("name"))      hql.append(" and p.name like :name");     if (key.equalsIgnoreCase("category"))      hql.append(" and p.category.name like :category");     if (key.equalsIgnoreCase("priceLowerBound")) {      int priceLowerBound = Integer.parseInt(conditions.get(key));      if (priceLowerBound > 0)       hql.append(" and p.price > :priceLowerBound");     }     if (key.equalsIgnoreCase("priceUpperBound")) {      int priceUpperBound = Integer.parseInt(conditions.get(key));      if (priceUpperBound > 0)       hql.append(" and p.price <= :priceUpperBound");     }     //Other conditions omitted    }      return (List<Product>) getHibernateTemplate().execute(new HibernateCallback() {        public Object doInHibernate(Session session) throws HibernateException {            Query query = session.createQuery(hql.toString());        for(String key : conditions.keySet()) {         if (key.equalsIgnoreCase("name"))          query.setParameter("name", getLikeCondition(conditions.get("name")));         if (key.equalsIgnoreCase("category"))          query.setParameter("category", getLikeCondition(conditions.get("category")));         if (key.equalsIgnoreCase("priceLowerBound")) {          int priceLowerBound = Integer.parseInt(conditions.get(key));          if (priceLowerBound > 0)           query.setParameter("priceLowerBound", (float)priceLowerBound);         }         if (key.equalsIgnoreCase("priceUpperBound")) {          int priceUpperBound = Integer.parseInt(conditions.get(key));          if (priceUpperBound > 0)           query.setParameter("priceUpperBound", (float)priceUpperBound);         }         //Other conditions omitted        }            List<Product> items = (List<Product>)query.list();            return items;        }    });     }  


方法getProducts接受一个类型为Map<String, String>的参数conditions,该参数是一个<属性,属性值>列表,在方法中分析这个列表并组合成查询条件,最后执行查询并返回符合条件的Product列表

让人不爽的是代码中使用了很多if判断,而且使用了两次循环。是否有更好的办法来简化这段代码呢?或者有没有更好的方式来实现这个需求?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值