[SSM-SpringMVC] 07-Data echo, batch update, batch delete

1. Batch modification and update

  Batch modify multiple data of a table to achieve update, put the modified multiple data into a collection and send to the background call dao to achieve batch update.

1.1 Introduction and realization of data echo

  When updating data daily , we do not only use one SQL statement to achieve it. Generally, two SQL statements are used together , one SQL data is used to query the data to be updated, and then displayed on the interface, and then modify the data in this interface , Call another sql statement to update data.
  The key to data echo is to bring idquery data, and then display it on another page. The data echo page is generally xxxUI.jspexpressed like this

    <a href="${pageContext.request.contextPath}/方法路径?id=${item.id}">修改</a>

1.1.1 Front-end jsp page

<table border="1px" width="100%">
        <tr>
            <th>序号 </th>
            <th>用户名 </th>
            <th>密码 </th>
            <th>城市 </th>
            <th>操作 </th>
        </tr>
        <%--通过forEach循环,item是循环的当前对象,存放到作用域中,通过el表达式获取--%>
        <c:forEach items="${list}" var="item" varStatus="vs">
            <tr>
                <td>${vs.index}</td>
                <td>${item.username}</td>
                <td>${item.password}</td>
                <td>${item.city}</td>
                <%-- 数据回显,当点击修改按钮的时候,会调用方法查询当前id的信息,然后将信息回显出来供你修改 --%>
                <td><a href="${pageContext.request.contextPath}/updateInfo.action?id=${item.id}">修改</a> </td>
            </tr>
        </c:forEach>

    </table>

1.1.2 Query by parameter id, and then return to the xxxUIecho page

 @RequestMapping("updateInfo.action")
    public ModelAndView updatePersonInfo(int id){
    
    //id=1
        //1:模拟一下查询数据库
       Person person= Db.findById(id);
        //2:转发到页面
        ModelAndView mv = new ModelAndView();//对数据与页面的封装
        mv.addObject("person",person);

        mv.setViewName("updatePersonUI");// 视图解析器加前后缀WEB-INFO/jsp/list.jsp
        return mv;
    }

1.1.3 When the query is found, the echo page will be displayed and the modified information will be submitted.

The data is echoed, the date type cannot be displayed, and the text is modified first.

	   <form method="post" action="${pageContext.request.contextPath}/updatePerson.action">
           用户名: <input type="text" name="username"  value="${person.username}"/><br/>
           密码: <input type="text" name="password"  value="${person.password}"/><br/>
           城市: <input type="text" name="city"  value="${person.city}"/><br/>
            出生日期<input type="text" name="birthday"value="${person.birthday2}" /><br/>
            <input type="submit" value="更新"/><br/>
        </form>


1.2 Batch modify Person data

1.2.1 personClass

public class Person {
    
     
    private Integer id;
    private String username;
    private String password;
    //省略get和set方法
}

1.2.2 jsp page

		<%-- 表单 只会将多条记录放到一个list里面,提交到后台--%>
        <form method="post" action="${pageContext.request.contextPath}/updateList.action">
            <table>
                <c:forEach items="${PersonList}" var="person" varStatus="vs">
                    <tr>
                        <td><input type="hidden" value="${person.id}" name="list[${vs.index}].id" /></td>
                        <td><input type="text" value="${person.username}" name="list[${vs.index}].username" /></td>
                        <td><input type="text" value="${person.password}" name="list[${vs.index}].password" /></td>
                    </tr>
                </c:forEach>
            </table>
            <input type="submit" value="提交数据到后台" > <br/>
        </form>

1.2.3 Create a MyQueryOvclass in the background to receive a list of foreground parameterspersonLists

public class MyQueryOv {
    
    
	//接收前台传过来的批量修改的person数据集合
    private List<Person> personList;

    public List<Person> getList() {
    
    
        return list;
    }

    public void setList(List<Person> list) {
    
    
        this.list = list;
    }
}

1.2.4 ControllerBatch modification in the background

@RequestMapping("updateList.action")
    public ModelAndView test03(MyQueryOv ov){
    
    //参数写QueryOv类,但是页面要指定变量名与索引
        System.out.println(ov.getList());
        //进行批量更新操作 
        return null;
    }

2. Batch delete operation

  With 复选框the name值same label and check box , the background can receive data in two ways:

  1. Receive in array mode ( 这种方式,方法形式参数名要与复选框的name值保持一致)
  2. Create a new class to receive data. (The set method of the class will be called for assignment, so the attribute names must be consistent)
public class A{
    
    
     private 数据类型[] 变量名;
}

Front jsp page

Front jsp page

		<%--需要让复选框使用同一个name,页面会将选中的复选框的value值以数组形式提交给后台。
		程序需要将多个值,放到数组中,提交到后台
		这里后台接收到的数据,是value的值,不一定是你看到的
		【***】一般把value的值设置为id,后台通过id删除--%>
        <form method="post" action="${pageContext.request.contextPath}/deleteMany.action">
            <input type="checkbox" value="1" name="ids"/> 数据1<br/>
            <input type="checkbox" value="2" name="ids"/> 数据2<br/>
            <input type="checkbox" value="3" name="ids"/> 数据3<br/>
            <input type="submit" value="提交数据到后台" > <br/>
        </form>

2.1 Receiving and deleting through an array

The background receives through the array and calls the delete method

@Controller
public class Demo01Controller {
    
    
    @RequestMapping("delete1.action")
    public ModelAndView test01(Integer[] ids){
    
    //参数只需要写数组
        System.out.println(Arrays.toString(ids));
        //调用删除方法
        return null;
    }
}

2.2 Receive data and delete by creating a new class

2.2.1 Create a class that receives check box values

public class MyQueryOv {
    
    
    private Integer[] ids;

    public Integer[] getIds() {
    
    
        return ids;
    }

    public void setIds(Integer[] ids) {
    
    
        this.ids = ids;
    }
}

2.2.2 Call method

    @RequestMapping("delete2.action")
    public ModelAndView test02(MyQueryOv ov){
    
    //参数只需要写QueryOV类,会自动赋值(调用set方法赋值),参数名要一致
        System.out.println(Arrays.toString(ov.getIds()));
        return null;
    }

Guess you like

Origin blog.csdn.net/qq_40542534/article/details/109099811