我处于反模式中,我想摆脱它

发布于 2024-09-14 02:05:13 字数 369 浏览 7 评论 0原文

我正在开发一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你的他你的她 2024-09-21 02:05:13

我们的解决方案是在类似的情况下创建动态传输对象。基本上,它是一个 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.

若水般的淡然安静女子 2024-09-21 02:05:13

您不能在应用程序中动态构建选择 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.

誰ツ都不明白 2024-09-21 02:05:13

您可以通过以下方式消除手动字段枚举:

  • 在表单加载时,对于每个可用的电子表格字段,输出一个布尔控件。可能最好在前面添加某种前缀,这样就不会与表单上存在的任何其他字段发生冲突。

  • 在表单提交时,您将显示任何包含的具有前缀的字段。

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.

郁金香雨 2024-09-21 02:05:13

您必须评估这是否真的更好,但是您可以使用一种方法,使用位数组来存储是否要使用字段。每个字段都有一个对应于单个位的值:

static final FIELD1 = 1;
static final FIELD2 = 2;
static final FIELD3 = 4;
static final FIELD4 = 8;
static final FIELD5 = 16;
etc

每个表单字段在选择时将具有其位值的值,或者在未选择时为 0。提交表单时,对字段求和并提交值。因此,如果选中“FIELD1”和“FIELD3”,您将提交值 5(二进制为 00101)。

然后,您为每个字段应用一个简单的位掩码来确定选择了哪些字段(有比逐个字段更好的方法吗?):

boolean field1Selected = sum & FIELD1;
boolean field2Selected = sum & FIELD2;
etc

缺点:对于 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:

static final FIELD1 = 1;
static final FIELD2 = 2;
static final FIELD3 = 4;
static final FIELD4 = 8;
static final FIELD5 = 16;
etc

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?):

boolean field1Selected = sum & FIELD1;
boolean field2Selected = sum & FIELD2;
etc

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文