xxxList not found

问题:MyBatis接口中以list作为参数类型,在mapper.xml中以改list的名称进行判定,报错


总结:
1.[table]
|类型|List|Array|Map|
|XML中接收值|list|array|封装的key|
|参数类型|java.util.List|java.util.Array|java.util.Map|
[/table]
2.使用map传递参数,mapper.xml中接收的值为map中的key值
3.在mapper.xml通过po类进行参数接收

<select id="viewMerchantBrand" resultMap="BaseResultMap" parameterType="user.po.MerchantBrand" >
select
<include refid="Base_Column_List" />
from usr_merchant_brand
where 1 = 1
<if test="merchantId != null" >
and merchant_id = #{merchantId,jdbcType=VARCHAR}
</if>
<if test="brandId != null" ><!--Po类中的属性字段名称 -->
and brand_id = #{brandId,jdbcType=INTEGER}
</if>
</select>

4.关于po与map做为参数的比较
[table]
|名称|po|map|
|集合如list|将该集合作为po的属性|集合作为key值|
|类属性|类中全部属性|类中部分属性|
[/table]

博文转载:[url]http://chenzhou123520.iteye.com/blog/1921284[/url]
有些许改动,改动的地方是MyBatis的调用方式不同
错误提示:
[color=red]
Error querying database.Cause: org.apache.ibatis.binding.BindingException: Parameter 'studentNameList' not found. Available parameters are [list]
Cause: org.apache.ibatis.binding.BindingException: Parameter 'studentNameList' not found. Available parameters are [list]
[/color]

单元测试类

@Test
public void testgetStudentCount(){
List<String> studentNameList = new ArrayList<String>();
studentNameList.add("zhangsan");
studentNameList.add("lisi");
int count = studentDao.getStudentCount(studentNameList);
System.out.println(count);
}


mapper.java

pulibc int getStudentCount(List<String> studentNameList);


mapper.xml

<select id="getStudentCount" parameterType="java.util.List" resultType="java.lang.Integer">
<![CDATA[ SELECT COUNT(*) FROM t_student WHERE 1=1 ]]>
<if test="studentNameList != null">
AND student_name in
<foreach collection="studentNameList" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</if>
</select>


报错原因:

根据报错日志分析,是MyBatis在解析xml时找不到其中声明的studentNameList,
但是在接口中传的参数就是studentNameList,怎么会报错呢?

查询了一下MyBatis官方的说明文档,终于找到了原因,在http://mybatis.github.io/mybatis-3/zh/dynamic-sql.html#foreach里有一段说明:
写道
注意 你可以传递一个 List 实例或者数组作为参数对象传给 MyBatis。当你这么做的时 候,MyBatis 会自动将它包装在一个 Map 中,用名称在作为键。List 实例将会以“list” 作为键,而数组实例将会以“array”作为键。

因为我传的参数只有一个,而且传入的是一个List集合,所以mybatis会自动封装成Map<"list",studentNameList>。在解析的时候会通过“list”作为Map的key值去寻找。但是我在xml中却声明成studentNameList了,所以自然会报错找不到。

解决方式:


第一种就是修改mapper.xml中foreach标签内容,把studentNameList修改为list

<if test="list != null">
AND student_name in
<foreach collection="list" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</if>

不过这种方式我个人不太建议,因为以后如果要扩展该方法,增加集合参数的时候,还得修改xml中的内容。

第二种方式,修改dao中的参数传入方式,手动封装成map,然后把map当参数传进去

Dao方法修改为:


public int getStudentCount(List<String> studentNameList){
//把参数手动封装在Map中
Map<String, Object> map = new HashMap<String, Object>();
map.put("studentNameList", studentNameList);
return super.count("getStudentCount", map);
}

然后修改mapper.xml中的parameterType类型为Map

<!--注意下面的parameterType类型必须修改为Map类型,foreach中引用的List名称不用改变-->
<select id="Student.getStudentCount" parameterType="java.util.Map" resultType="java.lang.Integer">
<![CDATA[
SELECT
COUNT(*)
FROM
t_student WHERE 1=1
]]>
<if test="studentNameList != null">
AND student_name in
<foreach collection="studentNameList" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</if>
</select>

修改完后,重新执行了一下测试用例,测试通过。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值