'ResultMap'에 해당되는 글 1건

  1. 2014.07.31 resultMap을 사용한 Mybatis 예제

간단한 SQL의 경우 Vo를 바로 return하면 되지만,

복잡한 return형식이 필요할 경우 resultMap을 사용하는 게 편리하다. 


resultMap을 사용해서 데이타를 가져오는 경우

java코드는 100%동일하게 사용하고

mapper xml만 바꿔서 resultMap을 이용하는 예제로 만들어 보면 다음과 같다.


( 이전 글 예제에서 변경되는 부분만 기술 )



4.  UserMapper.xml 에서 resultMap을 사용하도록만 설정.


    <mapper namespace="my.UserMapper">

<resultMap id="userMap" type="my.UserVo">      //UserVo가 실질적인 return type이 됨.

        <id     property="username"    column="username" />

        <result property="age"     column="age" />

      </resultMap>

 

      <select id="user" resultMap="userMap">              //위에서 정의한 userMap을 지칭.

        SELECT  username,age

        FROM    user

        WHERE   username=#{name}

      </select>

    </mapper>




5. VO안에 또 객체가 있다던지 하는 복잡한 경우는 associationcollection으로 해결.  REF

   그리고, 어떤 값에 따라 다른  SQL을 실행할 수 있는 discriminator가 있다.

Posted by yongary
,