当前位置:   article > 正文

SSM架构(二)

SSM架构(二)

接上一篇博客

SSM框架(一)-CSDN博客

2.4 Spring

2.4.1 Service设计

EmployeeService接口代码:

  1. List<Emp> search(Emp condition);
  2. Emp searchById(Integer id);
  3. boolean add(Emp emp);
  4. boolean update(Emp emp);
  5. boolean delete(Integer id);

EmployeeServiceImpl实现类代码:  

  1. @Service
  2. public class EmpServiceImpl implements EmpService {
  3. @Autowired
  4. EmpDao empDao;
  5. @Override
  6. public List<Emp> search(Emp condition) {
  7. List<Emp> list = empDao.search(condition);
  8. return list;
  9. }
  10. @Override
  11. public Emp searchById(Integer id) {
  12. Emp emp = empDao.searchById(id);
  13. return emp;
  14. }
  15. @Override
  16. public boolean add(Emp emp) {
  17. int rs = empDao.add(emp);
  18. return rs > 0;
  19. }
  20. @Override
  21. public boolean update(Emp emp) {
  22. int rs = empDao.update(emp);
  23. return rs > 0;
  24. }
  25. @Override
  26. public boolean delete(Integer id) {
  27. int rs = empDao.delete(id);
  28. return rs > 0;
  29. }
  30. }
DepartmentService 接口代码:
  1. public List<Dep> search();
  2. public Dep searchById(Integer id);
  3. public boolean add(Dep dep);
  4. public boolean update(Dep dep);
  5. public boolean delete(Integer id);
DepartmentServiceImpl 实现类代码:
  1. @Service
  2. public class DepServiceImpl implements DepService {
  3. @Autowired
  4. DepDao depDao;
  5. @Autowired
  6. EmpDao empDao;
  7. @Override
  8. public List<Dep> search() {
  9. List<Dep> list = depDao.search();
  10. return list;
  11. }
  12. @Override
  13. public Dep searchById(Integer id) {
  14. Dep dep = depDao.searchById(id);
  15. return dep;
  16. }
  17. @Override
  18. public boolean add(Dep dep) {
  19. int rs = depDao.add(dep);
  20. return rs > 0;
  21. }
  22. @Override
  23. public boolean update(Dep dep) {
  24. int rs = depDao.update(dep);
  25. return rs > 0;
  26. }
  27. @Override
  28. public boolean delete(Integer id) {
  29. int rs = depDao.delete(id);
  30. rs = empDao.updateByDep(id);
  31. return rs > 0;
  32. }
  33. }

 2.5 Spring MVC

Spring MVCDispatcherServlet的使用与配置,基于注解映射机制。

2.5.1 Controller设计

EmployeeController实现代码:

  1. @Controller
  2. @RequestMapping("emp")
  3. public class EmpController {
  4. @Autowired
  5. EmpService empService;
  6. @Autowired
  7. DepService depService;
  8. @RequestMapping("search")
  9. public ModelAndView search(Emp condition) {
  10. ModelAndView mv = new ModelAndView("emp/show");
  11. List<Emp> list = empService.search(condition);
  12. List<Dep> depList=depService.search();
  13. mv.addObject("list", list);
  14. mv.addObject("depList",depList);
  15. mv.addObject("c", condition);
  16. return mv;
  17. }
  18. @RequestMapping("showAdd")
  19. public ModelAndView showAdd() {
  20. ModelAndView mv = new ModelAndView("emp/add");
  21. List<Dep> depList = depService.search();
  22. mv.addObject("depList", depList);
  23. return mv;
  24. }
  25. @RequestMapping("add")
  26. public String add(Emp emp) {
  27. boolean flag = empService.add(emp);
  28. return "redirect:search";
  29. }
  30. @RequestMapping("showUpdate")
  31. public ModelAndView showUpdate(Integer id) {
  32. Emp emp = empService.searchById(id);
  33. List<Dep> depList = depService.search();
  34. ModelAndView mv = new ModelAndView("emp/update");
  35. mv.addObject("emp", emp);
  36. mv.addObject("depList", depList);
  37. return mv;
  38. }
  39. @RequestMapping(value="update")
  40. public String update(Emp emp) {
  41. boolean flag = empService.update(emp);
  42. return "redirect:search";
  43. }
  44. @RequestMapping("delete")
  45. public String delete(Integer id) {
  46. boolean flag = empService.delete(id);
  47. return "redirect:search";
  48. }
  49. }
DepartmentController 实现代码:
  1. @Controller
  2. @RequestMapping("dep")
  3. public class DepController {
  4. @Autowired
  5. DepService depService;
  6. @RequestMapping("search")
  7. public ModelAndView search() {
  8. ModelAndView mv = new ModelAndView("dep/show");
  9. List<Dep> list = depService.search();
  10. mv.addObject("list", list);
  11. return mv;
  12. }
  13. @RequestMapping("showAdd")
  14. public String showAdd() {
  15. return "dep/add";
  16. }
  17. @RequestMapping("add")
  18. public String add(Dep dep) {
  19. boolean flag = depService.add(dep);
  20. return "redirect:search";
  21. }
  22. @RequestMapping("showUpdate")
  23. public ModelAndView showUpdat(Integer id) {
  24. ModelAndView mv = new ModelAndView("dep/update");
  25. Dep dep = depService.searchById(id);
  26. mv.addObject("dep", dep);
  27. return mv;
  28. }
  29. @RequestMapping("update")
  30. public String update(Dep dep) {
  31. boolean flag = depService.update(dep);
  32. return "redirect:search";
  33. }
  34. @RequestMapping("delete")
  35. public String delete(Integer id) {
  36. boolean flag = depService.delete(id);
  37. return "redirect:search";
  38. }
  39. }

2.6 JSP(View)

2.6.1 emp

add.jsp

  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. pageEncoding="utf-8" %>
  3. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  4. <html>
  5. <head>
  6. <title></title>
  7. <style>
  8. #container {
  9. width: 700px;
  10. margin: 10px auto;
  11. }
  12. #container form .align {
  13. margin: 8px 0;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <div id="container">
  19. <form action="add">
  20. <div class="align">
  21. <label>编号</label>
  22. <input type="text" placeholder="请输入编号"
  23. name="number">
  24. </div>
  25. <div class="align">
  26. <label>名字</label>
  27. <input type="text" placeholder="请输入名字" name="name">
  28. </div>
  29. <div class="align">
  30. <label>性别</label>
  31. <input type="radio" value="男" name="gender"/>
  32. <input type="radio" value="女" name="gender"/>
  33. </div>
  34. <div class="align">
  35. <label>年龄</label>
  36. <input type="text" placeholder="请输入年龄" name="age">
  37. </div>
  38. <div class="align">
  39. <label>部门</label>
  40. <select name="dep.id">
  41. <c:forEach var="dep" items="${depList }">
  42. <option value="${dep.id }">${dep.name }</option>
  43. </c:forEach>
  44. </select>
  45. </div>
  46. <div class="align">
  47. <button type="submit">保存</button>
  48. </div>
  49. </form>
  50. </div>
  51. </body>
  52. </html>
show.jsp
  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. pageEncoding="utf-8" %>
  3. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  4. <html>
  5. <head>
  6. <title>员工展示</title>
  7. <style>
  8. #container {
  9. width: 700px;
  10. margin: 10px auto;
  11. }
  12. #container #search{
  13. overflow: hidden;
  14. }
  15. #container #search .align{
  16. float:left;
  17. margin-right:8px;
  18. }
  19. #container #search input{
  20. width:160px;
  21. }
  22. #container #data {
  23. clear: both;
  24. width: 700px;
  25. margin:10px 0;
  26. }
  27. </style>
  28. </head>
  29. <body>
  30. <div id="container">
  31. <form id="search" action="search" method="post">
  32. <div class="align">
  33. <input type="text" name="number"
  34. placeholder="编号" value=${c.number}>
  35. </div>
  36. <div class="align">
  37. <input type="text" name="name"
  38. placeholder="姓名" value=${c.name}>
  39. </div>
  40. <div class="align">
  41. <select name="gender">
  42. <option value="">性别</option>
  43. <option value="男" <c:if test="${c.gender =='男'}">
  44. selected</c:if>>男</option>
  45. <option value="女" <c:if test="${c.gender =='女'}">
  46. selected</c:if>>女</option>
  47. </select>
  48. </div>
  49. <div class="align">
  50. <input type="text" name="age" placeholder="年龄"
  51. value=${c.age!=null?c.age:''}>
  52. </div>
  53. <div class="align">
  54. <select name="dep.id">
  55. <option value="">部门</option>
  56. <c:forEach items="${depList}" var="dep">
  57. <option value="${dep.id }"
  58. <c:if test="${dep.id ==c.dep.id}">
  59. selected</c:if>>${dep.name }</option>
  60. </c:forEach>
  61. </select>
  62. </div>
  63. <div class="align">
  64. <button type="submit">搜索</button>
  65. </div>
  66. </form>
  67. <table id="data" border="1" >
  68. <tr>
  69. <th>编号</th>
  70. <th>名字</th>
  71. <th>性别</th>
  72. <th>年龄</th>
  73. <th>部门</th>
  74. </tr>
  75. <c:forEach items="${list}" var="data">
  76. <tr>
  77. <td>${data.number }</td>
  78. <td>${data.name }</td>
  79. <td>${data.gender }</td>
  80. <td>${data.age }</td>
  81. <td>${data.dep.name }</td>
  82. </tr>
  83. </c:forEach>
  84. </table>
  85. <button type="button" id="add">新增</button>
  86. <button type="button" id="update">修改</button>
  87. <button type="button" id="delete">删除</button>
  88. </div>
  89. </body>
  90. </html>
update.jsp
  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. pageEncoding="utf-8" %>
  3. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  4. <html>
  5. <head>
  6. <title>修改员工</title>
  7. <style>
  8. #container {
  9. width: 700px;
  10. margin: 10px auto;
  11. }
  12. #container form .align{
  13. margin:8px 0;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <div id="container">
  19. <form action="update">
  20. <input type="hidden" name="id" value="${emp.id}">
  21. <div class="align">
  22. <label>编号</label>
  23. <input type="text" placeholder="请输入编号"
  24. name="number" value="${emp.number}">
  25. </div>
  26. <div class="align">
  27. <label>名字</label>
  28. <input type="text" placeholder="请输入名字"
  29. name="name" value="${emp.name}">
  30. </div>
  31. <div class="align">
  32. <label>性别</label>
  33. <input type="radio" value="男" name="gender"
  34. <c:if test="${emp.gender=='男'}">checked</c:if> />男
  35. <input type="radio" value="女" name="gender"
  36. <c:if test="${emp.gender=='女'}">checked</c:if> />女
  37. </div>
  38. <div class="align">
  39. <label>年龄</label>
  40. <input type="text" placeholder="请输入年龄" name="age"
  41. value="${emp.age}">
  42. </div>
  43. <div class="align">
  44. <label>部门</label>
  45. <select name="dep.id">
  46. <c:forEach var="dep" items="${depList }">
  47. 2.6.2 dep
  48. add.jsp
  49. <option value="${dep.id }"
  50. <c:if test="${emp.dep.id==dep.id}">
  51. selected</c:if> >${dep.name }</option>
  52. </c:forEach>
  53. </select>
  54. </div>
  55. <div class="align">
  56. <button type="submit">保存</button>
  57. </div>
  58. </form>
  59. </div>
  60. </body>
  61. </html>
2.6.2 dep
add.jsp
  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. pageEncoding="utf-8" %>
  3. <html>
  4. <head>
  5. <title>部门新增</title>
  6. <style>
  7. #container {
  8. width: 400px;
  9. margin: 10px auto;
  10. }
  11. #container form .align {
  12. margin: 8px 0;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <div id="container">
  18. <form action="add">
  19. <div class="align">
  20. <label>编号</label>
  21. <input type="text" placeholder="请输入编号" name="number">
  22. </div>
  23. <div class="align">
  24. <label>名称</label>
  25. <input type="text" placeholder="请输入名称" name="name">
  26. </div>
  27. <div class="align">
  28. <button type="submit">保存</button>
  29. </div>
  30. </form>
  31. </div>
  32. </body>
  33. </html>
show.jsp
  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. pageEncoding="utf-8" %>
  3. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  4. <html>
  5. <head>
  6. <title>部门展示</title>
  7. <style>
  8. #container {
  9. width: 400px;
  10. margin: 10px auto;
  11. }
  12. #container #data {
  13. clear: both;
  14. width: 400px;
  15. margin: 10px 0;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <div id="container">
  21. <table id="data" border="1px">
  22. <tr>
  23. <th>编号</th>
  24. <th>名称</th>
  25. </tr>
  26. <c:forEach items="${list}" var="data">
  27. <tr>
  28. <td>${data.number }</td>
  29. <td>${data.name }</td>
  30. </tr>
  31. </c:forEach>
  32. </table>
  33. <button type="button" class="btn btn-primary" id="add">新增</button>
  34. <button type="button" class="btn btn-primary" id="update">修改</button>
  35. <button type="button" class="btn btn-danger" id="delete">删除</button>
  36. </div>
  37. </body>
  38. </html>
update.jsp
  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. pageEncoding="utf-8" %>
  3. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  4. <html>
  5. <head>
  6. <title>部门修改</title>
  7. <style>
  8. #container {
  9. width: 600px;
  10. margin: 10px auto;
  11. }
  12. #container form .align {
  13. margin: 8px 0;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <div id="container">
  19. <form action="update">
  20. <input type="hidden" name="id" value="${dep.id}">
  21. <div class="align">
  22. <label>编号</label>
  23. <input type="text" placeholder="请输入编号" name="number"
  24. value="${dep.number}">
  25. </div>
  26. <div class="align">
  27. <label>名称</label>
  28. <input type="text" placeholder="请输入名称"
  29. name="name" value="${dep.name}">
  30. </div>
  31. <div class="align">
  32. <button type="submit">保存</button>
  33. </div>
  34. </form>
  35. </div>
  36. </body>
  37. </html>
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/832669
推荐阅读
相关标签
  

闽ICP备14008679号