- 编辑商品信息回显
- 完成service层业务
- 在ProductInfoService中创建service根据id查询数据的接口
ProductInfo getByID(int id);
-
实现上面的接口
@Override public ProductInfo getByID(int id) { return productInfoMapper.selectByPrimaryKey(id); }
- 在ProductInfoService中创建service根据id查询数据的接口
- 完成ProductInfoAction类中的方法
@RequestMapping("/one") public String one(int pid, Model model){ ProductInfo info = productInfoMapper.getByID(pid); model.addAttribute("prod",info); return "update"; } - 完成jsp页面回显数据的请求
在update.jsp页面的编辑点击事件中
function one(pid, ispage) { location.href = "${pageContext.request.contextPath}/prod/one.action?pid=" + pid + "&page=" + ispage; }
- 完成service层业务
- 商品更新功能
-
在ProductInfoService业务层中完成保存商品的接口创建
int update(ProductInfo info);
-
创建对应的商品实现类
@Override public int update(ProductInfo info) { return productInfoMapper.updateByPrimaryKey(info); } -
完成ProductInfoAction更新商品的控制器方法编写
@RequestMapping("/update") public String update(ProductInfo info,HttpServletRequest request){ // 对重新上传图片信息进行处理,如果上传图片就重新赋值 if(!saveFileName.equals("")){ info.setpImage(saveFileName); } int num = -1; try { num = productInfoMapper.update(info); } catch (Exception e) { e.printStackTrace(); } if(num > 0){ request.setAttribute("msg","商品数据更新成功"); }else { request.setAttribute("msg", "商品信息更新失败"); } // 再次清空文件上传变量为空 saveFileName = ""; return "redirect:/prod/split.action"; }- 启动项目测试
-
- 删除单个商品信息
-
点击表格数据的删除按钮事件
//单个删除 function del(pid) { if (confirm("确定删除吗")) { //向服务器提交请求完成删除 // window.location="${pageContext.request.contextPath}/prod/delete.action?pid="+pid; $.ajax({ url:"${pageContext.request.contextPath}/prod/delete.action", data:{"pid":pid}, type:"post", dateType:"text", success: function(msg){ // 重新刷新局部表格数据 alert(msg); $("#table").load("http://localhost:8080/admin/product.jsp #table"); } }) } } -
完成Service的接口创建
int delete(int pid);
-
创建对应的接口实现类方法
@Override public int delete(int pid) { return productInfoMapper.deleteByPrimaryKey(pid); } -
完成删除商品的控制器开发
ajax异步请求数据都会使用@注解声明,但是当前这个删除的方法不可以使用,因为删除成功之后是需要跳转到分页数据查询的页面的,如果使用了注解进行声明,那么就无法进行页面的跳转了
@RequestMapping("/delete") public String delete(int pid,HttpServletRequest request){ int num = -1; try { num = productInfoMapper.delete(pid); } catch (Exception e) { e.printStackTrace(); } // 进行成功的提示 if(num > 0){ request.setAttribute("msg","商品数据删除成功"); }else { request.setAttribute("msg", "商品删除失败"); } // 删除结束后跳转ajax分页处理 return "deleteAjaxSplit"; } @ResponseBody @RequestMapping(value = "/deleteAjaxSplit",produces = "text/html;charset=UTF-8") public Object deleteAjaxSplit(HttpServletRequest request){ // 获取第一页的分页数据 PageInfo info = productInfoMapper.splitPage(1,PAGE_SIZE); request.getSession().setAttribute("info",info); return request.getAttribute("msg"); }在上面也有一个ajaxsplit的分页查询,但是我们删除后的查询是需要有返回msg的所以上面的分页查询方法不满足我们的需求,所以重新写了一个重新带返回的方法,msg的作用就是删除成功后的弹框提示
对于返回的数据是中文的格式,需要一定要使用produces = "text/html;charset=UTF-8"进行一个中文编码格式转换
-
启动项目测试删除功能
-
- 批量删除商品数据
-
全选按钮功能实现
1、当点击顶部的复选框按钮的时候,下面的五条数据复选框也会被选中状态
function allClick() { //取得全选复选框的选中未选 中状态 var ischeck=$("#all").prop("checked"); //将此状态赋值给每个商品列表里的复选框 $("input[name=ck]").each(function () { this.checked=ischeck; }); } -
其余商品数据复选框的状态变化
当商品数据的五个复选框中有一个没有选中的,那么全选的复选框状态就要去掉,当五个商品的复选框都是选中的那么就要将全选选中
// 商品单个复选框点击事件 function ckClick() { //取得所有name=ck的被选中的复选框长度 var length=$("input[name=ck]:checked").length; //取得所有name=ck的复选框的长度 var len=$("input[name=ck]").length; //比较 if(len == length){ $("#all").prop("checked",true); }else { $("#all").prop("checked",false); } } - 批量删除mapper层实现
在ProductInfoMappermapper层的接口中添加批量删除的接口方法
int deleteBatch(String[] ids);
在ProductInfoMapper.xml文件中生成对应的映射文件,完成sql语句的编写
delete from product_info where p_id in #{pid} -
批量删除的Service层业务实现
创建接口
int deleyeBatch(String []ids);
完成实现方法
@Override public int deleyeBatch(String[] ids) { return productInfoMapper.deleteBatch(ids); } -
批量删除的Controller层业务逻辑实现
@RequestMapping("/deleteBatch") public String deleteBatch(String pids,HttpServletRequest request){ // 将页面传递过来的id通过逗号进行拆分成字符串数组pids[1,2,3] String[] ps = pids.split(","); try { int num = productInfoMapper.deleyeBatch(ps); if(num > 0){ request.setAttribute("msg","商品批量删除成功"); }else{ request.setAttribute("msg", "商品批量删除失败"); } } catch (Exception e) { request.setAttribute("msg","商品信息不可删除"); } // 一定要注意跳转的路径 return "forward:/prod/deleteAjaxSplit"; }重点注意删除成功后的页面跳转路径
-
前端页面的点击批量删除按钮事件的判断和请求
//批量删除 function deleteBatch() { //取得所有被选中删除商品的pid var zhi=$("input[name=ck]:checked"); var str=""; var id=""; if(zhi.length==0){ alert("请选择将要删除的商品!"); }else{ // 有选中的商品,则取出每个选 中商品的ID,拼提交的ID的数据 if(confirm("您确定删除"+zhi.length+"条商品吗?")){ //拼接ID $.each(zhi,function (index,item) { id=$(item).val(); //22 33 if(id!=null) str += id+","; //22,33,44 }); // alert(str+"11111111"); //发送请求到服务器端 $.ajax({ url:"${pageContext.request.contextPath}/prop/deletebatch", data: {"pids":str}, type:"post", dateType:"text", success: function (msg){ alert(msg); // 将页面上的显示商品数据的容器重新加载 $("#tabel").load("http://localhost:8080/admin/product.jsp #table"); } }); } } } -
启动项目测试
对于当前商品数据删除之后又会回到第一页的数据查询,那么对使用者来说是一个很不友好的时候,因为当你去到其他页的时候删除数据还得返回去查看看到是否有删除或者修改时也是一样的【后面再对项目进行一个修改】
-



