1. 程式人生 > >Mybatis 的foreach 可以巢狀嗎?

Mybatis 的foreach 可以巢狀嗎?

  1. http://code.google.com/p/mybatis/source/browse/trunk/src/test/java/org/apache/ibatis/submitted/nested/?r=3467  
  2. Junit Test  
  3. NestedForEachTest.java  
  4. NestedForEach.xml  

照著這個效果,我在自己的paohe專案xml中這樣寫

<select id="queryQualityCost" parameterType="com.itonghui.biz.qualityclient.model.QualityClient" resultMap="BaseResultMap">
SELECT 
<include refid="Base_Column_List" />
FROM spot_quality_cost
      WHERE QUALITY_COST_ID IN
<foreach collection="qualitySampleinfoList" item="qualitySampleinfo" open="(" separator="," close=")"  >
<foreach collection="qualitySampleinfo.qualityVarietyList" item="qualityVariety" separator="," >
#{qualityVariety.qualityCostId}
    </foreach>
</foreach>
</select>

java程式碼:

QualityClientServiceImpl.java:

QualityClient qualityClient = new QualityClient();
qualityClient.setQualitySampleinfoList(sampleInfoListTmp);
List<QualityCost> qualityCostList = qualityCostMapper.queryQualityCost(qualityClient); //獲取最新的檢測項費用

其中QualityClient.java   vo類有 private List<QualitySampleinfo> qualitySampleinfoList; //樣品列表,

QualitySampleinfo.java vo類有 private List<QualityVariety> qualityVariety;

最終sql效果是

 SELECT QUALITY_COST_ID, QUALITY_DESC_ID, QUALITY_CHECK_ID, INSPECT_ORGANIZATION
_ID, TESTING_COST, CHECK_TYPE, IS_STANDARD_SAMPLE, REMARK, ALIVE_FLAG FROM spot_quality_cost WHERE QUALITY_COS
T_ID IN ( ? , ? , ? , ? , ? , ? ) 

mybatis多個foreach巢狀查詢成功!