java.lang.ClassCastException: class java.util.HashMap cannot be cast to class(Mybatis传入Map遍历出错)

Mybatis传入Map遍历出错

<select id="pagesSelectUser" resultType="map">
    select  id,username,password,role,registerTime from `user` limit #{startIndex},#{pageSize};
</select>
  SqlSession sqlSession = MybatisUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("startIndex",0);
        map.put("pageSize",5);
        List<User> users = mapper.pagesSelectUser(map);
            for (User user:users){
                logger.info(user);
            }
        sqlSession.close();
java.lang.ClassCastException: class java.util.HashMap cannot be cast to class com.tx.pojo.User (java.util.HashMap is in module java.base of loader 'bootstrap'; com.tx.pojo.User is in unnamed module of loader 'app')

	at com.tx.dao.TestUserMapper.pagesSelect(TestUserMapper.java:73)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)

原因:Mybatis中,查询中使用Map时resultType必须写返回的集合类型,而不是写map。
解决方式:修改resultType的值为返回的集合类型。
其他:在增删改中使用Map时,paramterType则需要写map类型。

发布了61 篇原创文章 · 获赞 0 · 访问量 2161

猜你喜欢

转载自blog.csdn.net/sabstarb/article/details/105146408
今日推荐