首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

MyBatis resultType 與 resultMap 深度解析:從自動映射到複雜關聯查詢

     編輯:Java 相關知識

核心區別

特性resultTyperesultMap
本質別名/全限定類名,MyBatis 自動建構 ResultMap顯式定義的 <resultMap>,完全控制映射
適用場景欄位名與屬性名一致(或別名對應)的簡單查詢欄位名不一致、多表關聯、嵌套結果、緩存優化
靈活度低(約定優於配置)高(配置優於約定)
效能首次查詢需反射構建映射,後續緩存啟動時解析,運行零反射開銷

resultType 自動映射機制

<select id="selectBlog" resultType="Blog">
  SELECT id, title, content, owner FROM t_blog WHERE id = #{id}
</select>

MyBatis 內部:

  1. 執行查詢獲取 ResultSet
  2. 對每行構建 Map<String, Object>(列名→值)
  3. 反射實例化 Blog,按 Property 名匹配 Map Key 賦值
  4. 若列名含下劃線 create_time,需開啟 mapUnderscoreToCamelCase=true 自動轉駝峰

resultMap 顯式映射

基礎映射

<resultMap id="blogMap" type="Blog">
  <id property="id" column="id"/>           <!-- 主鍵,影響 equals/hashCode -->
  <result property="title" column="title"/>
  <result property="content" column="content"/>
  <result property="owner" column="owner"/>
</resultMap>

<select id="selectBlog" resultMap="blogMap"> ... </select>
  • <id>:標識主鍵列,對嵌套結果去重、緩存關鍵
  • <result>:普通屬性映射
  • column:SQL 列名/別名;property:Java 屬性名

進階:關聯查詢映射

一對一:<association>(延遲/立即加載)

<resultMap id="commentMap" type="Comment">
  <id property="id" column="id"/>
  <result property="content" column="content"/>
  <association property="blog" javaType="Blog" 
               select="selectBlog" column="blog_id"/>  <!-- 子查詢 -->
</resultMap>

<select id="selectComment" resultMap="commentMap">
  SELECT * FROM t_comment WHERE id = #{id}
</select>

<select id="selectBlog" resultType="Blog">
  SELECT * FROM t_blog WHERE id = #{id}
</select>

column:將當前結果集的 blog_id 作為參數傳給 selectBlog 的 #{id}。

一對多:<collection>(集合映射)

<resultMap id="blogMap" type="Blog">
  <id property="id" column="id"/>
  <result property="title" column="title"/>
  <collection property="comments" ofType="Comment"
               select="selectCommentsByBlog" column="id"/>
</resultMap>

<select id="selectBlog" resultMap="blogMap"> ... </select>
<select id="selectCommentsByBlog" resultType="Comment">
  SELECT * FROM t_comment WHERE blog_id = #{blogId}
</select>
  • ofType:集合泛型類型(區別於 javaType)
  • 同理 column 傳遞參數給子查詢

聯合查詢(單 SQL 多結果集)

<resultMap id="blogMap" type="Blog">
  <id property="id" column="blog_id"/>
  <result property="title" column="blog_title"/>
  <collection property="comments" ofType="Comment">
    <id property="id" column="comment_id"/>
    <result property="content" column="comment_content"/>
  </collection>
</resultMap>

<select id="selectBlogWithComments" resultMap="blogMap">
  SELECT b.id blog_id, b.title blog_title, c.id comment_id, c.content comment_content
  FROM t_blog b LEFT JOIN t_comment c ON b.id = c.blog_id
  WHERE b.id = #{id}
</select>

單次查詢,MyBatis 按 <id> 分組自動聚合,避免 N+1 問題。

選型決策

查詢類型
├─ 簡單單表,列名≈屬性名 → resultType (最省事)
├─ 列名不一致/需轉換 → resultMap 基礎映射
├─ 一對一/多關聯,可接受 N+1 → resultMap + select 子查詢 (延遲加載)
├─ 一對一/多關聯,高效能要求 → resultMap + 聯合查詢 (單 SQL)
└─ 複雜多層嵌套/存儲過程 → resultMap + 自定義 ResultHandler

總結

resultType 是「約定優於配置」的便捷入口,resultMap 是「配置優於約定」的終極武器。實際開發中:80% 場景用 resultType,20% 核心複雜查詢用 resultMap。掌握 <id>、<association>、<collection> 三大核心標籤與子查詢/聯合查詢兩種加載策略,即可駕馭所有映射場景。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved