mybatis批量操作

mybatis 批量操作
add

//mapper 接口
Integer add(@Param("ids") List<User> ids);
//sql
<insert id="add" >
        insert into
        user_info(
        id,
        name)
        values
        <foreach collection="ids" item="item" separator=",">
            (#{item.id},
            #{item.name})
        </foreach>
    </insert>

update

@Data
@ApiModel
public class BatchDTO implements Serializable {

    private static final long serialVersionUID = -2200320124239531727L;
    @ApiModelProperty("是否删除 0 删除 1正常")
    private Integer isDelete;

    @ApiModelProperty("1 启用 0禁用")
    private Integer states;
    @ApiModelProperty("id数组")
    private List<Integer> ids;
    
    }
    
int batchUpdate(BatchDTO batchDTO);
 <update id="batchUpdate">
        update user_info
        <set>
            <if test="isDelete !=null">
                is_delete=#{isDelete},
            </if>
            <if test="states !=null">
                states=#{states}
            </if>
        </set>
        where id in
        <foreach collection="ids" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </update>

你可能感兴趣的:(mybatis批量操作)