MyBatis - accidentlywoo/secsec GitHub Wiki

테이블 명과 vo 클래스의 필드명이 일치할 때 마이바티스가 리플렉션으로 setter 주입을 한다.

복잡한 결과매핑

마이바티스 복잡한 결과 매핑

<!-- 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>

Many To One관계에서 [Blog와 Author]

-> One : property -> VO 컬럼명

-> Many

One To Many관계에서[Blog와 Post]

id 값 설정

id 값 설정을 하지 않으면 select 결과가 쿼리문 결과와 동일하게 출력된다. 하지만, 객체처럼 사용하기 위해서는 id값을 지정해서 불필요한 객체생성을 막는다.

⚠️ **GitHub.com Fallback** ⚠️