308. Advanced Result Maps ( 향상된 결과 맵 )

2012. 6. 7. 17:41공부/MYBATIS

마이바티스는 한 가지 생각을 염두에 두고 만들어졌어 : 데이터베이스는 항상 원하거나 필요로 하는 것은 아니여. 모든 데이터베이스의 3rd normal form 이나 BCNF가 완벽하기를 염원했지만 그렇지 못해. 그리고, 만약 하나의 데이터베이스가 완벽하게 모든 어플리케이션에 맵핑하는게 가능다면  대단해질거야. 하지만 그렇지 못해. ResultMap 이 마이바티스가 이 문제에 대해 제공하는 답이야.

예를 들어, 어떻게 이 문장을 매핑할까?

<!-- Very Complex Statement -->
<select id="selectBlogDetails" parameterType="int" resultMap="detailedBlogResultMap">
  select
       B.id as blog_id,
       B.title as blog_title,
       B.author_id as blog_author_id,
       A.id as author_id,
       A.username as author_username,
       A.password as author_password,
       A.email as author_email,
       A.bio as author_bio,
       A.favourite_section as author_favourite_section,
       P.id as post_id,
       P.blog_id as post_blog_id,
       P.author_id as post_author_id,
       P.created_on as post_created_on,
       P.section as post_section,
       P.subject as post_subject,
       P.draft as draft,
       P.body as post_body,
       C.id as comment_id,
       C.post_id as comment_post_id,
       C.name as comment_name,
       C.comment as comment_text,
       T.id as tag_id,
       T.name as tag_name
  from Blog B
       left outer join Author A on B.author_id = A.id
       left outer join Post P on B.id = P.blog_id
       left outer join Comment C on P.id = C.post_id
       left outer join Post_Tag PT on PT.post_id = P.id
       left outer join Tag T on PT.tag_id = T.id
  where B.id = #{id}
</select>

아마도 작성자에 의해 씌여지고, 많은 포스트를 가지고, 각각의 포스트는 0개나 수 많은 코멘트와 태그를 가진 블로그의 구성에 지능적인 객체 모델을 매핑하고 싶을거야. 다음은 복잡한 ResultMap의 완벽한 예제야 ( 작성자, 블로그, 포스트, 코멘트와 태그가 모두 타입 알리아스임을 가정 ). 슬쩍 보고 걱정하지 마라, 앞으로 하나 하나 다져볼거야. 처음엔 주눅이 들겠지만, 사실상 매우 간단해.

<!-- Very Complex Result Map -->
<resultMap id="detailedBlogResultMap" type="Blog">
  <constructor>
    <idArg column="blog_id" javaType="int"/>
  </constructor>
  <result property="title" column="blog_title"/>
  <association property="author" javaType=" Author">
    <id property="id" column="author_id"/>
    <result property="username" column="author_username"/>
    <result property="password" column="author_password"/>
    <result property="email" column="author_email"/>
    <result property="bio" column="author_bio"/>
    <result property="favouriteSection" column="author_favourite_section"/>
  </association>
  <collection property="posts" ofType="Post">
    <id property="id" column="post_id"/>
    <result property="subject" column="post_subject"/>
    <association property="author" javaType="Author"/>
    <collection property="comments" ofType=" Comment">
      <id property="id" column="comment_id"/>
    </collection>
    <collection property="tags" ofType=" Tag" >
      <id property="id" column="tag_id"/>
    </collection>
    <discriminator javaType="int" column="draft">
      <case value="1" resultType="DraftPost"/>
    </discriminator>
  </collection>
</resultMap>

resultmap 요소는 수 많은 부 요소와 토론할 갖치가 았는 구조를 가지고 있어. 다음은 resultMap 요소의 개념적인 이야기야.

resultMap

  • constructor - 초기화되는 클래스의 생성자에 결과를 주입하기 위해 사용
    • idArg - ID 인자; ID와 같은 flagging 결과는 전반적인 성능을 향상시킴
    • arg - 생성자에 주입되는 보통의 결과 
  • id – ID 결과; ID와 같은 flagging 결과는 전반적인 성능을 향상시킴
  • result –  필드나 자바빈 프라퍼티에 주입되는 보통의 결과
  • association – a complex type association; many results will roll up into this type
    • nested result mappings – associations are resultMaps themselves, or can refer to one
  • collection – a collection of complex types
    • nested result mappings – collections are resultMaps themselves, or can refer to one
  • discriminator – uses a result value to determine which resultMap to use
    • case – a case is a result map based on some value
      • nested result mappings – a case is also a result map itself, and thus can contain many of these same elements, or it can refer to an external resultMap.

BEST PRACTICE  언제나