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);
3、控制器將對應的類型轉換
String[] orgIds = {};
if(orgUuid !=null && orgUuid.length() >0){
orgIds= orgUuid.split(",");
params.put("orgUuid", orgIds);
}else{
params.put("orgUuid", orgUuid);
}
4、xml文件
<if test="orgUuid != null and orgUuid != ''">
AND s.org_uuid in
<foreach collection="orgUuid" open="(" close=")" item="item" separator=",">
#{item}
</foreach>
</if>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM