如何处理
我有一个打印用户的用户界面,但我不想按国家/地区进行过滤。 我制作了一个经典的
在 JSP 中,我有
<select id="country" onchange="return filter();">
<option value="">...</option>
<c:forEach var="country" items="${countries}">
<option value="${country.id}"
${country.name}
</option>
</c:forEach>
</select>
一些用户没有国家/地区,所以我需要处理 2 个过滤器: - 打印所有用户信息,无过滤器 - 只打印没有国家/地区的用户
所以我想知道对 Java 说的最好方式是什么:“找到我所有用户”和“找到我所有没有国家/地区的用户”。
我有一些想法:如果countryId = 0,服务器将其翻译为“没有国家/地区的用户”,如果countryId = null,服务器将其翻译为“所有用户”。
最后,DAO 对象将进行这样的查询,
public List<User> findByCountry(Integer countryId){
query = "select * from users"
if(countryId==0){
query+= " where country_id is null"
}
else if(countryId==null){
query += " where country_id = " + countryId;
}
return query results...
}
这样可以吗,还是很难看,或者有人有更好的模式来做到这一点?
I have an user interface that print user, and I wan't to have a filter by country.
I made a classic <select />
element.
In JSP I have
<select id="country" onchange="return filter();">
<option value="">...</option>
<c:forEach var="country" items="${countries}">
<option value="${country.id}"
${country.name}
</option>
</c:forEach>
</select>
The thing is some users doesn't have a country, so I need to deal with the 2 filters :
- one that print all the user, no filter
- one that print only users that doesn't have a country
So I'm wondering what is the best way to say to Java : "find me all users", and "find me all users who doesn't have a country".
I have some idea : if the countryId = 0, the server translate it to "users who doesn't have a country, and if countryId = null, the server translate it to "all users".
At the end, the DAO object will make a query like
public List<User> findByCountry(Integer countryId){
query = "select * from users"
if(countryId==0){
query+= " where country_id is null"
}
else if(countryId==null){
query += " where country_id = " + countryId;
}
return query results...
}
So is this ok, or is this ugly, or someone have a better pattern to do this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我实际上会提出两个 DAO API:
在我看来,你的方法的问题在于,由于动态 SQL 代码,你的 API 并不明确。这会让你的同事更难理解你的代码。其次,这是两个不同的任务,虽然它们很相似,但最好提出两种明确的方法。这更容易进行单元测试,并且圈复杂度更低,因为方法中的控制流更少。此外,代码更容易理解,因为其他开发人员不需要想知道为什么你要针对 0 或 null 测试countryId,这不会向他们传达大量有意义的消息,除了它只是一个快速解决问题的方法之外你目前的问题。三周后,您会想知道为什么自己要针对这些奇怪的值进行测试。 :)
I would actually come up with two DAO APIs:
The problem with your approach, in my honest opinion, is your API is not explicit due to the dynamic SQL code. It makes it harder for your coworker to understand your code. Second, these are 2 different tasks, while they are similar, it is best to come up with two explicit methods. This is much easier to unit test and you have less cyclomatic complexity because you have less control flow in the method. Further, the code is easier to understand because other developers don't need to wonder why you are testing the countryId against 0 or null which doesn't convey whole lot of meaningful message to them, other than it is just a quick hack to solve your current problem. 3 weeks down the road, and you will be wondering why you are testing against these weird values yourself. :)
我认为您的方法是正确的,但您可能想让任何用户
WHERE CountryID=0 || CountryID IS NULL
使用“所有用户”标签,这样您就可以过滤以查看没有设置国家/地区的任何人。然后,您可以根据需要修复这些用户。但我认为总的来说你的解决方案很好。I think your approach is correct, but you might want to make any user
WHERE CountryID=0 || CountryID IS NULL
use the 'All Users' label, this way you can filter to see anyone without a country set. Then you can fix those users if desired/needed. But I think overall your solution is good.