MyBatis基础用法(一)

时间:2023-03-09 02:39:13
MyBatis基础用法(一)
    <select id="getErrorTimes" resultType="Integer">
SELECT ErrorTimes FROM `employee_sensitive` WHERE `EmpId`=#{empId};
</select>

上述XML中,ErrorTimes为Integer类型,在接口中我如下声明:

Integer getErrorTimes(@Param("empId") Integer empId);

大多数情况下是没有问题的,但是有一种情况会出问题,就是在empId会查出多个列时候,程序报错。

所以接口改为如下就正确:

List<Integer> getErrorTimes(@Param("empId") Integer empId);

这也说明了一个事情,ResultType支持一个值或者一个List将其封装。