mybatis collection 用法

  <resultMap type="map" id="getQuestionCrosswiseByTableNameMap">
    <result column="table_name"      property="tableName"/>
    <result column="survey_table_id" property="surveyTableId"/>
    <collection property="questions" ofType="map" javaType="list">
      <result column="question_crosswise_id"   property="questionCrosswiseId"/>
      <result column="question_crosswise_name" property="questionCrosswiseName"/>
    </collection>
  </resultMap>


column 与 property 是必须的,如果只写了column 也是没有效果的

ofType 是对象的所属类型 javaType :collection 的类型

如:

 <collection property="questions" ofType="map" javaType="list">

对应的java 形态为 :List<Map<String,Object>> 


  <resultMap type="map" id="getQuestionCrosswiseByTableNameTwoMap">
      <result column="table_name" property="tableName"/>
      <result column="survey_table_id" property="surveyTableId"/>
      <collection property="questionGrooup" ofType="map" javaType="list">
          <result column="question_crosswise_id" property="questionCrosswiseId"/>
          <result column="question_crosswise_name" property="questionCrosswiseName"/>
            <collection property="questions"  fetchType="eager" column="question_crosswise_id" select="com.yikangyiliao.pension.dao.QuestionUnitDao.getQuestionUnitAnswerMapByQuetionCrosswiseId">                </collection>
  </collection>
  </resultMap>
 <resultMap type="map" id="QuestionUnitAnswerMap">
      <result column="question_portrait_id" property="questionPortraitId"></result>
      <result column="question_portrait_name" property="questionPortraitName"></result>
      <result column="answer_type"  property="answerType"/>
      <result column="weight_value"  property="weightVal"/>
      <collection property="answers" ofType="map" javaType="list">
          <result column="answer_id" property="answerId"/>
          <result column="answer_text" property="answerText"/>
          <result column="answer_val"property="answerVal"/>
      </collection>
  </resultMap>
  
  <select id="getQuestionUnitAnswerMapByQuetionCrosswiseId" resultMap="QuestionUnitAnswerMap"     parameterType="long">
        select qp.question_portrait_id,
        qp.question_portrait_name,
                answers.answer_id,
                answers.answer_text,
                answers.answer_val,
                qcpa.answer_type,
                    quw.weight_value
        from question_units qp 
        left join question_unit_answer_maps qcpa
        on qp.question_portrait_id=qcpa.question_portrait_group
        left join answers on qcpa.answer_id=answers.answer_id
            left join question_unit_weights quw on qp.question_portrait_id=quw.question_portrait_id
        where qp.question_crosswise_id=#{questionCrosswiseId}
  </select>




  <!-- 查询某一个量表中的所有问题及答案 -->

 

<resultMap type="map" id="GetQuestionsAndAnswersByTableNameMap">
      <result column="survey_table_id" property="surveyTableId"/>
      <result column="table_name" property="tableName"/>
      <collection property="questions" ofType="map" javaType="list">
          <result column="question_id" property="questionId"/>
          <result column="question_text" property="questionText"/>
          <collection property="answers"  ofType="map" javaType="list">
              <result column="answer_id" property="answerId"/>
              <result column="answer_text" property="answerText"/>
              <result column="answer_val" property="answerVal"/>
             </collection>
        </collection>
  </resultMap>


你可能感兴趣的:(mybatis,Collection)