這個方法比較坑,其實就是線根據主對象的id進行分組,而後拿主對象的ID進行關聯對象查詢,最坑的地方就是須要訪問不少遍數據庫,就是有N個主對象,就訪問N+1次數據庫java
可是這個會比另外那個好理解一點數據庫
主對象mybatis
public class PortFeeTemplate { private List<PortArea> pas;// 配送區域詳情 private Long pftId; private String pftName; private Integer pftBaseNum; private Integer pftFee; private Integer pftContinue; private Integer pftContinueFee; //get和set方法省略 }
被關聯對象.net
public class PortArea { private Long paId; private Long paPftId; private String paProvince; private String paCity; private String paArea; //省略get和set方法 }
xml的resultMap配置code
<resultMap id="BaseResultMap" type="com.impression.model.PortFeeTemplate"> <id column="pft_id" property="pftId" jdbcType="BIGINT" /> <result column="pft_name" property="pftName" jdbcType="VARCHAR" /> <result column="pft_base_num" property="pftBaseNum" jdbcType="INTEGER" /> <result column="pft_fee" property="pftFee" jdbcType="INTEGER" /> <result column="pft_continue" property="pftContinue" jdbcType="INTEGER" /> <result column="pft_continue_fee" property="pftContinueFee" jdbcType="INTEGER" /> <!--看到沒,其實這邊就是根據每一個住對象的ID去查找關聯對象而後拼接進來--> <collection property="pas" ofType="com.impression.model.PortArea" select="queryPasByPftIdForBaseResultMap" column="{pftId2=pft_id}" /> </resultMap> <resultMap id="pasResult" type="com.impression.model.PortArea"> <id column="pa_id" property="paId" jdbcType="BIGINT" /> <result column="pa_pft_id" property="paPftId" jdbcType="BIGINT" /> <result column="pa_province" property="paProvince" jdbcType="VARCHAR" /> <result column="pa_city" property="paCity" jdbcType="VARCHAR" /> <result column="pa_area" property="paArea" jdbcType="VARCHAR" /> </resultMap> <!--這個方法就是留着供關聯查詢用的--> <select id="queryPasByPftIdForBaseResultMap" resultMap="pasResult"> select * from im_port_area where pa_pft_id = ${pftId2} </select>
其實這個方法也挺噁心的,你們仍是儘可能用 mybatis 對象內包含對象如何查詢(一)這篇裏面的那個方法來吧xml