1. 普通参数传参(@Param)
Dao层:
int deleteById(@Param(”id”)Integer id, @Param (”name”) String name)) ;
Mapper.xml:
<delete id="deleteById">
DELETE FROM students
WHERE id = #{id,jdbcType=INTEGER}
<if test="name != null" >
,p_name = #{name}
</if>
</delete>
2. 单个(多个)集合类型参数
Dao层:
int batchUpdateStatusOfChecking(@Param(value=”ids”)String[] ids);
Mapper.xml:
<update id="batchUpdateStatusOfChecking">
update students
<set>
status = 5
</set>
<if test="ids != null and ids != ''">
where status in(2,4)
and id in
<foreach collection="ids" item="item" index="index" open="(" separator="," close=")">
#{item}
</foreach>
</if>
</update>
解析:collection表示传入的数组参数的名字,item集合中每一个元素进行迭代时的别名。
3. 列表(List)
Dao层:
int batchInsertStudent(List<String> students);
Mapper.xml:
<insert id="batchInsertStudent" parameterType="java.util.List">
insert into student (id,name,sex,tel,address) values
<foreach collection="list" item="item" index="index" separator=",">
(#{item.id},#{item.name},#{item.sex},#{item.tel},#
{item.address})
</foreach>
</insert>
4. Map
Dao层:
int batchUpdateStatusForChecking(@Param("map") Map<String,String> map, @Param("updateBy")String updateBy);
Mapper.xml:
<update id="batchUpdateStatusForChecking">
<foreach collection="map" index="key" item="ent" separator=";">
update search
set status = 2
<if test="updateBy!= null">
,update_by = #{updateBy}
</if>
,update_time = #{ent}
where id = #{key}
and status = 1
</foreach>
</update>
6. 混合参数(既有简单类型,同时又有集合类型)
Dao层:
int batchUpdateStatusOfChecking(@Param("ids")String[] ids,@Param("lastUpdateBy")String lastUpdateBy);
Mapper.xml:
<update id="batchUpdateStatusOfChecking">
update search
<set>
status = 5
<if test="lastUpdateBy != null" >
,last_update_by = #{lastUpdateBy}
</if>
</set>
<if test="ids != null && ids != ''">
where status in(2,4)
and id in
<foreach collection="ids" item="item" index="index" open="(" separator="," close=")">
#{item}
</foreach>
</if>
</update>