javaweb批量删除
时间: 2023-12-16 12:04:44 浏览: 138
以下是JavaWeb批量删除的示例代码:
1.在jsp页面中添加一个checkbox用于选择要删除的数据:
```html
<form action="deleteBatch" method="post">
<table>
<thead>
<tr>
<th><input type="checkbox" id="checkAll"></th>
<th>id</th>
<th>name</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach items="${list}" var="brand">
<tr>
<td><input type="checkbox" name="ids" value="${brand.id}"></td>
<td>${brand.id}</td>
<td>${brand.name}</td>
<td><a href="delete?id=${brand.id}">删除</a></td>
</tr>
</c:forEach>
</tbody>
</table>
<input type="submit" value="批量删除">
</form>
```
2.在servlet中获取选中的id并调用service层的方法进行批量删除:
```java
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String[] ids = request.getParameterValues("ids");
BrandService brandService = new BrandServiceImpl();
brandService.deleteByIds(ids);
response.sendRedirect(request.getContextPath() + "/list");}
```
3.在service层中实现批量删除的方法:
```java
@Override
public void deleteByIds(String[] ids) {
brandMapper.deleteByIds(ids);
}
```
4.在dao层中实现批量删除的方法:
```java
void deleteByIds(@Param("ids") String[] ids);
```
阅读全文