Fork me on GitHub

mybatis动态sql之foreach补充(二)

对于入参为数组:

    public List<Employee> getEmpByForeach3(Integer [] ids);

我们使collection="array"

    <select id="getEmpByForeach3" resultType="com.gong.mybatis.bean.Employee">
        select * from tbl_employee where id in
        <foreach collection="array" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </select>

并直接利用#{}接直接获取对应的值。

            Integer [] arr = {1,2,3};
            List<Employee> es = mapper.getEmpByForeach3(arr);
            for(Employee e:es) {
                System.out.println(e);
            }

输出:

DEBUG 01-21 16:31:10,332 ==>  Preparing: select * from tbl_employee where id in ( ? , ? , ? )   (BaseJdbcLogger.java:145) 
DEBUG 01-21 16:31:10,371 ==> Parameters: 1(Integer), 2(Integer), 3(Integer)  (BaseJdbcLogger.java:145) 
DEBUG 01-21 16:31:10,433 <==      Total: 3  (BaseJdbcLogger.java:145) 
Employee [id=1, lastName=dema, gender=1, email=dema@qq.com, dept=null]
Employee [id=2, lastName=jack, gender=1, email=675544321@qq.com, dept=null]
Employee [id=3, lastName=小红, gender=0, email=xiaohong@qq.com, dept=null]
posted @ 2020-01-21 16:32  西西嘛呦  阅读(232)  评论(0编辑  收藏  举报