public void in(String key, Object... value) { if(null==inMap){ inMap=new HashMap<String, List<Object>>(5); } List<Object> list=new ArrayList<Object>(); if(value!=null&&value.length>0){ for(int i=0;i<value.length;i++){ if(value[i] instanceof List) { list.addAll((List<Object>)value[i]); } else { list.add(value[i]); } } } inMap.put(key, list); }
时间: 2024-02-14 09:35:38 浏览: 60
这是一个Java代码段,该代码实现了一个in查询操作的方法。方法的输入参数包括一个字符串类型的key和一个可变长度的Object类型的value数组。
该方法中,首先判断inMap是否为空,如果是则创建一个HashMap类型的inMap,用于存储查询条件。然后,将传入的value数组中的元素逐一添加到一个ArrayList类型的list中,如果value数组中的元素是List类型的,则将List中的元素全部添加到list中。最后,将key和list作为键值对存储到inMap中。
该方法的作用是将多个值作为in查询的条件进行查询。
相关问题
在Java中用Dom4j把这一段xml内容转成Map对象:<?xml version="1.0" encoding="UTF-8"?> <java version="1.6.0_26" class="java.beans.XMLDecoder"> <object class="java.util.HashMap"> <void method="put"> <string>att_20160719180419281NEk9</string> <string>cx</string> </void> <void method="put"> <string>att_20160511094248589ZKYq</string> <string>cnmm</string> </void> <void method="put"> <string>att_20161018153614188aTus</string> <string>zsx;fj</string> </void> <void method="put"> <string>att_20160422110906494XuEf</string> <null/> </void> <void method="put"> <string>att_20160511130210612UULE</string> <string>fjyxapp;jjgjapp;wx;jd;dxjg;tb;wsjy</string> </void> <void method="put"> <string>att_20160719180542840LnFm</string> <string></string> </void> <void method="put"> <string>att_20160511130025203ssle</string> <string>zdgsszta</string> </void> <void method="put"> <string>att_20160810131359346N0ra</string> <string>szjys</string> </void> <void method="put"> <string>att_20160511130031927equg</string> <string></string> </void> <void method="put"> <string>att_20160422110456036qp7Y</string> <null/> </void> <void method="put"> <string>att_20160422110543585f7iI</string> <string>cpjbxx</string> </void> </object> </java>
您可以使用以下代码将该XML转换为Map对象:
```java
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class XmlToMap {
public static void main(String[] args) throws Exception {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <java version=\"1.6.0_26\" class=\"java.beans.XMLDecoder\"> <object class=\"java.util.HashMap\"> <void method=\"put\"> <string>att_20160719180419281NEk9</string> <string>cx</string> </void> <void method=\"put\"> <string>att_20160511094248589ZKYq</string> <string>cnmm</string> </void> <void method=\"put\"> <string>att_20161018153614188aTus</string> <string>zsx;fj</string> </void> <void method=\"put\"> <string>att_20160422110906494XuEf</string> <null/> </void> <void method=\"put\"> <string>att_20160511130210612UULE</string> <string>fjyxapp;jjgjapp;wx;jd;dxjg;tb;wsjy</string> </void> <void method=\"put\"> <string>att_20160719180542840LnFm</string> <string></string> </void> <void method=\"put\"> <string>att_20160511130025203ssle</string> <string>zdgsszta</string> </void> <void method=\"put\"> <string>att_20160810131359346N0ra</string> <string>szjys</string> </void> <void method=\"put\"> <string>att_20160511130031927equg</string> <string></string> </void> <void method=\"put\"> <string>att_20160422110456036qp7Y</string> <null/> </void> <void method=\"put\"> <string>att_20160422110543585f7iI</string> <string>cpjbxx</string> </void> </object> </java>";
Map<String, String> map = xmlToMap(xml);
System.out.println(map);
}
public static Map<String, String> xmlToMap(String xml) throws Exception {
Map<String, String> map = new HashMap<>();
SAXReader reader = new SAXReader();
Document document = reader.read(new StringReader(xml));
Element root = document.getRootElement().element("object");
for (Element element : root.elements("void")) {
Element keyElement = element.element("string");
Element valueElement = element.elements().get(1);
String key = keyElement.getText();
String value = valueElement == null ? null : valueElement.getText();
map.put(key, value);
}
return map;
}
}
```
该代码使用Dom4j库解析XML,并将其转换为Map对象。输出结果为:
```
{att_20160719180419281NEk9=cx, att_20160511094248589ZKYq=cnmm, att_20161018153614188aTus=zsx;fj, att_20160422110906494XuEf=null, att_20160511130210612UULE=fjyxapp;jjgjapp;wx;jd;dxjg;tb;wsjy, att_20160719180542840LnFm=, att_20160511130025203ssle=zdgsszta, att_20160810131359346N0ra=szjys, att_20160511130031927equg=, att_20160422110456036qp7Y=null, att_20160422110543585f7iI=cpjbxx}
```
public void renderJsonpObj(Object json){ String callbackFunctionName = request.getParameter("callback"); try { response.setContentType("text/html;charset=UTF-8"); response.getWriter().print(callbackFunctionName+"("+gson.toJson(json)+")"); response.getWriter().flush(); response.getWriter().close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public String encodeGet(String str){ if(str!=null){ try { str = new String(str.getBytes("iso8859-1"),"utf-8"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return str; } public String getRequestEncode(String key){ String str = request.getParameter(key); return encodeGet(str); } public Object getByRequest(Object model){ //Object model = c.getInterfaces(); Class c = model.getClass(); Map<String,String> map = new HashMap(); Map<String,String[]> parammap = request.getParameterMap(); for(Map.Entry<String, String[]> entry:parammap.entrySet()){ String[] value = entry.getValue(); if(value.length==1){ map.put(entry.getKey(),value[0]); } } Field[] fields = c.getDeclaredFields(); try { for(int i=0;i<fields.length;i++){ Field field = fields[i]; field.setAccessible(true); String fname = field.getName(); String mvalue = map.get(fname); if(mvalue != null && !mvalue.equals("") && !mvalue.equals("null")){ String ftype = field.getType().getSimpleName(); if(ftype.equals("String")){ field.set(model, mvalue); }else if(ftype.equals("Integer") || ftype.equals("int")){ field.set(model, Integer.parseInt(mvalue)); }else if(ftype.equals("Double")){ field.set(model, Double.parseDouble(mvalue)); }else if(ftype.equals("Timestamp")){ Timestamp timestamp = Timestamp.valueOf(mvalue); field.set(model,timestamp); } } } }catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } return model; }
这是一段 Java 代码,主要实现了以下几个功能:
1. renderJsonpObj 函数,用于将一个对象以 JSONP 格式输出到响应中,其中 callback 参数指定了回调函数的名称。
2. encodeGet 函数,将 ISO8859-1 编码的字符串转换为 UTF-8 编码的字符串。
3. getRequestEncode 函数,获取请求中指定参数的值,并将其使用 encodeGet 函数进行解码后返回。
4. getByRequest 函数,将请求参数映射到指定的 Java 对象中,支持 String、Integer、Double、Timestamp 类型的参数。
阅读全文