Mybatis之foreach和sql标签

     * foreach
     * collection:设置需要循环的数组或数组
     * item:表示数组或集合中的每一个数据
     * separator:循环体之间的分隔符
     * open:foreach标签所循环的所有内容的开始符
     * close:foreach标签所循环的所有内容的结束符

2.通过foreach进行批量的删除操作

 <!--int testDelete(@Param("eids") Integer[] eids);-->
    <delete id="testDelete">
        delete from t_emp where eid in
        <foreach collection="eids" item="eid" separator="," open="(" close=")">
            #{eid}
        </foreach>
    </delete>

3.通过foreach进行批量添加的操作

    <!--int testInsert(List<Emp> emps);-->
    <insert id="testInsert">
        insert into t_emp values
        <foreach collection="emps" item="emp" separator=",">
            (null,#{emp.empName},#{emp.age},#{emp.sex},#{emp.email},null)
        </foreach>
    </insert>

4.sql标签可以定义查询的哪些内容可以显示出来

    <sql id="empcolumn">eid,emp_name,age,sex,email</sql>
    <select id="getEmpByCondition" resultType="emp">
        select <include refid="empcolumn"></include> from t_emp
    </select>
全部评论

相关推荐

在写周报的打工人很独...:这个笔试昨天晚上做了一下,真难啊,前后端,ai全有
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务