MyBatis 批量新增

2020-08-17  本文已影响0人  码天下

1.新增:--返回值为增加的的行数

int insertLableBatch(@Param("list")List<UserGroupFilterLabel> list);

SQL语句

<insert id="insertLableBatch"  parameterType="com.sf.cps.mana.model.UserGroupFilterLabel">
  insert into ti_user_group_filter_label
  (emp_group_id,label_type,label_code,label_name,label_symbol,label_content,label_valid,create_name,create_emp,create_time)
  values
  <foreach collection="list" item="re" separator=",">
    (
    #{re.empGroupId,jdbcType=INTEGER},
    #{re.labelType,jdbcType=VARCHAR},
    #{re.labelCode,jdbcType=VARCHAR},
    #{re.labelName,jdbcType=VARCHAR},
    #{re.labelSymbol,jdbcType=VARCHAR},
    #{re.labelContent,jdbcType=VARCHAR},
    #{re.labelValid,jdbcType=INTEGER},
    #{re.createName,jdbcType=VARCHAR},
    #{re.createEmp,jdbcType=VARCHAR},
    now()
    )
  </foreach>
</insert>

2修改:

int updateLableBatch(@Param("list")List<UserGroupFilterLabel> list);

SQL语句

<!--批量修改-跟java循环其实是一样的只是逻辑清晰些-->
<update id="updateLableBatch" parameterType="com.sf.cps.mana.model.UserGroupFilterLabel">
  <foreach collection="list" separator=";" item="item">
    update ti_user_group_filter_label
    <set>
      <if test="item.empGroupId != null">
        emp_group_id = #{item.empGroupId,jdbcType=INTEGER},
      </if>
      <if test="item.labelType != null">
        label_type = #{item.labelType,jdbcType=VARCHAR},
      </if>
      <if test="item.labelCode != null">
        label_code = #{item.labelCode,jdbcType=VARCHAR},
      </if>
      <if test="item.labelName != null">
        label_name = #{item.labelName,jdbcType=VARCHAR},
      </if>
      <if test="item.labelSymbol != null">
        label_symbol = #{item.labelSymbol,jdbcType=VARCHAR},
      </if>
      <if test="item.labelContent != null">
        label_content = #{item.labelContent,jdbcType=VARCHAR},
      </if>
      <if test="item.labelValid != null">
        label_valid = #{item.labelValid,jdbcType=INTEGER},
      </if>
      <if test="item.modifyEmp != null">
        modify_emp = #{item.modifyEmp,jdbcType=VARCHAR},
      </if>
      <if test="item.modifyName != null">
        modify_name = #{item.modifyName,jdbcType=VARCHAR},
      </if>
      modify_time = now()
    </set>
    where id = #{item.id,jdbcType=INTEGER}
  </foreach>
</update>

3.批量删除

/**
* 
* 方法描述:根据ids集合进行删除
* @return
*/
int deleteByIds(List<Integer> ids);
二、Mapper.xml动态sql
collection:传递来的参数,可以是list,array(数组),还可以是map的key,可以是pojo中的属性
         item:循环中的当前元素
         index:当前元素的下标
         open:循环的开始
         close:循环的结束
         separator:分隔符

<delete id="deleteByIds" parameterType="list">
    delete from user where id in
    <foreach collection="list" item="id" open="(" close=")" separator=",">
        #{id}
    </foreach>
</delete>
这段SQL最终会被自动组合成:delete from user where id in ( ? , ? ) 
上一篇 下一篇

猜你喜欢

热点阅读