• 会议信息管理系统SSM记录(四)



    目录:

    (1)部门管理:查询-添加-删除

    (2)部门管理:修改


    (1)部门管理:查询-添加-删除

    在DepartmentController中添加方法:

    这里getAllDeps方法上篇已写 

    1. //获取所有部门请求,并跳转到部门页面
    2. @RequestMapping("/departments")
    3. public String departments(Model model){
    4. model.addAttribute("deps",departmentService.getAllDeps());
    5. return "departments";
    6. }

     删除固定的内容:

     给添加部门form表单添加action:

    在DepartmetntController中提那家这个接口:

    1. /**
    2. * 增加部门
    3. *
    4. * @param departmentname 部门名称
    5. * @return 重定向到获取所有部门的页面
    6. */
    7. @PostMapping("/addDepartments")
    8. public String addDepartments(String departmentname,Model model){
    9. Department depByName = departmentService.getDepByName(departmentname);//根据名字查询部门
    10. if (depByName != null){
    11. model.addAttribute("error","部门名字重复");
    12. }else {
    13. departmentService.addDepartments(departmentname);
    14. model.addAttribute("error","部门添加成功!!");
    15. }
    16. return "redirect:/admin/departments";
    17. }

     在DepartmentService:添加这个方法:

    1. //根据名字查询部门
    2. public Department getDepByName(String departmentname) {
    3. return departmentMapper.getDepByName(departmentname);
    4. }
    1. //增加部门
    2. public Integer addDepartments(String departmentname) {
    3. return departmentMapper.addDepartments(departmentname);
    4. }

     在添加这个方法:

     DepartmentMapper:添加最后两个方法

    1. //根据名字查询部门
    2. Department getDepByName(String departmentname);
    1. //增加部门
    2. Integer addDepartments(String departmentname);

    DepartmentMapper.xml写两个sql语句:

    1. <!--根据名字查询部门-->
    2. <select id="getDepByName" resultType="com.xzb.meeting.model.Department">
    3. select *
    4. from department
    5. where departmentname = #{departmentName}
    6. </select>
    1. <insert id="addDepartments">
    2. insert into department(departmentname)
    3. values (#{departmentName})
    4. insert>

    给删除按钮添加:删除请求

     在DepartmentController添加删除请求:

    1. /**
    2. * 根据id删除部门
    3. *
    4. * @param departmentid 部门编号
    5. * @return Integer
    6. */
    7. @RequestMapping("/deletedep")
    8. public String deletedep(Integer departmentid){
    9. departmentService.deletedepById(departmentid);
    10. return "redirect:/admin/departments";
    11. }

     在DepartmentService添加删除方法:

    1. //根据id删除部门
    2. public Integer deletedepById(Integer departmentid) {
    3. return departmentMapper.deletedepById(departmentid);
    4. }

     在DepartmentMapper添加:

    1. //根据id删除部门
    2. Integer deletedepById(Integer departmentid);

     DepartmentMapper.xml添加sql语句:

    1. <!--根据id删除部门-->
    2. <delete id="deletedepById">
    3. delete
    4. from department
    5. where departmentid = #{departmentId}
    6. </delete>

    点击222删除 

     (2)部门管理:修改

    修改部门页面:添加id onclick

    1. <tr>
    2. <td>${dep.departmentId}</td>
    3. <td id="depname${dep.departmentId}">${dep.departmentName}</td>
    4. <td>
    5. <a class="clickbutton" href="#" id="edit${dep.departmentId}" onclick="editDep(${dep.departmentId})">编辑</a>
    6. <a class="clickbutton" style="display: none" href="#" id="cancel${dep.departmentId}" onclick="cancelDep(${dep.departmentId})">取消</a>
    7. <a class="clickbutton" href="/admin/deletedep?departmentid=${dep.departmentId}">删除</a>
    8. </td>
    9. </tr>

    添加js:

    1. <script>
    2. //定义变量
    3. var depname;
    4. //编辑的方法
    5. function editDep(depid) {
    6. var editBtn = $('#edit'+depid);//编辑
    7. var cancelBtn = $('#cancel'+depid);//取消
    8. var ele = $('#depname' + depid);//行的id
    9. depname = ele.html();
    10. if (cancelBtn.css('display') == 'none'){
    11. cancelBtn.css('display','inline');
    12. editBtn.html('确定');//点击编辑按钮按后,编辑按钮变为确定
    13. var depName = ele.text();//获取到里面的内容
    14. //ele变为输入框
    15. ele.html('+depName+'"/>')
    16. }else {
    17. var children = ele.children('input');//获取input的元素
    18. var val = children.val();//获取输入的值
    19. $.post('/admin/updatedep',{id:depid,name:val},function (msg) {
    20. if (msg == 'success'){
    21. cancelBtn.css('display','none');//隐藏取消按钮
    22. editBtn.html('编辑');
    23. ele.html(val);
    24. }
    25. })
    26. }
    27. }
    28. //取消的方法
    29. function cancelDep(depid) {
    30. var editBtn = $('#edit' + depid);
    31. var cancelBtn = $('#cancel' + depid);
    32. var ele = $('#depname' + depid);
    33. cancelBtn.css('display','none');//点击取消取消按钮display属性为none
    34. editBtn.html('编辑');//恢复称编辑
    35. ele.html(depname);
    36. }
    37. </script>

    更新请求:在DepartmentControlle添加更新请求

    1. /**
    2. * 更新部门
    3. *
    4. * @param id 部门编号
    5. * @param name 部门名称
    6. * @return success或者error给前端ajax
    7. */
    8. @RequestMapping("/updatedep")
    9. @ResponseBody
    10. public String updatedep(Integer id,String name){
    11. Integer result = departmentService.updatedep(id,name);
    12. if (result == 1){
    13. return "success";
    14. }
    15. return "error";
    16. }

    在DepartmentService添加方法:

    1. //更新部门
    2. public Integer updatedep(Integer id, String name) {
    3. return departmentMapper.updatedep(id,name);
    4. }

     在DepartmentMapper添加更新方法:

    1. //更新部门
    2. Integer updatedep(@Param("id") Integer id,@Param("name") String name);

    DepartmentMapper.xml添加sql语句:

    1. <update id="updatedep">
    2. update department
    3. set departmentname = #{name}
    4. where departmentid = #{id}
    5. update>

  • 相关阅读:
    js控制input数字输入[情况差不多全部考虑到了]
    Python——缩进和选择
    OpenMP编程-九点差分法求解泊松方程
    软件工程导论——第三章——需求分析
    Gateway微服务路由使微服务静态资源加载失败
    Python——字典数据存入excel
    第08章 中文分词
    【明年找到好工作】:面试题打卡第一天
    postgresql用户与权限管理
    保健品行业正在升级,你准备好了吗?
  • 原文地址:https://blog.csdn.net/dengfengling999/article/details/128003413