工具类
This commit is contained in:
44
ems-common/src/main/java/com/xzzn/common/utils/MapUtils.java
Normal file
44
ems-common/src/main/java/com/xzzn/common/utils/MapUtils.java
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
package com.xzzn.common.utils;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class MapUtils {
|
||||||
|
/**
|
||||||
|
* 从Map中获取值并转为Integer(适配tinyint字段)
|
||||||
|
* @param map 数据源Map
|
||||||
|
* @param key 字段名
|
||||||
|
* @return 转换后的Integer,默认返回0(可根据业务调整默认值)
|
||||||
|
*/
|
||||||
|
public static Integer getInteger(Map<String, Object> map, String key) {
|
||||||
|
// 1. 处理Map为null或key不存在的情况
|
||||||
|
if (map == null || !map.containsKey(key)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
// 2. 获取原始值
|
||||||
|
Object value = map.get(key);
|
||||||
|
if (value == null) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
// 3. 转换为Integer(处理常见类型)
|
||||||
|
if (value instanceof Integer) {
|
||||||
|
return (Integer) value;
|
||||||
|
} else if (value instanceof Long) {
|
||||||
|
// 若Map中存的是Long(如JSON解析时数字默认转为Long)
|
||||||
|
return ((Long) value).intValue();
|
||||||
|
} else if (value instanceof String) {
|
||||||
|
// 若值是字符串类型(如"1")
|
||||||
|
try {
|
||||||
|
return Integer.parseInt((String) value);
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
// 字符串无法转数字,返回默认值
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
} else if (value instanceof Boolean) {
|
||||||
|
// 特殊情况:布尔值转整数(true→1,false→0)
|
||||||
|
return (Boolean) value ? 1 : 0;
|
||||||
|
} else {
|
||||||
|
// 其他不支持的类型,返回默认值
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user