第一种
参数是常规的list, 但是xml变量名不是list------报错
完整错误如下:
org.apache.ibatis.binding.bindingexception: parameter ‘customeridlist’ not found. available parameters are [collection, list]
解释:
- 当我们传递一个 list 实例或者数组作为参数对象传给 mybatis。
- 当你这么做的时 候,mybatis 会自动将它包装在一个 map 中,用名称在作为键。
- list 实例将会以“list” 作为键,而数组实例将会以“array”作为键。
- 所以,当我们传递的是一个list集合时,mybatis会自动把我们的list集合包装成以list为key值的map。
dao 层: long selectcustomercountlist(list customeridlist); xml文件: <select id="selectcustomercountlist" parametertype="java.util.list" resulttype="java.lang.long"> select count(1) from np_customer_info where id in <foreach item="item" collection="customeridlist" separator="," open="(" close=")" index=""> #{item, jdbctype=integer} </foreach> </select> ====================== 注意:dao 层接口的参数名与xml 文件中的collection的属性值一致,是导致的问题的主要原因。
第二种
参数是常规的list, xml变量名是list------正常
- 利用mybatis给我们的封装进行xml配置,将我们的xml中collection属性值设置为list。
dao 层: long selectcustomercountlist( list customeridlist); xml文件: <select id="selectcustomercountlist" parametertype="java.util.list" resulttype="java.lang.long"> select count(1) from np_customer_info where id in <foreach item="item" collection="list" separator="," open="(" close=")" index=""> #{item, jdbctype=integer} </foreach> </select> ====================== 注意:此时collection强制指定为list且不可改变
第三种
利用注解@param指定入参list的名称------正常
dao层: long selectcustomercountlist(@param("customeridlist") list customeridlist); xml文件: <select id="selectcustomercountlist" parametertype="java.util.list" resulttype="java.lang.long"> select count(1) from np_customer_info where id in <foreach item="item" collection="customeridlist" separator="," open="(" close=")" index=""> #{item, jdbctype=integer} </foreach> </select> ====================== 注意: 此时的dao层参数名可以 @param("customeridlist") 与 collection的属性值一致
第四种
将list包装成map参数进行传递------正常
在service业务处理层次上面将参数进行包装 public long selectcustomercountmap(list customeridlist) { map maps = new hashmap(); maps.put("customerids", customeridlist); return customermapper.selectcustomercountmap(maps); } dao层: long selectcustomercountmap(map maps); xml文件: <select id="selectcustomercountmap" parametertype="java.util.map" resulttype="java.lang.long"> select count(1) from np_customer_info where id in <foreach item="item" collection="customerids" separator="," open="(" close=")" index=""> #{item, jdbctype=integer} </foreach> </select> ============== 注意: 入参类型是java.util.map而不再是list ,此时的collection属性值为map中的key值。
第五种
把list 放入一个bean对象中 ------报错
model层(bean层): public class companyquerymodel { private string companytype; private list<string> customscodelist; } dao层: list<companyresultmodel> selectcompanyinfo (companyquerymodel companyquerymodel); xml层: <select id="selectcompanyinfo" parametertype="com..dto.companyquerymodel" resulttype="com.dto.companyresultmodel"> select * from np_company_info <if test="customscodelist != null and customscodelist.size() > 0"> and v.customs_code in <foreach item="item" index="index" collection="customscodelist" open="(" separator="," close=")"> #{item} </foreach> </if> </select>
第六种
把list 放入一个bean对象中,利用@param指定入参bean名称,xml取bean.list------正常
model层(bean层): public class companyquerymodel { private string companytype; private list<string> customscodelist; } dao层: list<companyresultmodel> selectcompanyinfo(@param("model") companyquerymodel companyquerymodel); xml层: <select id="selectcompanyinfo" parametertype="com..dto.companyquerymodel" resulttype="com.dto.companyresultmodel"> select * from np_company_info <if test="model.customscodelist != null and model.customscodelist.size() > 0"> and v.customs_code in <foreach item="item" index="index" collection="model.customscodelist" open="(" separator="," close=")"> #{item} </foreach> </if> </select>
第七种
把list 放入一个bean对象中, xml不用#{item} 改为 #{tagids[${index}]}
- 这中写法意思是,取这个数组中的每一个,因为字段是list。
bean层: public class alarmconditiondto { private list<string> orgids; private list<string> tagids; private string alerttype; } dao层: list<map<string,string>> selectdevicecountbycondition(alarmconditiondto alarmconditiondto); xml层: <select id="selectdevicecountbycondition" resulttype="java.util.map"> select * from md_tag_target_relation_device where 1=1 <if test="tagids != null and tagids.size()>0"> and tag_id in <foreach collection="orgids" index="index" open="(" close=")" separator="," item="item"> #{tagids[${index}],jdbctype=varchar} </foreach> </if> <if test="orgids != null and orgids.size()>0"> and d.region_code in <foreach collection="orgids" index="index" open="(" close=")" separator="," item="item"> #{orgids[${index}],jdbctype=varchar} </foreach> </if>
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论