mybatis 注解 批量更新

一、前端代码

toShare:function() {
        //判断选中状态
        var ids ="";
        //判断选中状态
        var num = 0;
        $(".checkbox").each(function () {
            if($(this).is(':checked')){
                ids +=$(this).val() + ",";
                num++;
            }
        });
        if(num <= 0){
            toastr.error('请选择要共享的文件!');
            return false;
        }
        ids = ids.slice(0,ids.length-1);
        //共享
        $.ajax({
            cache: false,
            type: "post",
            url:backbasePath+'/apia/v1/file/shareFile',
            dataType:'json',
            data:{
                token:$("#token").val(),
                id:ids,
            },
            async: true,
            success: function(data) {
                if('000000'==data.code){
                    toastr.success(data.msg);
                    // 上传成功之后进行table的重新加载
                    $('#filesList').DataTable().ajax.reload();
                    // 设置不选中状态
                    $("#checkBoxMaster").prop("checked",false);
                } else if ('900000' == data.code) {
                    toastr.error('共享失败!');
                } else {
                    toastr.error(data.msg);
                }
            },
            error: function () {
                toastr.error('共享失败!');
            }
        });
    }

二、后端代码

1、业务逻辑层

   // 共享文件
    @Transactional
    public String shareFile(HttpServletRequest req){
        String result="";
        try{
            Map<String, Object> m = getMaps(req);
            log.info("|" + m + "|");
            // 获取选中的id
            String ids=m.get("id").toString();
            //将获取到的选中的列表封装在list中
            List<String> list = new ArrayList<String>();
            String[] stIds = ids.split(",");
            for (String value : stIds){
                list.add(value);
            }
            int number =knowledgeDao.updateById(list);
            if(number > 0){
                result= RequestResponseTool.getJsonMessage(RespCode.commonSucc, RespMsg.commonSucc);
            }
         }catch(Exception e){
            rollBack(e,log);
            result= RequestResponseTool.getJsonMessage(RespCode.commonFail, RespMsg.commonFail);
        }
        return result;
    }

2、持久层

    /**
     * 共享文件
     * * @return
     */
    @Update("<script>" +
            " update dzj_knowledge_resource_info set is_public='1' where id in " +
            " <foreach collection='ids' item='id' index='index' open='('  separator=',' close=')' >" +
            " #{id}" +
            " </foreach>" +
            "</script>")
    int updateById(@Param("ids")List<String> ids);

猜你喜欢

转载自www.cnblogs.com/flyShare/p/12518378.html
今日推荐