Mybatis往Mapper.xml文件中传递多个参数问题
作者:w32718155
这篇文章主要介绍了Mybatis往Mapper.xml文件中传递多个参数问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
Mybatis往Mapper.xml传递多个参数
场景1
当Mapper接口定义了多个参数的时候就需要使用Param注解来给参数取个名字,然后在Mapper.xml文件中,使用Param注解中的值(名字),告诉Mybatis你使用的变量是哪一个,用在哪,此时insert就不用在声明参数类型了,因为有多个参数类型,而paramType只能声明一个,同理,update,delete,select,都是一样的。
场景2
当Mapper接口只有一个参数的时候,在Mapper.xml文件中需要声明其参数类型,此时我们可以使用任意名称的变量来获取传入的值,因为传递进来的参数只有一个。(包括集合)
场景3
当Mapper接口定义了一个集合参数和简单类型参数的时候也需要使用Param注解来给参数取个名字,然后在Mapper.xml文件中使用取得名字来使用它们。
集合在foreach中的colloection标签中,也是写的参数的名字,如果接口方法中只有一个集合参数,那么foreach中的colloection标签随便写什么都是可以的。

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对象属性的类型上,在开发过程中不推荐这种方式。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
