resultType:映射接口sql增、删、改、查方法的返回值类型。

    resultType: 执行 sql 得到 ResultSet 转换的类型,使用类型的完全限定名或别名。 注意如果返回的是集合,那应该设置为集合包含的类型,而不是集合本身。resultType 和 resultMap,不能同时使用。

    A、简单类型
    接口方法:

    1. int countStudent();

    mapper 文件:

    1. <select id="countStudent" resultType="int">
    2. select count(*) from student
    3. </select>

    测试方法:

    1. @Test
    2. public void testRetunInt(){
    3. int count = studentDao.countStudent();
    4. System.out.println("学生总人数:"+ count);
    5. }

    B、 对象类型
    接口方法:

    Student selectById(int id);
    

    mapper 文件:

    <select id="selectById" resultType="com.bjpowernode.domain.Student">
    
         select id,name,email,age from student where id=#{studentId}
    
    </select>
    

    框架的处理: 使用构造方法创建对象。调用 setXXX 给属性赋值。
    Student student = new Student();

    注意:Dao 接口方法返回是集合类型,需要指定集合中的类型,不是集合本身。
    image.jpeg

    C、 Map
    sql 的查询结果作为 Map 的 key 和 value。推荐使用Map。
    注意:Map 作为接口返回值,sql 语句的查询结果最多只能有一条记录。大于一条记录是错误。

    接口方法:

    Map<Object,Object> selectReturnMap(int id);
    

    mapper 文件:

    <select id="selectReturnMap" resultType="java.util.HashMap">
    
         select name,email from student where id = #{studentId}
    
    </select>
    

    测试方法:

    @Test
    public void testReturnMap(){
    
         Map<Object,Object> retMap = studentDao.selectReturnMap(1002);
    
         System.out.println("查询结果是 Map:"+retMap);
    
    }