SSM架构(二)

接上一篇博客

SSM框架(一)-CSDN博客

2.4 Spring

2.4.1 Service****设计

EmployeeService接口代码:

java 复制代码
    List<Emp> search(Emp condition);
    Emp searchById(Integer id);
    boolean add(Emp emp);
    boolean update(Emp emp);
    boolean delete(Integer id);

EmployeeServiceImpl实现类代码:

java 复制代码
@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;
    }
}

DepartmentService 接口代码:

java 复制代码
    public List<Dep> search();
    public Dep searchById(Integer id);
    public boolean add(Dep dep);
    public boolean update(Dep dep);
    public boolean delete(Integer id);

DepartmentServiceImpl 实现类代码:

java 复制代码
@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;
    }
}

2.5 Spring MVC

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

2.5.1 Controller****设计

EmployeeController实现代码:

java 复制代码
@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";
    }
}

DepartmentController 实现代码:

java 复制代码
@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";
    }
}

2.6 JSP(View)

2.6.1 emp

add.jsp

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="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>

show.jsp

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>

update.jsp

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>
2.6.2 dep

add.jsp

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>

show.jsp

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>

update.jsp

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>
相关推荐
m0_571957581 小时前
Java | Leetcode Java题解之第543题二叉树的直径
java·leetcode·题解
魔道不误砍柴功3 小时前
Java 中如何巧妙应用 Function 让方法复用性更强
java·开发语言·python
NiNg_1_2343 小时前
SpringBoot整合SpringSecurity实现密码加密解密、登录认证退出功能
java·spring boot·后端
闲晨3 小时前
C++ 继承:代码传承的魔法棒,开启奇幻编程之旅
java·c语言·开发语言·c++·经验分享
测开小菜鸟5 小时前
使用python向钉钉群聊发送消息
java·python·钉钉
P.H. Infinity6 小时前
【RabbitMQ】04-发送者可靠性
java·rabbitmq·java-rabbitmq
生命几十年3万天6 小时前
java的threadlocal为何内存泄漏
java
caridle6 小时前
教程:使用 InterBase Express 访问数据库(五):TIBTransaction
java·数据库·express
^velpro^6 小时前
数据库连接池的创建
java·开发语言·数据库
苹果醋36 小时前
Java8->Java19的初步探索
java·运维·spring boot·mysql·nginx