Mybatis學(xué)習(xí)02:dao層函數(shù)傳入多個(gè)參數(shù)的處理方法

對(duì)于傳入一個(gè)參數(shù)的形式

dao層函數(shù)應(yīng)該這么寫

 public Student getStudent(@Param("id")int id);

在StudentMapper.xml應(yīng)該這么寫

<select id="getStudent" resultMap="User" >
        SELECT *FROM student WHERE stu_id=#{id}
    </select>

或者這種形式

<select id="getStudent" resultMap="User" >
        SELECT *FROM student WHERE stu_id=#{0}
    </select>

對(duì)于傳入多個(gè)參數(shù),也可以像上面這樣處理,但是這樣處理有時(shí)候顯得太麻煩,我們就想傳入一個(gè)list或者map.或者數(shù)組,這樣就方便多啦。
  傳入map的實(shí)例如下:
dao層函數(shù)是這樣的:

 public List<Artical> getSomeArticals(Map<String,Integer> map);

然后在StudentMapper.xml中:

 <select id="getSomeArticals" parameterType="hashmap" resultMap="StudentArticleList">
       select student.stu_id,student.stu_name,student.stu_age,student.stu_mon,article.id,article.title,article.stuid,article.content from student,article
              where stu_id=article.stuid 
              
              <if test="stuid!=null and departmentId!=''">  
                and stu_id=#{stuid} 
              </if>  
    </select>
    

stuid直接取就行,跟傳入單個(gè)一樣,只要與傳入進(jìn)來的map.key保持一致就行。
至于list,和array,道理是一樣,遍歷就行

<!--List:forech中的collection屬性類型是List,collection的值必須是:list,item的值可以隨意,Dao接口中參數(shù)名字隨意 -->  
    <select id="getEmployeesListParams" resultType="Employees">  
        select *  
        from EMPLOYEES e  
        where e.EMPLOYEE_ID in  
        <foreach collection="list" item="employeeId" index="index"  
            open="(" close=")" separator=",">  
            #{employeeId}  
        </foreach>  
    </select>  
  
    <!--Array:forech中的collection屬性類型是array,collection的值必須是:list,item的值可以隨意,Dao接口中參數(shù)名字隨意 -->  
    <select id="getEmployeesArrayParams" resultType="Employees">  
        select *  
        from EMPLOYEES e  
        where e.EMPLOYEE_ID in  
        <foreach collection="array" item="employeeId" index="index"  
            open="(" close=")" separator=",">  
            #{employeeId}  
        </foreach>  
    </select>  
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容