工具类

This commit is contained in:
2025-11-17 12:15:07 +08:00
parent 3753075b10
commit 1cc7d53559

View 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→1false→0
return (Boolean) value ? 1 : 0;
} else {
// 其他不支持的类型,返回默认值
return 0;
}
}
}