前端选中ID批量删除后端数据完整步骤

数据库的表和数据

创建product表
CREATE TABLE `product` (
  `id` int(11) NOT NULL,
  `productNum` varchar(50) DEFAULT NULL,
  `productName` varchar(50) DEFAULT NULL,
  `cityName` varchar(50) DEFAULT NULL,
  `DepartureTime` datetime DEFAULT NULL,
  `productPrice` int(10) DEFAULT NULL,
  `productDesc` varchar(500) DEFAULT NULL,
  `productStatus` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

插入数据
insert  into `product`(`id`,`productNum`,`productName`,`cityName`,`DepartureTime`,`productPrice`,`productDesc`,`productStatus`)
 values (1,'qingfeng-001','北京三日游','北京','2019-10-14 11:52:52',1200,'不错的旅行',1),
 (2,'qingfeng-002','青岛三日游','青岛','2019-10-15 16:00:41',1500,'不错的旅行',1),
 (3,'qingfeng-003','广东三日游','广东','2019-10-08 16:00:45',1600,'不错的旅行',1),
 (4,'qingfeng-004','南京三日游','南京','2019-10-13 16:00:49',1200,'不错的旅行',1),
 (5,'qingfeng-005','上海三日游','上海','2019-10-11 16:00:54',3000,'不错的旅行',1),
 (6,'qingfeng-006','四川三日游','四川','2019-10-19 16:00:58',3200,'不错的旅行',1),
 (7,'qingfeng-007','天津三日游','天津','2019-10-10 16:01:02',2300,'不错的旅行',1),
 (8,'qingfeng-008','张家界三日游','张家界','2019-10-01 16:01:29',1300,'不错的旅行',1),
 (9,'qingfeng-009','深圳三日游','深圳','2019-11-18 16:01:25',2100,'不错的旅行',1);



表的数据如下图

jsp页面

 <!--数据列表-->
                        <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
                            <thead>
                            <tr>
                                <th class="" style="padding-right:0px;">
                                    <input id="selall" type="checkbox" class="icheckbox_square-blue">
                                </th>
                                <th class="sorting_asc">ID</th>
                                <th class="sorting_desc">编号</th>
                                <th class="sorting_asc sorting_asc_disabled">产品名称</th>
                                <th class="sorting_desc sorting_desc_disabled">出发城市</th>
                                <th class="sorting">出发时间</th>
                                <th class="text-center sorting">产品价格</th>
                                <th class="text-center sorting">产品描述</th>
                                <th class="text-center sorting">状态</th>
                                <th class="text-center">操作</th>
                            </tr>
                            </thead>
                            <tbody>
                           
                            <c:forEach var="product" items="${productList}">
                            <tr>
                                <td><input  id="delete_id" product_id="${product.id}"  name="ids" type="checkbox"></td>
                                <td>${product.id}</td>
                                <td>${product.productNum}
                                </td>
                                <td>${product.productName}</td>
                                <td>${product.cityName}</td>
                                <td>${product.departureTimeStr}</td>
                                <td class="text-center">${product.productPrice}</td>
                                <td class="text-center">${product.productDesc}</td>
                                <td class="text-center">${product.productStatusStr}</td>
                                <td class="text-center">
                                    
                                </td>
                            </tr>
                            </c:forEach>

                            </tbody>
                
                        </table>

JS代码

<script>

	//删除
	 $("#delete_productId").click(function() {
		 var delUrl = "${pageContext.request.contextPath}/product/delete.do?ids=";
		 var ids ="";
			//拿到要删除所有员工的id  $(".single_check:checked")表示选中框被选中的框
			$("#delete_id:checked").each(function(){
				//取出我们自定义的id    $(this)表示这全部选中的一个选中框
				ids += $(this).attr("product_id") + ",";
			});   
			//去除最后一个逗号
			ids = ids.substring(0,ids.length-1);
			alert(ids);
			//拼接路径和删除的id
			delUrl +=ids;
			//让浏览器访问删除链接
			if(confirm("确定删除["+ids+"]员工吗?")){
			location.href  = delUrl ;
			}
			return false;
		});
    
</script>

Controller层代码

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;

  //产品删除
    @RequestMapping("/delete.do")
    public String delete(@RequestParam(value = "ids", defaultValue = "") String ids) throws Exception {
        productService.delete(ids);
        return "redirect:findAll.do";
    }
}

 

IProductService接口

import java.util.List;

import com.qingfeng.domain.Product;

public interface IProductService {

    
	public void delete(String ids);
}

ProductServiceImpl 代码

@Service
@Transactional
public class ProductServiceImpl implements IProductService{

    @Autowired
    private IProductDao productDao;

	public void delete(String ids) {
		//判断ids是否包含,
		  if (ids.contains(",")) {
			  //根据,拆分得到一个字符串数组
	            String[] split = ids.split(",");
	            //创建一个集合
	            List<Integer> list = new ArrayList<Integer>();
	            //遍历split字符串数组
	            for (String s : split) {
	                int i = 0;
	                try {
	                	//将String字符类型数据转换为Integer整型数据
	                    i = Integer.parseInt(s);
	                } catch (NumberFormatException e) {
	                }
	                //将转换的整形数据添加到集合里
	                list.add(i);
	            } 
	            //遍历list集合,删除id所在的集合
	            for (Integer id : list) {
	            	 productDao.deleteId(id);
				}
	        }else {
	        	//删除只有一个id
	        	  productDao.deleteId(Integer.parseInt(ids));
	        }
	}
	
}

IProductDao接口

public interface IProductDao {

    //根据id删除
    @Delete("delete  from product where id=#{id}")
	public void deleteId(Integer id);

}

 

  • 5
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值