一、bug描述
Map<Integer, Map<String, Object>> contactMaps = null;
List<Map<String, Object>> persons = customCrmMapper.findPersonByIds(idList);
contactMaps = persons.stream().collect(Collectors.toMap(e -> (Integer) e.get("id"), e -> e));
<select id="findPersonByIds" resultType="map">
SELECT
id, phone, weixin, email
FROM
person
</select>
person表中id字段为int类型,在用mybatis查询后用Map接收,之后再用map.get(“id”)获取后强转成Integer类型报如下错误。
java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
二、bug原因
mybatis偶尔会抽风,数据库里int类型的字段会时不时的被转成long或者int。
具体原因可参考这篇文章
三、解决办法
1、在Mapper.xml文件里显示的指定javaType=“xxx”。
2、先转为string类型,然后再解析为Integer或者Long类型。
Integer type = Integer.parseInt(map.get("type").toString());
四、参考资料
1、https://blog.csdn.net/ahwsk/article/details/81975117
2、https://blog.csdn.net/f641385712/article/details/81713846