http://www.javaeye.com/post/523791
有一个简单的需求:Product是一个实体类,用户在搜索页面上输入Product的属性值,系统根据这些属性值找到符合条件的Product并列举出来。搜索的代码如下:
方法getProducts接受一个类型为Map<String, String>的参数conditions,该参数是一个<属性,属性值>列表,在方法中分析这个列表并组合成查询条件,最后执行查询并返回符合条件的Product列表
让人不爽的是代码中使用了很多if判断,而且使用了两次循环。是否有更好的办法来简化这段代码呢?或者有没有更好的方式来实现这个需求?
有一个简单的需求:Product是一个实体类,用户在搜索页面上输入Product的属性值,系统根据这些属性值找到符合条件的Product并列举出来。搜索的代码如下:
- 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;
- }
- });
- }
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判断,而且使用了两次循环。是否有更好的办法来简化这段代码呢?或者有没有更好的方式来实现这个需求?