使用association实现一对一对应关系

	<select id="getConfigMenuItems" resultMap="configMenuItemsMap">
		SELECT
			   m1.id,
			   m1.ppath,
			   m1.path,
			   m1.name,
			   ly1.isshow AS ly1_isshow,
			   ly1.orderby AS ly1_orderby,
			   ly2.isvalid AS ly2_isvalid,
			   ly2.NAME AS ly2_name,
			   ly2.url AS ly2_url,
			   ly2.ID AS ly2_id,
               d1.name as layer_type_name ,
               d1.code as layer_type_code
		FROM
			 catlog_config AS m1
				 LEFT JOIN catlog_case_config AS ly1 ON CAST ( m1.ID AS VARCHAR ) = CAST ( ly1.belong_catlog AS VARCHAR )
				 LEFT JOIN drawing_case AS ly2 ON CAST ( ly2.ID AS VARCHAR ) = CAST ( ly1.belong_case AS VARCHAR )
                 LEFT JOIN mutiplan_range_dic AS d1 ON d1.id = ly2.type
        ORDER BY
				 m1.orderby,
				 m1.PATH,
				 ly2.orderby;
    </select>

	<resultMap id="configMenuItemsMap" type="com.xx.mvc.entity.menu.CatlogConfigMenu">
		<id property="id" column="id"/>
        <result property="ppath" column="ppath" />
        <result property="path" column="path" />
		<result property="name" column="name"/>
		<result property="note" column="note"/>

		<association property="layerInfo" javaType="com.xx.mvc.entity.menu.CatlogConfigLayer">
			<id column="ly2_id" property="layerId"/>
			<result column="ly2_name" property="layerName"/>
			<result column="ly2_url" property="layerUrl"/>
            <result column="layer_type_name" property="layerTypeName"/>
            <result column="layer_type_code" property="layerTypeCode"/>
        </association>
	</resultMap>

使用collection实现一对多

<select id="getCompareSelections" resultMap="compareSelectionsMap">
        SELECT t1.id,
               t1.RIGHT,
               t1.NAME,
               t1.LEFT,
               t1.orderby,
               t2.id            AS ly_id,
               t2.orderby       AS ly_orderby,
               t2.ws_type       AS ly_ws_type,
               t2.left_or_right AS ly_left_or_right,
               t2.url           AS ly_url,
               t2.code          AS ly_code,
               d1.code          AS wsTypeCode,
               d1.name          AS wsTypeName
        FROM compare_scheme_relation AS t1
                 LEFT JOIN compare_scheme_information AS t2 ON t2.code = t1.ID
                 LEFT JOIN mutiplan_range_dic AS d1 ON d1.id = t2.ws_type
        ORDER BY t1.orderby, t2.orderby
    </select>

    <resultMap id="compareSelectionsMap"
               type="com.xx.mvc.entity.compare.CompareSchemeRelation"
               autoMapping="true">
        <id property="id" column="id"/>
        <!--columnPrefix="rit_" 似乎不可用-->
        <collection property="layerInfoArray"
                    ofType="com.xx.mvc.entity.compare.CompareSchemeInformation">
            <!--通过id会自动过滤重复数据-->
            <id property="id" column="ly_id"/>
            <result property="wsType" column="ly_ws_type"/>
            <result property="orderby" column="ly_orderby"/>
            <result property="wsType" column="ly_ws_type"/>
            <result property="leftOrRight" column="ly_left_or_right"/>
            <result property="url" column="ly_url"/>
            <result property="wsTypeCode" column="wsTypeCode"/>
            <result property="wsTypeName" column="wsTypeName"/>
        </collection>
    </resultMap>

注意

1). property直接对应实体类的属性名,而非数据库表名。 <result property="wsType" column="ly_ws_type"/>

2). autoMapping="true" 可以自动匹配,少些一些<result property="xx" column="xx"/>之类的代码

3). 可进一步嵌套,如下

<resultMap id="searchMutPlanIndexInfoMap"
           type="com.th.multiplanning.entity.business.indexStandard.MutplanIndex" autoMapping="true">
    <id property="id" column="id"/>
    <collection property="symbolList" ofType="com.th.multiplanning.entity.business.treeManager.MutiSymbol">
        <id property="id" column="sm_id"/>
        <result property="belongIndex" column="sm_belong"/>
        <result property="val" column="sm_val"/>
        <result property="range" column="sm_range"/>
		<result property="name" column="sm_name"/>
		<association property="fileInfo" javaType="com.th.multiplanning.entity.document.filemanager.FileItem">
			<id column="f_id" property="id"/>
			<result column="f_name" property="name"/>
			<result column="f_file_name" property="fileName"/>
		</association>
    </collection>
</resultMap>