首页 > 其他 > 详细

Mybatis中使用foreach标签出现的错误

时间:2021-01-31 17:28:53      阅读:73      评论:0      收藏:0      [点我收藏+]

错误原因:

Cause: org.apache.ibatis.binding.BindingException: Parameter ‘ids‘ not found. Available parameters are [arg0, collection, list]

代码如下:

dao:

public List<Employee> getEmpsByConditionForeach(List<Integer> ids);

xml:

<select id="getEmpsByConditionForeach" resultType="com.fenga.mybatis.bean.Employee">
        select * from tbl_employee
         <!--
             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="where id in(" close=")">
             #{item_id}
         </foreach>
    </select>

Test:

@Test
    public void test1() throws IOException {
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        SqlSession openSession = sqlSessionFactory.openSession();
        
        try {
            EmployeeMapperDynamicSQL mapper = openSession.getMapper(EmployeeMapperDynamicSQL.class);
            
            List<Employee> list = mapper.getEmpsByConditionForeach(Arrays.asList(1,2,3));
            for (Employee emp : list) {
                System.out.println(emp);
            }
            
        }finally {
            openSession.close();
        }
        
    }

解决问题:

  Parameter ‘ids‘ not found. 意思是没找到ids这个集合,可以通过@Param给集合命名,如下:

public List<Employee> getEmpsByConditionForeach(@Param("ids")List<Integer> ids);

 结果如图

技术分享图片

 

Mybatis中使用foreach标签出现的错误

原文:https://www.cnblogs.com/LLFA/p/14352664.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!