IBATIS/myBatis
2017.05.22 / 18:08

mybatis 3.0 ¿¡¼­ resultMap È°¿ë

ducati
Ãßõ ¼ö 273

1. ÇϳªÀÇ Å×ÀÌºí¿¡ ´ëÇÑ °á°ú Á¶È¸

 

         <resultMap id="bbsDataResult" type="kr.xboard.dao.bbsdata.BbsDataBean">

                  <id property="bbsId" column="S_BBS_ID" javaType="java.lang.String"/>

                  <id property="bbsIdx" column="I_BBS_IDX" javaType="int"/>

                  <result property="parentIdx" column="I_PARENT_IDX" javaType="int"/>

                  <result property="idxLeft" column="I_IDX_LEFT" javaType="int"/>

                  <result property="idxRight" column="I_IDX_RIGHT" javaType="int"/>

                  <result property="bbsTitle" column="S_BBS_TITLE" javaType="java.lang.String"/>

                  <result property="bbsDesc" column="S_BBS_DESC" javaType="java.lang.String"/>

         </resultMap>

 

         <select id="getBbsDataList" parameterType="BbsDataBean" resultMap="bbsDataResult">

         <!-- bbs_data ÀÇ ¸ñ·Ï -->

                  SELECT S_BBS_ID,

                              I_BBS_IDX,

                              I_PARENT_IDX,

                              I_IDX_LEFT,

                              I_IDX_RIGHT,

                              S_BBS_TITLE,

                              S_BBS_DESC

                    FROM bbs_data

                  <where>

                           <if test="bbsId != null and bbsId != ''">

                                   AND S_BBS_ID = #{bbsId}

                           </if>

                           <if test="bbsIdx != null and bbsIdx != ''">

                                   AND I_BBS_IDX = #{bbsIdx}

                           </if>

                  </where>

         </select>

 

getBbsDataList Äõ¸®¿¡¼­ °á°ú°¡ bbsDataResult ·Î ¸ÊÇÎÀÌ µÈ´Ù.

resultmap ÀÇ id ´Â pk ¿©ºÎ¸¦ ³ªÅ¸³»¸çÀÌÈÄ ¼³¸íÇÒ association, collection ¿¡¼­ Áß¿äÇÏ°Ô »ç¿ëµÈ´Ù.

 

°Ô½ÃÆÇ ³»¿ëÀ» º¸±â¸¦ Çϱâ À§Çؼ­´Â

°Ô½ÃÆÇ¿¡ ´ëÇÑ ±âº» Á¤º¸ (bbs_info) ¿Í °Ô½Ã±Û (bbs_data), °Ô½Ã±Û¿¡ ÷ºÎ ÆÄÀÏ(bbs_file), °Ô½Ã±Û¿¡ ´ëÇÑ µ¡±Û (bbs_comment)°¡ ÀÖÀ» ¼ö ÀÖ´Ù.

ÀÌ¿Í °°ÀÌ 4°³ÀÇ Å×ÀÌºí¿¡ ´ëÇØ °¢°¢ Äõ¸®¸¦ ÀÛ¼ºÇÏÁö ¾Ê°í,

ÇϳªÀÇ resultmap ¿¡¼­ ó¸® °¡´ÉÇϵµ·Ï Çغ¸ÀÚ

 

 

Entity Generator ·Î »ý¼ºµÈ BbsDataBean ÆÄÀÏ¿¡ ´ÙÀ½°ú °°ÀÌ

°Ô½ÃÆÇ Á¤º¸ (BbsInfoBean), Ã·ºÎÆÄÀÏ (BbsFileBean), µ¡±Û (BbsCommentBean) À» °¢°¢ µî·ÏÇÑ´Ù.

BbsInfoBean Àº °Ô½Ã±Û¿¡ ´ëÇØ 1°³ÀÇ row ¸¦ °¡Áö°í

BbsFileBean °ú BbsCommentBean Àº n°³ÀÇ row ¸¦ °¡Áú ¼ö ÀÖ´Ù.

 

kr.xboard.dao.bbsdata.BbsDataBean.java

 

public class BbsDataBean {

        

 

         /**

          * °Ô½ÃÆÇ Á¤º¸

          */

         public BbsInfoBean bbsInfoBean;

        

         /**

          * °Ô½Ã¹° ÆÄÀÏ ¸ñ·Ï

          */

         public List<BbsFileBean> bbsFileBeans;

        

         /**

          * °Ô½Ã¹° µ¡±Û ¸ñ·Ï

          */

         public List<BbsCommentBean> bbsCommentBeans;

        

 

    /**

     * bbs_data.S_BBS_ID

     * defaultValue = ''

     *

     * °Ô½ÃÆÇ ÄÚµå

     */

    private String bbsId = "";

 

sqlmap.xml ÆÄÀÏ¿¡ ¼öÁ¤µÈ BbsDataBean ÆÄÀÏ¿¡ ´ëÀÀÇϴ resultMap À» »ý¼ºÇÑ´Ù.

 

kr.xboard.dao.bbsdata.BbsData-sqlmap.xml

 

         <resultMap id="bbsDataExResult" extends="bbsDataResult" type="BbsDataBean">

                  <association property="bbsInfoBean" column="{bbsId=S_BBS_ID}"select="kr.xboard.dao.bbsinfo.BbsInfoDao.getBbsInfoItem"/>

                  <collection property="bbsFileBeans" ofType="BbsFileBean" column="{bbsId=S_BBS_ID,bbsIdx=I_BBS_IDX}" select="kr.xboard.dao.bbsfile.BbsFileDao.getBbsFileList"/>

                  <collection property="bbsCommentBeans" ofType="BbsCommentBean" column="{bbsId=S_BBS_ID,bbsIdx=I_BBS_IDX}"select="kr.xboard.dao.bbscomment.BbsCommentDao.getBbsCommentList"/>

         </resultMap>     

 

select="kr.xboard.dao.bbsinfo.BbsInfoDao.getBbsInfoItem"

ÀÇ getBbsInfoItem Àº kr.xboard.dao.bbsInfo.BbsInfo-sqlmap.xml ÆÄÀÏ¿¡ Á¸ÀçÇÑ´Ù.

column="{bbsId=S_BBS_ID}"

ÀÇ bbsId ´Â pk °ªÀ» ³ªÅ¸³»¸ç, getBbsInfoItem select query ÀÇ resultMap ÀÎ

 

         <resultMap id="bbsInfoResult" type="kr.xboard.dao.bbsinfo.BbsInfoBean">

                  <id property="bbsId" column="S_BBS_ID" javaType="java.lang.String"/>

 

¿¡¼­ id ·Î µî·ÏµÇ¾î ÀÖ´Ù.

column ÀÇ °ª¿¡ µû¶ó select ¿¡¼­ ¼öÇàµÇ´Â Äõ¸®ÀÇ #{bbsId} °ªÀÌ ¸ÊÇÎÀÌ µÈ´Ù.

 

ÀÌÁ¦ getBbsDataItem ÀÇ resultMap À» »õ·Î Ãß°¡ÇÑ bbsDataExResult ·Î º¯°æÇÑ ÈÄ

 

         <select id="getBbsDataItem" parameterType="BbsDataBean" resultMap="bbsDataExResult">

         <!-- bbs_data ÀÇ »ó¼¼ ³»¿ª -->

                  SELECT S_BBS_ID,

                              I_BBS_IDX,

                              I_PARENT_IDX,

                              I_IDX_LEFT,

                              I_IDX_RIGHT,

                              S_BBS_TITLE,

                              S_BBS_DESC

                    FROM bbs_data

                  <where>

                           <if test="bbsId != null and bbsId != ''">

                                   AND S_BBS_ID = #{bbsId}

                           </if>

                           <if test="bbsIdx != null and bbsIdx != ''">

                                   AND I_BBS_IDX = #{bbsIdx}

                           </if>

                  </where>

         </select>

 

Å×ÀÌºí¿¡ ¸î°³ÀÇ µ¥ÀÌÅ͸¦ Ãß°¡ÇÏÀÚ.

±×¸®°í test.xboard.BbsDataControllerTest.java ÆÄÀÏÀ» ¿­¾î

 

@Test

    public void getBbsDataItemTest () {

                  BbsDataBean bean = new BbsDataBean();

                  bean.setBbsId("1");                // Å×½ºÆ®¿ë

                  bean.setBbsIdx(1);                 // Å×½ºÆ®¿ë

                  bbsDataController.getBbsDataItem(bean);

    }   

 

bbsid, bbsidx ¸¦ ÀÔ·ÂÇÑ ÈÄ

 

JUnit Test ¸¦ ¼öÇàÇÏ¸é ¿À·ù ¾øÀÌ Á¤»óÀûÀ¸·Î µ¿À۵ȴÙ.

 

P6Spy ·Î±×¸¦ º¸¸é

 

bbs_data Äõ¸®°¡ ¼öÇàµÇ±â ÀÌÀü¿¡

bbs_info, bbs_file, bbs_comment ¿¡ °¢°¢ Äõ¸®°¡ ¼öÇàµÇ´Â °ÍÀ» È®ÀÎÇÒ ¼ö ÀÖ´Ù.

 

 

 

º¹ÀâÇÑ join Äõ¸®¿¡ ´ëÇÑ resultMap

º¹ÀâÇÑ join Äõ¸®ÀÇ °æ¿ì Äõ¸®¸¦ Çϳª¾¿ ¼öÇàÇϱ⠺¸´Ù´Â ÇÑ Äõ¸®¿¡¼­ °á°ú¸¦ °¢°¢ °¡Áö°í ¿Ã ¼ö µµ ÀÖ´Ù.

 

        

         <resultMap id="bbsDataEx2Result" extends="bbsDataResult" type="BbsDataBean">

                  <association property="bbsInfoBean" column="{bbsId=S_BBS_ID}"select="kr.xboard.dao.bbsinfo.BbsInfoDao.getBbsInfoItem"/>

                  <collection property="bbsFileBeans" ofType="BbsFileBean" column="{bbsId=S_BBS_ID,bbsIdx=I_BBS_IDX}" resultMap="kr.xboard.dao.bbsfile.BbsFileDao.bbsFileResult"/>

                  <collection property="bbsCommentBeans" ofType="BbsCommentBean" column="{bbsId=S_BBS_ID,bbsIdx=I_BBS_IDX}"resultMap="kr.xboard.dao.bbscomment.BbsCommentDao.bbsCommentResult"/>          

         </resultMap>

 

         <select id="getBbsDataItem2" parameterType="BbsDataBean" resultMap="bbsDataEx2Result">

                  SELECT *

                    FROM bbs_data a, bbs_file c, bbs_comment d

                   WHERE a.S_BBS_ID = c.S_BBS_ID

                     AND a.I_BBS_IDX = c.I_BBS_IDX

                     AND a.S_BBS_ID = d.S_BBS_ID

                     AND a.I_BBS_IDX = d.I_BBS_IDX

                           <if test="bbsId != null and bbsId != ''">

                                   AND a.S_BBS_ID = #{bbsId}

                           </if>

                           <if test="bbsIdx != null and bbsIdx != ''">

                                   AND a.I_BBS_IDX = #{bbsIdx}

                           </if>              

         </select>        

 

getBbsDataItem2 ´Â bbs_data, bbs_file, bbs_comment ¸¦ °¢°¢ join À» Çؼ­ °á°ú¸¦ °¡Áö°í ¿Â´Ù.

resultMap ¿¡¼­ÀÇ Â÷ÀÌÁ¡Àº collection ÀÇ select attribute °¡ resultMap À¸·Î º¯°æµÇ¾ú´Ù.

 

[Ãâó] mybatis ÀÇ resultmap È°¿ë|ÀÛ¼ºÀÚ YES

[Ãâó] mybatis ÀÇ resultmap È°¿ë|ÀÛ¼ºÀÚ 



Ãâó: http://devx.tistory.com/entry/mybatis-30-¿¡¼­-resultMap-È°¿ë [Meta Developer Story]