我正在尝试为 AJAX Web 项目编写数据访问层。此 DAL 必须将通过 AJAX servlet 传入的数据转换为可以传递给PreparedStatement 执行的对象。
AJAX servlet 中的数据通过使用 HttpServletRequest.getParameter(...) 检索,以字符串形式出现。
在每个数据类中,我都有一组已知的字段及其数据类型,例如 CustomerId(整数)、CustomerName(字符串)。
我当然可以在 Customer 类中编写一个方法来处理转换,但这意味着我必须为每个数据对象的类执行此操作。我更愿意有一个进行转换的通用方法,例如
Object convert(String value, Class<?> targetType) { ... }
任何人都可以指出我正确的方向吗?
I'm trying to write a data access layer for an AJAX web project. This DAL has to convert data coming in via an AJAX servlet to objects that can be passed to a PreparedStatement for execution.
Data in the AJAX servlet, retrieved by using HttpServletRequest.getParameter(...), come in as strings.
In each data class, I have a known set of fields as well as their data types, e.g. CustomerId(integer), CustomerName(string).
I can of course write a method in the Customer class to handle the conversion, but this means I have to do it for every data object's class. I would much rather have a generic method that does conversion, e.g.
Object convert(String value, Class<?> targetType) { ... }
Can anyone point me in the right direction?
发布评论
评论(4)
使用您想要使用的所有转换方法创建一个实用程序类。在其静态初始值设定项内,利用反射按映射中的参数类型和返回类型收集所有这些方法。然后,在
convert()
方法中,只需选择适合给定源和目标类型的方法并调用它。利用泛型将返回类型修复为与目标类型相同:您可以在 这篇文章。
但正如 bmargulies 指出的那样,JSON 也是一个有趣的选择。您可以让 ajax 将所有参数作为一个 JSON 字符串发送。然后,您可以使用 Google Gson 等 JSON 到 Javabean 转换器进行转换JSON 字符串到一个完整的 Javabean,如
Customer
。它会很简单:另请参阅此答案了解另一个示例。
Create an utility class with all conversion methods you would like to use. Inside its static initializer, make use of reflection to collect all those methods by parameter type and return type in a map. Then, in the
convert()
method just pick the method which suits the given source and target type and invoke it. Make use of generics to fix the return type to be the same as the target type:You can find an example in this article.
But as bmargulies pointed out, JSON is also an interesting option. You could let ajax to send all parameters as one JSON string. Then, you can use a JSON-to-Javabean converter like Google Gson to convert the JSON string to a fullworthy Javabean like
Customer
. It'll be as simple as:See also this answer for another example.
有一些 JSON 库可以进行数据类型转换。杰克逊就是其中之一。或者,您可以使用 JAX-RS 服务框架而不是原始 servlet 来编写整个想法,它会为您处理所有这些事情。 Apache CXF 是包含此支持的一个框架。既然您需要一种通用的解决方案,为什么不使用已经存在的解决方案呢?
There are JSON libraries that will do data type conversion. Jackson is one. Or, you could code the whole think using a JAX-RS service framework instead of a raw servlet, and it will take care of all this for you. Apache CXF is one framework that contains this support. Since you are asking for a generic solution, why not use one that's already out there.
我们在实用程序类中使用大量静态转换器来完成此操作。它并不优雅,但确实简单有效。
We do this exact thing using a plethora of static converters in a utility class. It isn't elegant but it sure is easy and effective.
既然您想使用PreparedStatement中的参数,为什么必须转换它们呢?
当使用
setString(index,parameter)
时,SQL将很乐意为您进行转换。因此,您可能想要做的唯一一件事就是对输入是否确实有效进行某种验证(或者您甚至可以将这部分留给您的 SQL 引擎,如果它不理解您的意思,它将抛出异常。
Since you want to use the parameters in your PreparedStatement, why do you have to convert them at all?
When using
setString(index, parameter)
SQL will be happy to do the conversion for you.Thus the only thing you might want to do is some kind of validation that the input is really valid (or you could even leave this part to your SQL engine which will throw an exception if it doesn't understand you.