Mybatis报错—— A query was run and no Result Maps were found for the Mapped Statement 。。。 It’s likely that neither a Result Type nor a Result Map was specified.
报错如下
org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.apache.ibatis.executor.ExecutorException: A query was run and no Result Maps were found for the Mapped Statement 'com.mapper.EmployeeMapper.getEmpByIdAndLastname'. It's likely that neither a Result Type nor a Result Map was specified.
### The error may exist in com/mapper/EmployeeMapper.xml
### The error may involve com.mapper.EmployeeMapper.getEmpByIdAndLastname
### The error occurred while handling results
### SQL: select * from tbl_employee where id = ? and last_name=?
### Cause: org.apache.ibatis.executor.ExecutorException: A query was run and no Result Maps were found for the Mapped Statement 'com.mapper.EmployeeMapper.getEmpByIdAndLastname'. It's likely that neither a Result Type nor a Result Map was specified.
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)
... 29 more
异常分析:
关键在第一段提示: It’s likely that neither a Result Type nor a Result Map was specified.意思是无法确定返回值类型。
如下代码:
<select id="getEmpById" >
select * from tbl_employee where id = #{id}
</select>
在写select查询标签是,没加resultType属性。
异常解决,代码如下:
<select id="getEmpById" resultType="com.bean.Employee" >
select * from tbl_employee where id = #{id}
</select>
小结:
在写select查询标签进行查询时,必须写resultType属性,不然mybatis无法确定返回值类型,就会报错。
补充:
1.进行查询时,resultType(返回值类型)属性不能省略。
2.进行增删改时,parameterType(参数类型)属性可以省略,且它们没有resultType属性,返回值可以直接在Mapper接口上定义。
mybatis允许增删改查直接定义以下类型返回值(Mapper接口中定义)
Integer(返回SQL语句影响的行数)、
Long(同上)
Boolean(返回SQL语句是否执行成功)
<!-- parameterType:参数类型,可以省略 -->
<insert id="addEmp" parameterType="com.bean.Employee">
INSERT INTO tbl_employee
VALUES (#{lastName}, #{gender},#{email});
</insert>