0
点赞
收藏
分享

微信扫一扫

问题记录-A query was run and no Result Maps were found for the Mapped Statement..

幸福的无所谓 2022-01-31 阅读 26
java

A query was run and no Result Maps were found for the Mapped Statement…

1.报错信息

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.executor.ExecutorException: A query was run and no Result Maps were found for the Mapped Statement 'xxx'.  It's likely that neither a Result Type nor a Result Map was specified.

2.问题分析
由报错信息可知: 很可能既没有指定结果类型, 也没有指定结果映射。
检查mapper.xml文件后, 发现遗漏了里面一个属性resultType=" " 或者 resultMap=" "

<!--报错的配置-->
<select id="selectDiscussPosts">
    select <include refid="selectFields"></include>
    from discuss_post
    where status != 2
    <if test="userId!=0">
        and user_id = #{userId}
    </if>
    order by type desc, create_time desc
    limit #{offset}, #{limit}
</select>

MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的,而resultMap则是对外部ResultMap的引用,但是resultType跟resultMap不能同时存在.
其中,如果返回类型是java自带类型,可以自动检测到,那么就可以省略不写这个属性.(个人感觉最好都写上,不容易犯错).
3.解决方法
在< select >标签上加上属性resultType=" "即可

<select id="selectDiscussPosts" resultType="DiscussPost">
...
</select>

在resultType=“DiscussPost”中,DiscussPost就是自定义的POJO,此时可以使用全限定类名 (全限定类名:就是类名全称,带包路径的用点隔开,例如: java.lang.String) 来指定这些POJO的引用.
但这里resultType没写全是因为在配置文件使用mybatis.type-aliases-package来指定POJO扫描包来让mybatis自动扫描到自定义的POJO。

mybatis.type-aliases-package=com.example.demo.entity
举报

相关推荐

0 条评论