From 1cc7d5355982c119b301b506c0fe5b2a36bcdfd7 Mon Sep 17 00:00:00 2001 From: mashili Date: Mon, 17 Nov 2025 12:15:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/xzzn/common/utils/MapUtils.java | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 ems-common/src/main/java/com/xzzn/common/utils/MapUtils.java diff --git a/ems-common/src/main/java/com/xzzn/common/utils/MapUtils.java b/ems-common/src/main/java/com/xzzn/common/utils/MapUtils.java new file mode 100644 index 0000000..cb1fc03 --- /dev/null +++ b/ems-common/src/main/java/com/xzzn/common/utils/MapUtils.java @@ -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 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; + } + } +} \ No newline at end of file