mybatis:Parameter 'ids' not found. Available parameters are [templateId, param1, param2, valueList]

mybatis:Parameter 'ids' not found. Available parameters are [templateId, param1, param2, valueList]

今天晚上发现了一个很有意思的问题:

mybatis:Parameter 'ids' not found. Available parameters are [templateId, param1, param2, valueList]

找不到这个ids这个集合变量,

<!-- public List<Employee> getEmpsByConditionForeach(List<Integer>  emp); -->
	<select id="getEmpsByConditionForeach" resultType="com.cn.zhu.bean.Employee"
	>
	    select *  from tbl_employee where id in
	   <!-- 
	     collection 指定要遍历的集合
	         list类型的参数会特殊处理封装在map中,map的key就叫list
	      item 将当前遍历出的元素赋值给指定 的变量
	      #{变量名} 就能取出变量的值也就是当前遍历
	      separator 每个元素之间的分隔符
	      open 遍历出所有结果拼接一个开始的字符
	      close: 遍历出所有结果拼接一个结束的字符
	      index : 索引  遍历list的时候index是索引  ,item就是当前值
	                 遍历map的时候index表示的就是map的key    item就是map的值
	    -->
	    <foreach collection="ids" item="item_id" separator="," open="(" close=")"
	    >
	      #{item_id}
	    </foreach>
	    
	</select>

控制台报错

### Error querying database.  Cause: org.apache.ibatis.binding.BindingException: Parameter 'ids' not found. Available parameters are [list, collection]

一直找不到ids这个值,那么问题在哪?

其实

collection 指定要遍历的集合
        list类型的参数会特殊处理封装在map中,map的key就叫list

改成这样:::

<!-- public List<Employee> getEmpsByConditionForeach(List<Integer>  emp); -->
	<select id="getEmpsByConditionForeach" resultType="com.cn.zhu.bean.Employee"
	>
	    select *  from tbl_employee where id in
	   <!-- 
	     collection 指定要遍历的集合
	         list类型的参数会特殊处理封装在map中,map的key就叫list
	      item 将当前遍历出的元素赋值给指定 的变量
	      #{变量名} 就能取出变量的值也就是当前遍历
	      separator 每个元素之间的分隔符
	      open 遍历出所有结果拼接一个开始的字符
	      close: 遍历出所有结果拼接一个结束的字符
	      index : 索引  遍历list的时候index是索引  ,item就是当前值
	                 遍历map的时候index表示的就是map的key    item就是map的值
	    -->
	    <foreach collection="list" item="item_id" separator="," open="(" close=")"
	    >
	      #{item_id}
	    </foreach>
	    
	</select>


把collection="list"   问题解决

测试类

@Test
	public void  testDynamicSql() throws  IOException{
		SqlSessionFactory sqlSessionFactory=getSqlSessionFactory();
		SqlSession openSession=sqlSessionFactory.openSession();
		try {
			EmployeeMapperDynamicSQL   mapper=	openSession.getMapper(EmployeeMapperDynamicSQL.class);	
			
			List<Employee> list11 = mapper.getEmpsByConditionForeach(Arrays.asList(1,2,3,4));
			for (Employee emp : list11) {
				System.out.println(emp);
			}
		} catch (Exception e) {
			// TODO: handle exception
			openSession.close();
			e.printStackTrace();
		}

	}


那么,为什么要那样改呢?

 collection 指定要遍历的集合
	         list类型的参数会特殊处理封装在map中,map的key就叫list

根本原因就是他们根本就没有理解foreach里面的collection应该放什么东西,错误的理解里面放的是java.util.List,其实这个类型应该是和我们的@Param绑定的参数名一致

<strong><!--public void addEmps(@Param("emps")List<Employee> emps);  --></strong>

如果前面加上了别名,那么collection就要用别名emps,,,,否则就默认list

发布了89 篇原创文章 · 获赞 1 · 访问量 770

猜你喜欢

转载自blog.csdn.net/zhupengqq1/article/details/103990781
今日推荐