哪一个是以下问题的有效解决方案?
我有一个struts和hibernate项目,
从action类中,我需要调用DAO,从数据库中获取数据。
我需要将表单类中的所有字段传递给 DAO 中的方法。
该表单大约有 15 个字段,全部都是 String 类型。
哪个更好或更高效:将字段直接传递给 DAO,还是将整个 FORM 对象作为参数传递?
I have a struts and hibernate project,
From the action class, I need to call the DAO, to get the data from the database.
I need all the fields in the form class, to be passed to method in the DAO.
The form has about 15 fields, all are of type String.
Which is better or efficient: Pass the fields directly to the DAO, or the entire FORM object as a parameter?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
想象一个具有 15 个字符串参数的方法:
哦,看,一个水平滚动条。还有一个很小的按钮。嗯。这看起来很可疑。不要这样做。
如果参数超过 3 或 4 个,最好将参数收集到参数对象中。长参数列表会产生以下问题:
您不能省略任何参数。因此,如果您不需要它们中的任何一个,则必须传递大量
null
或其他内容。参数的顺序是固定的。如果您需要更改它或添加参数,您总是会遇到麻烦。如果您犯了错误(而且您一定会犯错误),则没有简单的方法可以找出错误所在。
如果您使用带有 setter 和 getter 的参数对象,则可以按任意顺序填写值。
参数没有名称。示例:
这是什么意思?看这里:
哦,这是一个电话号码。现在很明显了。
Imagine a method with 15 String parameters:
Oh look, a horizontal scrollbar. And a pretty small button. Hm. That looks dubious. Don't do it.
It's always better to collect parameters in a parameter object if you have more than 3 or 4 of them. Long parameter lists create the following problems:
You can't omit any paramater. So if you don't need any of them, you'll have to pass lots of
null
s or something.The order of parameters is fixed. If you need to change it, or add parameters, you're always in trouble. If you make a mistake (and you will), there is no easy way to find out where.
If you use a parameter object with setters and getters, you can fill in the values in any order.
Parameters have no name. Example:
what does that mean? Look here:
Oh, it's a phone number. Now it's obvious.
在方法或基于集合的数据结构中传递 15 个参数很容易出错且难以维护。
您可以简单地将 pojo(表单 bean)传递到 Dao 方法中,该方法将构造查询。作为奖励,hibernate 提供了 hibernate 查询示例 API 这可能100%适合你这个问题。
Passing 15 parameters in a method or in a Collection based datastructure is error prone and hard to maintain.
You could simply pass your pojo (Form bean) into a Dao method and which will construct the query. As a bonus hibernate offers the hibernate Query by Example API which may suit you 100% for this problem.