2

在以下情况下,我需要您的一些意见来决定正确的方法。

我目前正在开发一个电子商务应用程序(一个在线购物网站)。在这里,在应用程序的主页上,我必须显示商店中可用的产品列表。

最终用户可以对产品列表应用过滤器,以便只显示满足应用过滤器的产品。(过滤器如显示产品与选定的公司、价格范围等)。

所以,我的问题是关于应用过滤器的逻辑。我有以下想法来实现这一要求。

  1. 当用户在页面上选择不同的条件时,动态生成 SQL 查询(通过附加带有所需 where 子句的字符串)并在每个请求上触发生成的 SQL。

  2. 已经在数据库中设置了所有可能的 SQL 组合(比如在存储过程中)。当用户更改条件时,在存储过程中选择适当的 SQL 来运行。

谁能指导我,如果有更好的方法来处理这个要求?任何输入都受到高度重视。如果在这种情况下需要更多信息,请告诉我。

谢谢。

4

2 回答 2

0

Something that works well for sites like yours is to not change the query to retrieve different items to offer for sale but, instead, to offer the all the same items but to change the order they appear on the site.

So, if the user wants items priced $100-$500, you still show the items under $100 and over $500 but the ones in that range appear first, then the ones outside the range in some sort of random order. Or, if there are multiple criteria, you may show items meeting all but one of the criteria.

This also solves the problem of the user asking for coffee mugs over $500. If there are no matches, you don't give the user 0 items to select from but you give them some of the other items with preference to coffee mugs of any price. Presenting the user with no items to purchase is a lost chance that they might see something they want.

You can implement this in either of the ways you mentioned except you are varying the "order by" clause.

My personal preference is to NOT use stored procedures.

But, if you are working with a large group with experts in database use in the group that can optimize the database usage and keep the stored procedures in top shape, that might be an option.

If its a single person or small team of java developers, don't expect them to be experts in Java and SQL/Database as well. Play to your teams strengths.

于 2013-06-18T17:56:13.340 回答
0

我会做第一个,但你必须警惕 SQL 注入攻击。

我不喜欢您的第二个选项,因为存储过程通常是用我认为比 java 更糟糕的语言编写的。这是纯粹的观点,但这是我的观点。它们也不是数据库不可知论者。

或者,您可以为此使用 JPA。例如,有Hibernate Filters

于 2013-06-18T17:43:59.797 回答