child spring对象之间的显式类型转换,它是超级java.util对象 [英] Explicit type conversion between child spring object, and it's super java.util object

查看:222
本文介绍了child spring对象之间的显式类型转换,它是超级java.util对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在春天,我使用jdbcTemplate,但有一个问题,它返回一个Linkedcaseinsensitivemap查询一个List时,当做以下我仍然得到spring linkedcaseinsensitivemap,即使我把它转换到java util List和定义左 - 作为java.util.List的赋值。

In spring I am using jdbcTemplate, but having a problem that it is returning a Linkedcaseinsensitivemap when querying for a List, when doing the following I still get the spring linkedcaseinsensitivemap, even if I cast it to java util List and define the left-side of the assignment as a java.util.List.

首先,这怎么可能?

final java.util.List<Map<String, Object>> list = (java.util.List<Map<String, Object>>) jdbc
          .queryForList("SELECT * FROM customer");

所以,一个人如何做这种类型的upcaste?
而不需要声明第二个列表为它分配内存,然后将对象手动放入java.util.List?

so, how would one achive doing this type of upcaste? without needing to declare a second list allocate memory for it and then put the objects manually into the java.util.List?

由于LinkedCaseInsensitive是java对象的子类化,Im很难找到如何转换为超级对象的java列表。如何实现这一点是目前的一个谜。

Since the LinkedCaseInsensitive is subclassing the java object, Im having a hard time figuring out how to cast to the super object which is the java List. How to achieve this is a mystery at the moment.

因为目前没有办法知道哪些代理将使用我们的AMQ,目标太严格保持jms对象,
所以我不能开始发送spring对象,因为jms应该是我们的标准,还请注意我没有选择实现AMQProtocol,我需要发送基本的java对象,

since there is no way currently to know which brokers will use our AMQ, the goal is too strictly keep to jms objects, So I can't start sending spring objects, since jms should be our standard, also please note I do not have the option to implement the AMQProtocol, I need to send basic java objects,

由于序列化到JSON已经建议,我会解释为什么它不工作在这种情况下,为什么我需要发送对象原样到接收器,因为他们会把它

Since serialising to JSON has been suggested I will explain why it does not work in this case, why I'll need to send the Objects "as-are" to the receiver since they will put it into a Notes document.

for (int i = 1; i <= metadata.getColumnCount(); i++) {
                String columnName = metadata.getColumnName(i);
                Object values = sqlConnection.getRset().getObject(i);
                doc.replaceItemValue(columnName, values);

}

美丽?

请提前感谢

So SO'ers, how does one achieve doing this more beautifully?
please help thanks in advance!

推荐答案

SQL select可以返回多个行,每行有多个选定列。
queryForList方法你调用返回一个列表,为每个选定的行一个Map映射列名到列值。

A SQL select can return multiple rows, and each row has multiple selected columns. The queryForList method you are calling returns a List with for each selected row a Map mapping column name to column value.

映射和List是接口,因此Spring可以自由选择任何实现。它为Map选择LinkedCaseInsensitiveHashMap,因为该映射将按插入顺序列出键。因此,选择列的顺序不会丢失。

Map and List are interfaces, so Spring is free to pick whatever implementation it likes. It chooses the LinkedCaseInsensitiveHashMap for the Map because that map will list the keys in the order of insertion. So the order in which the columns were selected does not get lost.

如果您希望将结果列表发送给您知道的接收器,您可能最好将其序列化为JSON并将其作为文本消息发送。

If you wish to send the result list to a receiver that you know little about, you can probably best serialize it to JSON and send it as a text message.

您可以使用 Gson Jackson2 。您创建一个序列化程序,并将其转换为要转换为字符串的对象。
例如在Gson中,序列化类被称为Gson:

You can serialize to JSON using a library like Gson or Jackson2. You create a serializer and feed it the object you wish to convert to a String. So for example in Gson, where the serializer class is called Gson:

TextMessage message;
// initialize message and headers however you like
// then serialize it to String:
Gson gson = new Gson();
String json = gson.toJson(list);
// and set it in the message:
message.setText(json);

(你也可以让Spring JmsTemplate为你使用一个MessageConverter转换为JSON, d

(You can also let Spring JmsTemplate do this for you using a MessageConverter that converts to JSON but I'd estimate that that's a bit harder to get working.)

或者,如果您希望自定义作为ObjectMessage发送的Map,可以使用不同的查询方法,允许您指定自定义 RowMapper 创建一个java.util.Map实现你的喜好。注意,如果你使用TreeMap,它会按字母顺序对列进行排序,如果你使用HashMap,它将按随机顺序排列。

Alternatively, if you wish to customize the Map that you send as an ObjectMessage, you can use a different query method that allows you to specify a custom RowMapper that creates a java.util.Map implementation of your liking. Note that if you use a TreeMap, it'll sort the columns alphabetically and if you use a HashMap, it'll put them in random order.

将JSON解压缩回Java对象。 gson.fromGson(json)将返回地图列表。

The receiver then can unpack the JSON back into Java objects. gson.fromGson(json) will return a List of Maps.

这篇关于child spring对象之间的显式类型转换,它是超级java.util对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆