文章目录
mapper.xml传参及其使用
@Param(“name”):用来给xml准确获取参数使用
一、mapper.xml传参.
1.传多个参数
mapper层方法:
List<Files> test(String name ,Integer size);
//xml:#{0}代表接收的是 dao 层中的第一个参数,#{1}代表 dao 层中第二
//参数,更多参数一致往后加即可
<select id="test" resulttype="files">
select * from ss_files where name = #{0} and size=#{1}
</select>
//带注释的方式
List<Files> test(@Param(value="name") String name ,@Param(value="size") Integer size);
//xml:#{0}代表接收的是 dao 层中的第一个参数,#{1}代表 dao 层中第二
//参数,更多参数一致往后加即可
<select id="test" resulttype="files">
select * from ss_files where name = #{0} and size=#{1}
</select>
2.键值对传参
Service层:
Map paramMap=new hashMap();
paramMap.put(“name ”, value);
paramMap.put(“size”,value);
mapper层方法:
List<Files> test(Map paramMap);
//直接通过属性名获取值
mapper.xml:
<select id=”test” resulttype=”files”>
select * from ss_files where name = #{name } and size=#{size}
</select>
//使用注释
mapper层方法:
List<Files> test(@Param(value="paramMap") Map paramMap);
//通过.点属性名的方式获取值
mapper.xml:
<select id=”test” resulttype=”files”>
select * from ss_files where name = #{paramMap.name } and size=#{paramMap.size}
<if test="paramMap.size!=''">
<![CDATA[and size> #{paramMap.size} ]]>
</if>
</select>
3.传数组/集合
//<foreach > 循环: 循环体:item 序号:index 集合:collection 分割符:separator
//-----------------------------数组
mapper层方法:
List<Files> test(@Param("arrayIds") Integer[] arrayIds);
//通过.点属性名的方式获取值
mapper.xml:
<select id=”test” resulttype=”files” parameterType="Integer[]">
select * from ss_files where
<if test="arrayIds!=null and arrayIds.length >0 ">
<foreach collection="arrayIds" open=" and id in(" close=")" item="item" separator=",">
#{item}
</foreach>
</if>
</select>
List<Files> test(@Param("listInt") List<Integer> listInt);
//通过.点属性名的方式获取值
mapper.xml:
<select id=”test” resulttype=”files”>
select * from ss_files where
<if test="listInt!= null and listInt.size()>0">
and ps.material_code in(
<foreach item="item" index="index" collection="listInt" separator=",">
#{item}
</foreach>
)
</if>
</select>
//-----------------------------集合
4.传对象参数
mapper层方法:
List<Files> test(Files file);
//直接通过属性名获取值
mapper.xml:
<select id=”test” resulttype=”files”>
select * from ss_files where name = #{name } and size=#{size}
</select>
//使用注释
mapper层方法:
List<Files> test(@Param(value="file") Files file);
//通过.点属性名的方式获取值
mapper.xml:
<select id=”test” resulttype=”files”>
select * from ss_files where name = #{file.name } and size=#{file.size}
<if test="file.size!=''">
<![CDATA[and size> #{file.size} ]]>
</if>
</select>
5.同时传多个参数和对象
//使用注释
mapper层方法:
List<Files> test(@Param(value="file") Files file,@Param(value="size") Integer size);
//通过.点属性名的方式获取值
mapper.xml:
<select id=”test” resulttype=”files”>
select * from ss_files where name = #{file.name } and size=#{size}
<if test="file.size!=''">
<![CDATA[and size> #{size} ]]>
</if>
</select>
二、mapper.xml部分参数作用
1.resultMap和 resultType的区别:
两者都是表示查询结果集与java对象之间的一种关系,处理查询结果集,映射到java对象。
resultMap:将查询结果集中的列一一映射到bean对象的各个属性,映射的查询结果集中的列标签可以根据需要灵活变化。
resultType :的是bean中的对象类,必须保证查询结果集中的属性 和 bean对象类中的属性是一一对应,大小写不敏感,但是有限制。
2.parameterMap(不推荐) & parameterType
parameterMap和resultMap类似,表示将查询结果集中列值的类型一一映射到java对象属性的类型上,在开发过程中不推荐这种方式。