對(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>