Mybatis 实现批量插入、批量插入——<foreach>标签的运用

需要加上参数类型parameterType。

<foreach>标签中的 collection为所遍历的类的别名,item为单个遍历单位名,separator为分隔符,分隔符一定不能用分号,会出错!

<foreach>标签外需加上外层判断标签<test>来确保list不为空

  <insert id="insertDuo" parameterType="arraylist">
        insert into eng_person_record(
        PK_ID,PROJECT_ID,PERSON_ID,
        IN_DATE,OUT_DATE
        )
        values
        <foreach collection="list" item="item" separator=",">
            (
            #{item.pkId},#{item.projectId},#{item.personId},#{item.inDate},#{item.outDate}
            )
        </foreach>
  </insert>

 

  <select id="getnamebyids" resultType="java.lang.String" parameterType="arraylist">
        select `name`
        from emergency_org
        where   
        1=1
        <if test="ids!= null and ids.size>0">
        and id in
        <foreach collection="ids" index="index" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>        

 

posted @ 2021-07-13 14:24  onecyl  阅读(462)  评论(0编辑  收藏  举报