"@ResultType" 注解是搭配 "ResultHandler" 来用的。
«REF: "https://mybatis.org/mybatis-3/java-api.html#mapper-> annotations" (https://link.segmentfault.com/?enc=6L2as3ciyY7OiMOygNlqpQ%3D%3D.a5XBDyN66%2FH8dPNVUw1pJEoL3LuFqlKZiqgL31ecgchUkk21tcgo8FHmGmnfU%2F4iWYvO87J8P%2FLW3hrSUOx2ow%3D%3D)This annotation is used when using a result handler. In that case, the
return type is "void" so MyBatis must have a way to determine the type of
object to construct for each row. If there is an XML result map, use the
"@ResultMap" annotation. If the result type is specified in XML on the
"" element, then no other annotation is necessary. In other cases,
use this annotation. For example, if a "@Select" annotated method will use a
result handler, the return type must be "void" and this annotation (or
"@ResultMap") is required. This annotation is ignored unless the method
return type is "void".»
所以你那个写法不对,起码你得定义一个 ResultHandler 出来:
@Select("select * from Student")
@ResultType(Student.class)
void queryStudent(StudentResultHandler resultHandler);
public class StudentResultHandler implements ResultHandler {
private final List students;
public StudentResultHandler() {
students = new ArrayList();
}
@Override
public void handleResult(ResultContext context) {
Student student = (Student)context.getResultObject();
students.add(student);
}
}