Spring+Mybatis初步实现增删改查

控制层


@Controller("departmentController")
public class DepartmentController {
    //业务层
    @Autowired
    private DepartmentService departmentService;

    //规则  /department/list.do     /department_list.jsp
    public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //查找全部
        List list = departmentService.getAll();
        request.setAttribute("LIST",list);
        request.getRequestDispatcher("../department_list.jsp").forward(request,response);
    }

    public void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("../department_add.jsp").forward(request,response);
    }

    //修改
    public void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1获取ID
        Integer id = Integer.parseInt(request.getParameter("id"));
        //2.方法
        Department department = departmentService.get(id);
        request.setAttribute("OBJ",department);
        request.getRequestDispatcher("../department_edit.jsp").forward(request,response);
    }

    //发送请求
    public void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = Integer.parseInt(request.getParameter("id"));
        String name = request.getParameter("name");
        String address = request.getParameter("address");

        //实例化
        Department department = new Department();
        department.setId(id);
        department.setName(name);
        department.setAddress(address);


        departmentService.edit(department);
        response.sendRedirect("list.do");

    }


    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //定义请求
        String name = request.getParameter("name");
        String address = request.getParameter("address");

        //实例化
        Department department = new Department();
        department.setName(name);
        department.setAddress(address);

        //添加
        departmentService.add(department);

        response.sendRedirect("list.do");
    }

    //删除
    public void remove(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = Integer.parseInt(request.getParameter("id"));
        departmentService.remove(id);
        response.sendRedirect("list.do");
    }
}


编辑页面
`<%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2019/5/18
  Time: 21:50
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    
    
    添加部门
    
    
    
    
    



添加部门
 
`主界面

<%–
Created by IntelliJ IDEA.
User: admin
Date: 2019/5/18
Time: 21:56
To change this template use File | Settings | File Templates.
–%>
<%@ page contentType=“text/html;charset=UTF-8” language=“java” %>
<%@taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core” %>

部门列表
部门列表
 
名称 地址 操作
${dep.name} ${dep.address} 编辑 删除
添加

添加界面

<%–
Created by IntelliJ IDEA.
User: admin
Date: 2019/5/18
Time: 22:59
To change this template use File | Settings | File Templates.
–%>
<%@ page contentType=“text/html;charset=UTF-8” language=“java” %>
<%@taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core” %>

编辑部门
编辑部门
 
  • 名称:
    *
  • 地址:
  • 返回 保存

Spring+Mybatis初步实现增删改查_第1张图片

Spring+Mybatis初步实现增删改查_第2张图片

Spring+Mybatis初步实现增删改查_第3张图片
Spring+Mybatis初步实现增删改查_第4张图片

你可能感兴趣的:(萌新)