resultMap解释:java
通常写mybatis时,为以下格式sql
<select id="selectUsers" resultType="com.someapp.model.User"> select id, username, hashedPassword from some_table where id = #{id} </select>
以上格式要求咱们数据表和java类中的属性名一致。 若是不一致,咱们也能够用sql中的别名来进行匹配,以下缓存
<typeAlias type="com.someapp.model.User" alias="User"/> //使用类型别名,下面就再也不须要输入类的彻底限定名称了。 <select id="selectUsers" resultType="User"> select user_id as "id", user_name as "userName", hashed_password as "hashedPassword" from some_table where id = #{id} </select>
resultMap就是来实现数据表和java类中属性的匹配的问题的,上面咱们没有直接调用它,但其实也是mybatis隐性的调用了resultMap才实现了映射。 下面咱们显式的使用一下result,来看一下对映射处理过程。ps:这也是解决名称不匹配的一种办法mybatis
<resultMap id="userResultMap" type="User"> <id property="id" column="user_id" /> <result property="username" column="user_name"/> <result property="password" column="hashed_password"/> </resultMap> <select id="selectUsers" resultMap="userResultMap"> select user_id, user_name, hashed_password from some_table where id = #{id} </select>
resultMap中的id和result基本相同,惟一不一样是id 表示的结果将是对象的标识属性,这会在比较对象实例时用到。 这样能够提升总体的性能,尤为是缓存和嵌套结果映射(也就是联合映射)的时候。app