我遇见的问题
我实在是太懒了,不想每次都写实体类,直接用map接了(个人不建议这样,该写还是要写的)
问题来了,map里的key是带下划线的,不是驼峰,前端不干了。Integer、Double、Float、Long等精度也会有问题。时间也会变成时间戳Timestamp,为什么这样自己去查,本萌新不知道。
我的解决方式
写了个工具类MapUtil,里面有很多方法就不全粘过来了
/**
* 格式化返回map
*
* @param map
* @return
* @throws Exception
*/
public static Map<String, String> format(Map<String, ?> map) throws Exception {
Map<String, String> resMap = new HashMap<>();
for (Map.Entry<String, ?> entry :
map.entrySet()) {
//key下划线转驼峰
String key = entry.getKey().toLowerCase();
Matcher matcher = linePattern.matcher(key);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
}
matcher.appendTail(sb);
key = sb.toString();
Object value = entry.getValue();
//将时间戳转换为yyyy-MM-dd HH:mm:ss.SSS
if (value instanceof Timestamp) {
Date date = (Date) value;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
resMap.put(key,
sdf.format(date));
continue;
}
//将Integer Double Float Long
// BigDecimal的科学计数法变为非科学计数法
if (value instanceof Double || value instanceof Integer || value instanceof Float
|| value instanceof Long || value instanceof BigDecimal) {
BigDecimal decimal =
new BigDecimal(value.toString());
resMap.put(key,
decimal.toPlainString());
continue;
}
resMap.put(key, value.toString());
}
return resMap;
}
直接在逻辑层调用就好了