我处于反模式中,我想摆脱它
我正在开发一个java webapp,使用jsp/jquery/ejb/jboss。
我有一个 Web 表单,使用户能够从数据库中选择 100 个字段(全部来自不同的不相关表/对象)的任意组合。然后,这些字段通过 java servlet 输出到 Excel 电子表格。执行的存储过程始终返回所有 100 个字段。
Web 表单在传输对象 (TO) 中设置 100 个布尔值,以确定是否应显示数据。然后引用该 TO 来生成电子表格的标题行以及数据库中迭代的每一行。
一切都很好,但感觉不对。我想不出一种不引用 100 个布尔值(N+1 次)来确定某个字段是否应包含在输出电子表格中的可行方法。例如,当我说可行时,我的意思是我不想重写存储过程或创建 100 个不同的存储过程。
I am developing a java webapp, using jsp/jquery/ejb/jboss.
I have a web-form that enables the user to select any combination of 100 fields (all from different unrelated tables/objects) from the database. These fields are then output, via a java servlet, to an excel spreadsheet. A stored procedure is executed that always returns all 100 fields.
The web-form sets 100 boolean values in a transfer object(TO) to determine whether data should be then be displayed. This TO is then referenced to produce the title row of the spreadsheet and also for each row from database which is iterated over.
It all works fine, however it feels wrong. I cannot think of a viable way which does not reference 100 booleans (N+1 times) to determine whether a field should be included in the outputted spreadsheet. When I say viable I mean, for example, that I don't want to rewrite stored procedure or create 100 different stored procedures.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我们的解决方案是在类似的情况下创建动态传输对象。基本上,它是一个
Map
,而不是具有多个 getter 和 setter 的 POJO。填充和读取该传输对象的代码是简单的迭代。
Our solution was in similar situations to create a dynamic Transfer Object. Basically, it was a
Map
instead of a POJO having a number of getters and setters.The codes which fills and reads this transfer object were simple iterations.
您不能在应用程序中动态构建选择 SQL 字符串,然后执行该 SQL 语句,而不是使用存储过程来实现此目的。因此,您只需要引用布尔值一次,并且只返回您需要的列。
Instead of using a stored procedure for this couldn't you build your select SQL string dynamically in your application and then execute that SQL statement. So you only need to reference you booleans once and you only return the columns you need.
您可以通过以下方式消除手动字段枚举:
在表单加载时,对于每个可用的电子表格字段,输出一个布尔控件。可能最好在前面添加某种前缀,这样就不会与表单上存在的任何其他字段发生冲突。
在表单提交时,您将显示任何包含的具有前缀的字段。
You could eliminate the manual field enumeration by:
on form load, for each of your available spreadsheet fields, you output a boolean control. Probably best to prepend some sort of prefix so you don't have conflicts with any other fields that exist on the form.
on form submission, you display any included fields that have the prefix.
您必须评估这是否真的更好,但是您可以使用一种方法,使用位数组来存储是否要使用字段。每个字段都有一个对应于单个位的值:
每个表单字段在选择时将具有其位值的值,或者在未选择时为 0。提交表单时,对字段求和并提交值。因此,如果选中“FIELD1”和“FIELD3”,您将提交值 5(二进制为 00101)。
然后,您为每个字段应用一个简单的位掩码来确定选择了哪些字段(有比逐个字段更好的方法吗?):
缺点:对于 100 个字段,您正在谈论一个非常大的数字!您可能必须使用 2 位数组。我也不相信这真的简化了你的问题,但也许确实如此。
You'll have to evaluate whether this is really better, but you could use an approach where you use a bit array to store whether or not the fields are to be used. Each field would have a value corresponding to a single bit:
Each form field would have a value of its bit value when selected, or 0 when not selected. On form submission, total up the fields and submit the value. So if FIELD1 and FIELD3 were checked, you'd submit a value of 5 (00101 in binary).
Then you apply a simple bitmask for each field to determine which ones were selected (is there a better way than field-by-field?):
Disadvantages: with 100 fields, you're talking about a really big number! You might have to use 2 bit arrays. I'm also not convinced that this really simplifies your problem, but maybe it does.