SSSP项目-CRUD的实现

  • A+
所属分类:JAVA

CRUD的实现

1.新增,编辑,修改员工信息

基本可以通过一个模板ModelAttribute的来实现新增,修改,和保存

Service层实现

  1. /**
  2.      * 保存员工
  3.      * @param employee
  4.      */
  5.     @Transactional(readOnly = false)
  6.     public void saveEmployee(Employee employee) {
  7.         //新增员工的情况
  8.         if(employee.getId() == null) {
  9.             employee.setCreateTime(new Date());
  10.         }
  11.         employeeRepository.saveAndFlush(employee);
  12.     }
  13.     /**
  14.      * 获取员工
  15.      * @param id
  16.      * @return
  17.      */
  18.     @Transactional(readOnly = true)
  19.     public Employee getEmployee(int id) {
  20.         return employeeRepository.findOne(id);
  21.     }

Controller层实现

  1. /**
  2.      * 员工编辑
  3.      * @return
  4.      */
  5.     @RequestMapping(value="/empedit/{id}",method=RequestMethod.GET)
  6.     public String empedit(@PathVariable("id") Integer id,Map<String, Object> map) {
  7.         Employee employee = new Employee();
  8.         if(id != null) {
  9.             employee = employeeService.getEmployee(id);
  10.         }
  11.         map.put("employee", employee);
  12.         //所有员工
  13.         map.put("employees", employeeService.getAll());
  14.         //所有部门
  15.         map.put("depts", deptService.getAll());
  16.         return "employee/edit";
  17.     }
  18.     /**
  19.      * 员工保存
  20.      * @param map
  21.      * @return
  22.      */
  23.     @RequestMapping(value="/empsave",method=RequestMethod.POST)
  24.     public String empsave(Employee employee) {
  25.         //保存员工信息
  26.         employeeService.saveEmployee(employee);
  27.         //跳转到员工列表
  28.         return "redirect:/emplistpage";
  29.     }
  30.     /**
  31.      * 新增员工
  32.      * @param map
  33.      * @return
  34.      */
  35.     @RequestMapping("/empadd")
  36.     public String empadd(Map<String, Object> map) {
  37.         //放置空的employee
  38.         map.put("employee"new Employee());
  39.         //所有员工
  40.         map.put("employees", employeeService.getAll());
  41.         //所有部门
  42.         map.put("depts", deptService.getAll());
  43.         return "employee/edit";
  44.     }
  1. /**
  2.      * 模板方法
  3.      * @param id
  4.      * @param map
  5.      */
  6.     @ModelAttribute
  7.     public void getEmployee(@RequestParam(value="id",required=false)Integer id,Map<String, Object> map) {
  8.         if(id != null) {
  9.             Employee employee = employeeService.getEmployee(id);
  10.             employee.setDept(null);
  11.             map.put("employee", employee);
  12.         }
  13.     }

jsp页面

  1. <div class="container">
  2.         <!-- 路径导航 -->
  3.         <ol class="breadcrumb">
  4.           <li><a href="<%=path %>">首页</a></li>
  5.           <li><a href="<%=path %>/emplistpage">员工列表</a></li>
  6.           <li class="active">员工信息编辑</li>
  7.         </ol>
  8.         <h3>员工信息</h3>
  9.         <form:form action="${pageContext.request.contextPath }/empsave" method="POST" modelAttribute="employee" >
  10.           <div class="form-group">
  11.             <label for="">序号:</label>
  12.             <input class="form-control" name="id" value="${employee.id }" readonly="readonly">
  13.           </div>
  14.           <div class="form-group">
  15.             <label for="">姓名</label>
  16.             <form:input class="form-control" path="name" placeholder="姓名" />
  17.           </div>
  18.           <div class="form-group">
  19.             <label for="">电子邮箱</label>
  20.             <form:input class="form-control" path="email" placeholder="电子邮箱" />
  21.           </div>
  22.           <div class="form-group">
  23.             <label for="">出生日期</label>
  24.             <input type="date" class="form-control" name="birth" value="${employee.birth }" data-date-format="YYYY-MM-DD"/>
  25.           </div>
  26.           <div class="form-group">
  27.             <label for="">进入公司时间</label>
  28.             <input type="date" class="form-control" name="entryTime" value="${employee.entryTime }" data-date-format="YYYY-MM-DD"/>
  29.           </div>
  30.           <div class="form-group">
  31.             <label for="">直接主管</label>
  32.             <form:select class="form-control" path="supervisor.id" items="${employees }" itemLabel="name" itemValue="id"></form:select>
  33.           </div>
  34.           <div class="form-group">
  35.             <label for="">薪水</label>
  36.             <form:input class="form-control" path="salary"/>
  37.           </div>
  38.           <div class="form-group">
  39.             <label for="">所属部门</label>
  40.             <form:select class="form-control" path="dept.id" items="${depts }" itemLabel="deptName" itemValue="id"></form:select>
  41.           </div>
  42.           <button type="submit" class="btn btn-default">Submit</button>
  43.         </form:form>
  44.     </div>

2.删除员工的处理

删除员工通过JavaScript来实现,在于员工列表同一个页面

service层

  1. /**
  2.      * 删除员工
  3.      * @param id
  4.      */
  5.     @Transactional(readOnly = false)
  6.     public void deleteEmployee(Integer id) {
  7.         employeeRepository.delete(id);
  8.     }

Controller层

  1. /**
  2.      * 员工删除
  3.      * @param id
  4.      * @return
  5.      */
  6.     @ResponseBody
  7.     @RequestMapping(value="/empdel/{id}",method=RequestMethod.POST)
  8.     public String empdel(@PathVariable("id")Integer id) {
  9.         if(id != null) {
  10.             employeeService.deleteEmployee(id);
  11.             //成功
  12.             return "1";
  13.         }
  14.         //失败
  15.         return "0";
  16.     }

jsp页面

  1. <!-- deleteModal删除模块 -->
  2.     <div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  3.       <div class="modal-dialog" role="document">
  4.         <div class="modal-content">
  5.           <div class="modal-header">
  6.             <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
  7.             <h4 class="modal-title" id="myModalLabel">删除确认</h4>
  8.           </div>
  9.           <div class="modal-body">
  10.                 <h4 id="deleteText"></h4>
  11.           </div>
  12.           <div class="modal-footer">
  13.             <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
  14.             <button type="button" class="btn btn-danger" id="delbutton" >删除</button>
  15.           </div>
  16.         </div>
  17.       </div>
  18.     </div>
  19.     <!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery,所以必须放在前边) -->
  20.     <script src="<%=path %>/jquery/jquery-3.5.1.min.js"></script>
  21.     <!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
  22.     <script src="<%=path %>/bootstrap/bootstrap.min.js"></script>
  23.     <script type="text/javascript">
  24.         //显示名称
  25.         $('#deleteModal').on('show.bs.modal', function (event) {
  26.           var button = $(event.relatedTarget);
  27.           var name = button.data('name');
  28.           var id = button.data('id');
  29.           var modal = $(this);
  30.           modal.find('#deleteText').text("真的要删除"+name+"吗?");
  31.           //删除事件
  32.           $("#delbutton").click(function (){
  33.                 var url = "${pageContext.request.contextPath }/empdel/"+id;
  34.                 var args = {"date":new Date()};
  35.                 $.post(url,args,function (data){
  36.                     if(data == "1"){
  37.                         modal.find('#deleteText').text("删除成功,2秒后页面自动刷新");
  38.                         setTimeout(function(){
  39.                             window.location.reload();//2秒刷新当前页面.
  40.                             },2000)
  41.                     }else{
  42.                         modal.find('#deleteText').text("删除失败,请稍后再试");
  43.                     }
  44.                 });
  45.           });
  46.         });
  47.     </script>

 

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: