Study Notes * Realization of Batch Batch Delete Function

Study Notes * Realization of Batch Batch Delete Function


Foreword: The realization idea is based on the connection method of using C3P0Util database.


------Add a column to the front of the original form of jsp--------
    such as: <td style="cursor:hand"; width="10%" ; align="center"> in table of contents column
              <input type="checkbox" id="ckAll" onclick="checkAll()" />
        </td>

     <c:forEach items="${books }" var= "b">

        <td style="cursor:hand"; width="10%"; align="center"> in the display data column
              <input type="checkbox" name="ids" value="${b. id }" />

        </td>

    </c>


---js implementation of selection function ideas---------

    1. Use click event to call checkAll()

    2. What should be done in the method?
         Get the chAll element, get its selected state,
         get the element ids of all the checkboxes,
         loop each checkbox, and assign a value to it (that is, the selected state value of ckAll)


-------------- --js implements the function process --------

   such as: //select all/unselect all
       function checkAll(){
           //get the ckAll element and get its selected state
           var flag = document.getElementById("ckAll ").checked;
           //get all ids checkbox elements
           var ids = document.getElementByName("ids");
           //loop assigning each checkbox value
           for(var i=0; i<ids.length; i++){
               ids[i].checked = flag; //Assign the state of the ckAll element to each element of ids[i]
           }
       }

----------------Batch delete function implementation--------
   steps:
       1. Add a button (value: batch delete) to the jsp form page, the code is as follows:
          
          <button type="button" id="del" name="del" value="batch delete" onclick="delAllBooks()">batch delete
               
          </button>


       2. Implement delAllBooks() method in js file
          //batch Delete
          function delAllBooks(){
             //Get the elements of all checkboxes
             var ids = document.getElementsByName("ids");
             var str=""; 
             //loop to get the ids of the selected checkboxes
             for(var i=0 ; i<ids.length; i++){
                if(ids[i].checked){
                    str+="ids="+ids[i].value+"&"; //Get the value of the selected id and pass it to delAllBooksServlet
                }
             }
              str = str.substring(0, str.length-1); //Intercept all strings and assign them to str again, the effect is: ids=1001&ids=1002&ids=1003
            
              if(str != "")
                   location.href=" ${pageContext.request.contextPath }/delAllBooksServlet?"+str;
          }

       3. To process batch deletion, create a new service class DelAllBooksServlet, the code is as follows:

          //Get all ids
          String[] ids = request.getParameterValues("ids") ; //ids is the name of the check box of the form, which is actually the associated id
          //Call the delete business
         BookServiceImpl bs = new BookServiceImpl();
         bs.deleAllBooks(ids);
          //Distribution redirection
          //The relative path
         request is used. getRequestDispatcher("bookListServlet").forward(request, response);

       4. Process the service layer, create a new method deleAllBooks in the business layer, and call the delete method deleAllBooks() of the data access layer. The code is as follows:
          
          public void deleAllBooks(String[] ids){
               bookDao.deleAllBooks(ids);
          }

       5. Process business Layer, create a new method deleAllBooks in the data access layer, specifically implement the delete method deleAllBooks(), the code is as follows:

          //Batch delete
          public void deleAllBooks(String[] ids){
               QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
               Object [][] params = new Object[ids.length][];
               for(int i=0; i<params.length; i++){
                   params[i] = new Object[]{ids[i]}; // The loop assigns a value to each element in the one-dimensional array, the value is id
               }               
               String sql = "delete from book where id=?";
               qr.batch(sql, params);
          }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324649291&siteId=291194637