【MyBatis】resultMap和resultType的区别

mybatis中resultMap和resultType的区别

mybatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap。resultType是直接表示返回类型的,而resultMap则是对外部ResultMap的引用,但是resultType跟resultMap不能同时存在。

public class User {

  private int id;

  private String username;

  private String hashedPassword;

//省略setter/getter方法

}

使用resultType
<select >

  select user_id, user_name, hashed_password

  from some_table

  where id = #{id}

</select>

不同
resultType对应的是java对象中的属性,大小写不敏感;resultMap对应的是对已经定义好了id的resultTupe的引用,key是查询语句的列名,value是查询的值,大小写敏感;
使用resultType的时候,要保证结果集的列名与java对象的属性相同,而resultMap则不用。
另外,resultMap 元素,它是 MyBatis 中最重要最强大的元素,它能提供级联查询,缓存等功能。