關於mybatis如何返回list類型的解決

很多時候HashMap會滿足不了我們的需求,所以我們可以使用自定義的方式來定義屬於自己的list集合。

首先,直接在配置文件中定義一個關於student的list

type 寫相關model的全路徑。id是這個resultMap的唯一標識,方便待會我們調用這個定義好的 resultMap

 <resultMap type="model.Student" id="studentList">
      <result column="stu_id" property="id"/>
      <result column="ope_id"  property="opeId"/>
      <result column="stu_no"  property="stuNo"/>
      <result column="stu_sex" property="stuSex"/>
      <result column="stu_name" property="stuName"/>
      <result column="stu_birth"  property="stuBirth"/>
      <result column="stu_pic" property="stuPic"/>
      <result column="cla_id"  property="claId"/>        
</resultMap>

 

 

然后把上面的id,寫在你相關操作數據庫的語句中的 resultMap中。。這就可以實現了。。

<!-- select 區域  -->
    <select id="selectStudent" parameterType="model.Student" resultMap="studentList">
          SELECT * FROM student
          <where>
              <if test="id != null">
                  stu_id = #{id}
              </if>
              <if test="opeId != null">
                  And ope_id = #{opeId}
              </if>
              <if test="stuNo != null">
                  And stu_no = #{stuNo}
              </if>
              <if test="stuSex != null">
                  And stu_sex = #{stuSex}
              </if>
              <if test="stuName != null">
                  And stu_name = #{stuName}
              </if>
              <if test="stuBirth != null">
                  And stu_birth = #{stuBirth}
              </if>
              <if test="stuPic != null">
                  And stu_pic = #{stuPic}
              </if>
              <if test="claId != null">
                  And cla_id = #{claId}
              </if>         
          </where>
    </select>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。