赞
踩
接上一篇博客
EmployeeService接口代码:
- List<Emp> search(Emp condition);
- Emp searchById(Integer id);
- boolean add(Emp emp);
- boolean update(Emp emp);
- boolean delete(Integer id);
EmployeeServiceImpl实现类代码:
- @Service
- public class EmpServiceImpl implements EmpService {
- @Autowired
- EmpDao empDao;
- @Override
- public List<Emp> search(Emp condition) {
- List<Emp> list = empDao.search(condition);
- return list;
- }
- @Override
- public Emp searchById(Integer id) {
- Emp emp = empDao.searchById(id);
- return emp;
- }
- @Override
- public boolean add(Emp emp) {
- int rs = empDao.add(emp);
- return rs > 0;
- }
- @Override
- public boolean update(Emp emp) {
- int rs = empDao.update(emp);
- return rs > 0;
- }
- @Override
- public boolean delete(Integer id) {
- int rs = empDao.delete(id);
- return rs > 0;
- }
- }
- public List<Dep> search();
- public Dep searchById(Integer id);
- public boolean add(Dep dep);
- public boolean update(Dep dep);
- public boolean delete(Integer id);
- @Service
- public class DepServiceImpl implements DepService {
- @Autowired
- DepDao depDao;
- @Autowired
- EmpDao empDao;
- @Override
-
- public List<Dep> search() {
- List<Dep> list = depDao.search();
- return list;
- }
- @Override
- public Dep searchById(Integer id) {
- Dep dep = depDao.searchById(id);
- return dep;
- }
- @Override
- public boolean add(Dep dep) {
- int rs = depDao.add(dep);
- return rs > 0;
- }
- @Override
- public boolean update(Dep dep) {
- int rs = depDao.update(dep);
- return rs > 0;
- }
- @Override
- public boolean delete(Integer id) {
- int rs = depDao.delete(id);
- rs = empDao.updateByDep(id);
- return rs > 0;
- }
- }
Spring MVC的DispatcherServlet的使用与配置,基于注解映射机制。
EmployeeController实现代码:
- @Controller
- @RequestMapping("emp")
- public class EmpController {
- @Autowired
- EmpService empService;
- @Autowired
- DepService depService;
- @RequestMapping("search")
- public ModelAndView search(Emp condition) {
- ModelAndView mv = new ModelAndView("emp/show");
- List<Emp> list = empService.search(condition);
- List<Dep> depList=depService.search();
- mv.addObject("list", list);
- mv.addObject("depList",depList);
- mv.addObject("c", condition);
- return mv;
- }
- @RequestMapping("showAdd")
- public ModelAndView showAdd() {
- ModelAndView mv = new ModelAndView("emp/add");
- List<Dep> depList = depService.search();
- mv.addObject("depList", depList);
- return mv;
- }
- @RequestMapping("add")
- public String add(Emp emp) {
- boolean flag = empService.add(emp);
- return "redirect:search";
- }
- @RequestMapping("showUpdate")
- public ModelAndView showUpdate(Integer id) {
- Emp emp = empService.searchById(id);
- List<Dep> depList = depService.search();
- ModelAndView mv = new ModelAndView("emp/update");
- mv.addObject("emp", emp);
- mv.addObject("depList", depList);
- return mv;
- }
- @RequestMapping(value="update")
- public String update(Emp emp) {
- boolean flag = empService.update(emp);
- return "redirect:search";
- }
- @RequestMapping("delete")
- public String delete(Integer id) {
- boolean flag = empService.delete(id);
- return "redirect:search";
- }
- }
- @Controller
- @RequestMapping("dep")
- public class DepController {
- @Autowired
- DepService depService;
- @RequestMapping("search")
- public ModelAndView search() {
- ModelAndView mv = new ModelAndView("dep/show");
- List<Dep> list = depService.search();
- mv.addObject("list", list);
- return mv;
- }
- @RequestMapping("showAdd")
- public String showAdd() {
- return "dep/add";
- }
- @RequestMapping("add")
- public String add(Dep dep) {
- boolean flag = depService.add(dep);
- return "redirect:search";
- }
- @RequestMapping("showUpdate")
- public ModelAndView showUpdat(Integer id) {
- ModelAndView mv = new ModelAndView("dep/update");
- Dep dep = depService.searchById(id);
- mv.addObject("dep", dep);
- return mv;
- }
- @RequestMapping("update")
- public String update(Dep dep) {
- boolean flag = depService.update(dep);
- return "redirect:search";
- }
- @RequestMapping("delete")
- public String delete(Integer id) {
- boolean flag = depService.delete(id);
- return "redirect:search";
- }
- }
add.jsp
- <%@ page language="java" contentType="text/html; charset=utf-8"
- pageEncoding="utf-8" %>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
- <html>
- <head>
- <title></title>
- <style>
- #container {
- width: 700px;
- margin: 10px auto;
- }
- #container form .align {
- margin: 8px 0;
- }
- </style>
- </head>
- <body>
- <div id="container">
- <form action="add">
- <div class="align">
- <label>编号</label>
- <input type="text" placeholder="请输入编号"
- name="number">
- </div>
- <div class="align">
- <label>名字</label>
- <input type="text" placeholder="请输入名字" name="name">
- </div>
- <div class="align">
- <label>性别</label>
- <input type="radio" value="男" name="gender"/>男
- <input type="radio" value="女" name="gender"/>女
- </div>
- <div class="align">
- <label>年龄</label>
- <input type="text" placeholder="请输入年龄" name="age">
- </div>
- <div class="align">
- <label>部门</label>
- <select name="dep.id">
- <c:forEach var="dep" items="${depList }">
- <option value="${dep.id }">${dep.name }</option>
- </c:forEach>
- </select>
- </div>
- <div class="align">
- <button type="submit">保存</button>
- </div>
- </form>
- </div>
- </body>
- </html>
- <%@ page language="java" contentType="text/html; charset=utf-8"
- pageEncoding="utf-8" %>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
- <html>
- <head>
- <title>员工展示</title>
- <style>
- #container {
- width: 700px;
- margin: 10px auto;
- }
- #container #search{
- overflow: hidden;
- }
- #container #search .align{
- float:left;
- margin-right:8px;
- }
- #container #search input{
- width:160px;
- }
- #container #data {
- clear: both;
- width: 700px;
- margin:10px 0;
- }
- </style>
- </head>
- <body>
- <div id="container">
- <form id="search" action="search" method="post">
- <div class="align">
- <input type="text" name="number"
- placeholder="编号" value=${c.number}>
- </div>
- <div class="align">
- <input type="text" name="name"
- placeholder="姓名" value=${c.name}>
- </div>
- <div class="align">
- <select name="gender">
- <option value="">性别</option>
- <option value="男" <c:if test="${c.gender =='男'}">
- selected</c:if>>男</option>
- <option value="女" <c:if test="${c.gender =='女'}">
- selected</c:if>>女</option>
- </select>
- </div>
- <div class="align">
- <input type="text" name="age" placeholder="年龄"
- value=${c.age!=null?c.age:''}>
- </div>
- <div class="align">
- <select name="dep.id">
- <option value="">部门</option>
- <c:forEach items="${depList}" var="dep">
- <option value="${dep.id }"
- <c:if test="${dep.id ==c.dep.id}">
- selected</c:if>>${dep.name }</option>
- </c:forEach>
- </select>
- </div>
- <div class="align">
- <button type="submit">搜索</button>
- </div>
- </form>
- <table id="data" border="1" >
- <tr>
- <th>编号</th>
- <th>名字</th>
- <th>性别</th>
- <th>年龄</th>
- <th>部门</th>
- </tr>
- <c:forEach items="${list}" var="data">
- <tr>
- <td>${data.number }</td>
- <td>${data.name }</td>
- <td>${data.gender }</td>
- <td>${data.age }</td>
- <td>${data.dep.name }</td>
- </tr>
- </c:forEach>
- </table>
- <button type="button" id="add">新增</button>
- <button type="button" id="update">修改</button>
- <button type="button" id="delete">删除</button>
- </div>
- </body>
- </html>
- <%@ page language="java" contentType="text/html; charset=utf-8"
- pageEncoding="utf-8" %>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
- <html>
- <head>
- <title>修改员工</title>
- <style>
- #container {
- width: 700px;
- margin: 10px auto;
- }
- #container form .align{
- margin:8px 0;
- }
- </style>
- </head>
- <body>
- <div id="container">
- <form action="update">
- <input type="hidden" name="id" value="${emp.id}">
- <div class="align">
- <label>编号</label>
- <input type="text" placeholder="请输入编号"
- name="number" value="${emp.number}">
- </div>
- <div class="align">
- <label>名字</label>
- <input type="text" placeholder="请输入名字"
- name="name" value="${emp.name}">
- </div>
- <div class="align">
- <label>性别</label>
- <input type="radio" value="男" name="gender"
- <c:if test="${emp.gender=='男'}">checked</c:if> />男
- <input type="radio" value="女" name="gender"
- <c:if test="${emp.gender=='女'}">checked</c:if> />女
- </div>
- <div class="align">
- <label>年龄</label>
- <input type="text" placeholder="请输入年龄" name="age"
- value="${emp.age}">
- </div>
- <div class="align">
- <label>部门</label>
- <select name="dep.id">
- <c:forEach var="dep" items="${depList }">
- 2.6.2 dep
- add.jsp
- <option value="${dep.id }"
- <c:if test="${emp.dep.id==dep.id}">
- selected</c:if> >${dep.name }</option>
- </c:forEach>
- </select>
- </div>
- <div class="align">
- <button type="submit">保存</button>
- </div>
- </form>
- </div>
- </body>
- </html>
- <%@ page language="java" contentType="text/html; charset=utf-8"
- pageEncoding="utf-8" %>
- <html>
- <head>
- <title>部门新增</title>
- <style>
- #container {
- width: 400px;
- margin: 10px auto;
- }
- #container form .align {
- margin: 8px 0;
- }
- </style>
- </head>
- <body>
- <div id="container">
- <form action="add">
- <div class="align">
- <label>编号</label>
- <input type="text" placeholder="请输入编号" name="number">
- </div>
- <div class="align">
- <label>名称</label>
- <input type="text" placeholder="请输入名称" name="name">
- </div>
- <div class="align">
- <button type="submit">保存</button>
- </div>
- </form>
- </div>
- </body>
- </html>
- <%@ page language="java" contentType="text/html; charset=utf-8"
- pageEncoding="utf-8" %>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
- <html>
- <head>
- <title>部门展示</title>
- <style>
- #container {
- width: 400px;
- margin: 10px auto;
- }
- #container #data {
- clear: both;
- width: 400px;
- margin: 10px 0;
- }
- </style>
- </head>
- <body>
- <div id="container">
- <table id="data" border="1px">
- <tr>
- <th>编号</th>
- <th>名称</th>
- </tr>
- <c:forEach items="${list}" var="data">
- <tr>
- <td>${data.number }</td>
- <td>${data.name }</td>
- </tr>
- </c:forEach>
- </table>
- <button type="button" class="btn btn-primary" id="add">新增</button>
- <button type="button" class="btn btn-primary" id="update">修改</button>
- <button type="button" class="btn btn-danger" id="delete">删除</button>
- </div>
- </body>
- </html>
- <%@ page language="java" contentType="text/html; charset=utf-8"
- pageEncoding="utf-8" %>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
- <html>
- <head>
- <title>部门修改</title>
- <style>
- #container {
- width: 600px;
- margin: 10px auto;
- }
- #container form .align {
- margin: 8px 0;
- }
- </style>
- </head>
- <body>
- <div id="container">
- <form action="update">
- <input type="hidden" name="id" value="${dep.id}">
- <div class="align">
- <label>编号</label>
- <input type="text" placeholder="请输入编号" name="number"
- value="${dep.number}">
- </div>
- <div class="align">
- <label>名称</label>
- <input type="text" placeholder="请输入名称"
- name="name" value="${dep.name}">
- </div>
- <div class="align">
- <button type="submit">保存</button>
- </div>
- </form>
- </div>
- </body>
- </html>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。