如题:我用sql查数据,查出来后,在代码那里,死活来了个long类型不能注入到integer类型,我知道的办法都用了,哪位大神帮忙看下
这条sql,
select type, count(*) from lzh_member_discuz_logs group by type;
这是数据库结果
public DiscuzLogsDTO selectForfum() {
List<Map<Integer, Integer>> list = discuzLogsDAO.selectForumPosts();
DiscuzLogsDTO discuzLogsDTO = new DiscuzLogsDTO();
for (Map<Integer, Integer> map2 : list) {
if (map2.get("type") == 1) {
logger.info("sendPosts : {}",map2.get("count(*)"));
System.out.println("1");
《问题在这,debug走到这里,会报错,long不能注入到integer》
Integer integer = map2.get("count(*)").intValue();
System.out.println("2");
discuzLogsDTO.setSendPosts(integer);
System.out.println("3");
}
if (map2.get("type") == 2) {
logger.info("backPosts : {}",map2.get("count(*)"));
discuzLogsDTO.setBackPosts(map2.get("count(*)").intValue());
}
}
return discuzLogsDTO;
}
求助~~
把intValue()去掉也报错吗?看代码,你的map2 的key明明是Integer类型。但是为什么你map2取值get的时候怎么成了String型?
你代码里面map2的key和value的类型都是Integer,而调用了map2.get(字符串)方法,改为map2.get(0)或map2.get(1)
map2.get("count(*)").intValue() 的值应该是Long类型的
List> 改为 List> 试试,
Long integer = map2.get("count(*)").intValue();
(map2.get("type"))你这个map的key不是字符串吗? 你存储的时候不能使用 Map吗?
/**
* Returns the value of this {@code Long} as an {@code int} after
* a narrowing primitive conversion.
* @jls 5.1.3 Narrowing Primitive Conversions
*/
public int intValue() {
return (int)value;
}
//这是intValue()的源码,你直接使用强制转换类型试试,不行应该是其他问题不是这里错了,你应该把报错的记录截图发出来,这样才能看得更具体一点
问题解决了 , 谢谢大家 我是换了个玩法,感觉还是resultMap比resultType 的多...直接数据库跟对象映射...
Integer i = new Long(map2.get("count(*)")).intValue();
原始类型不能转换的