Object转换成Map(支持驼峰,下划线)

更新时间:2023-05-21 12:31:35 阅读: 评论:0

Object转换成Map(⽀持驼峰,下划线)⼀.Object转换成Map,⽀持下划线转换成map
le.common.ba.CaFormat;
le.common.ba.Converter;
class Utils{
private static Converter<String, String> converter = CaFormat.verterTo(CaFormat.UPPER_UNDERSCORE); //Object转换为Map(实体对象转换成带下划线的map)
public static Map<String,Object> objToMap(Object obj) throws Exception{
Map<String,Object> map = new LinkedHashMap<String, Object>();
Field[] fields = Class().getDeclaredFields();
for(Field field : fields){
field.tAccessible(true);
map.Name()) , (obj));
}
return map;
}
}
⼆.驼峰与下划线⽅式名称互相转换⼯具类
public class CamelUnderlineUtil {
private static final char UNDERLINE ='_';
public static String camelToUnderline(String param) {
if (StringUtil.isEmpty(param)) {
return "";
}
StringBuilder sb = new StringBuilder();
int len = param.length();
for (int i = 0; i < len; i++) {
char c = param.charAt(i);
if (Character.isUpperCa(c)) {
sb.append(UNDERLINE);
sb.LowerCa(c));
} el {
sb.append(c);
}
}
String();
}
public static String underlineToCamel(String param){
if (StringUtil.isEmpty(param)) {
return "";
}
StringBuilder sb = new StringBuilder();
int len = param.length();
for (int i = 0; i < len; i++) {
char c = param.charAt(i);
if (c==UNDERLINE) {
katy perry 格莱美if(++i<len){
sb.UpperCa(param.charAt(i)));
}
} el {
sb.append(c);
}
}
String();
}
}
三.Bean对象转Map⽅法
private Map<String, String> convertObjectToMap(Object obj, Class clazz) throws Exception {
Map<String, String> dstMap = new HashMap<String, String>();
Field[] fields = Class().getDeclaredFields();
for (Field field : fields) {
if ("rialVersionUID".Name())) {
continue;
1653
}高中英语名词性从句
String dstName = CamelUnderlineUtil.Name());
PropertyDescriptor pd;
pd = new Name(), clazz);
Method method = pd.getReadMethod();
crueltyObject dstObject = method.invoke(obj);
if (dstObject instanceof Date) {
dstObject = convertToTimeStamp16((Date) dstObject);
}
faultlessif (dstObject instanceof ArrayList) {
dstObject = "";wavinflag
}
dstMap.put(dstName, StringUtil.isEmpty(dstObject) ? "" : String());        }
return dstMap;
}
四.map转bean,bean转map(不带下划线转换)
  1.实体对象转换成Map
/**
* 实体对象转成Map
*
* @param obj 实体对象
* @return
*/
public static Map<String, Object> object2Map(Object obj) {
Map<String, Object> map = new HashMap<>();
if (obj == null) {
mistake的用法return map;
}
Class clazz = Class();
Field[] fields = DeclaredFields();
try {
for (Field field : fields) {
field.tAccessible(true);
map.Name(), (obj));
}
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
  2.Map转换成实体对象
/**
* Map转成实体对象
*
* @param map  实体对象包含属性
* @param clazz 实体对象类型
* @return
*/
public static Object map2Object(Map<String, Object> map, Class<?> clazz) {
if (map == null) {
return null;
}
imbd
Object obj = null;
try {
obj = wInstance();
first name 什么意思Field[] fields = Class().getDeclaredFields();
for (Field field : fields) {
int mod = Modifiers();
if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
continue;
mingjian}
field.tAccessible(true);
field.t(obj, (Name()));
}
} catch (Exception e) {
e.printStackTrace();
}
return obj;
}

本文发布于:2023-05-21 12:31:35,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/117056.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:实体   对象   转成
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图