java
技术随笔
Mybatis报错—— A query was run and no Result Maps were found for the Mapped Statement
报错信息
org.apache.ibatis.executor.ExecutorException:
A query was run and no Result Maps were found for the Mapped Statement
'com.example.demo.repository.UserMapper.findAll'.
It's likely that neither a Result Type nor a Result Map was specified.原因
在 MyBatis 的 <select> 标签中,必须指定返回值类型。如果漏写 resultType(或 resultMap),MyBatis 在执行查询后无法知道把结果集映射成什么对象,就会抛出该异常。
例如下面这段就缺少 resultType:
<select id="findAll" parameterType="User">
select * from user
</select>解决
补上 resultType:
<select id="findAll" parameterType="User" resultType="User">
select * from user
</select>补充:增删改可以不写 resultType
- 查询(select):
resultType不能省略。 - 增删改(insert/update/delete):没有 resultType,返回值直接在 Mapper 接口上定义,通常用
Integer(影响行数)、Long或Boolean(是否成功)。parameterType可省略。
<!-- parameterType 可省略 -->
<insert id="addEmp">
INSERT INTO tbl_employee VALUES (#{lastName}, #{gender}, #{email});
</insert>