看情况会有用,虽然我觉得转Json更快……
@SuppressWarnings("unchecked")
public static Map<String, Object> toMap(Object obj) {
Map<String, Object> map = new HashMap<String, Object>();
if (obj == null) {
return map;
}else if(obj instanceof Map){
return (Map<String, Object>)obj;
}
BeanMap beanMap = new BeanMap(obj);
Iterator<String> it = beanMap.keyIterator();
while (it.hasNext()) {
String name = it.next();
Object value = beanMap.get(name);
// 转换时会将类名也转换成属性,此处去掉
if (value != null && !name.equals("class")) {
map.put(name, value);
}
}
return map;
}
//在方法中进行调用
public void Test(Entity entity,……) {
……
Map<String, Object> map = BeanUtils.toMap(query);//方法放在BeanUtils里面
……
}