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>
相关推荐
时差freebright9 分钟前
【Visual Studio 报错】vs 在使用二进制写入文件时弹窗报错:使用简体中文 gb2312 编码加载文件
android·java·visual studio
我的K840921 分钟前
如何将java文件导入idea中运行和推送到Gitee仓库问题
java·idea
有邪25 分钟前
一文讲懂IDEA的编码格式file encoding
java·ide·intellij-idea
fmc1211041 小时前
Excel文档的读取(3)
java·前端·python
纵横君=_=1 小时前
Day8 | Java框架 | Maven
java·开发语言·maven
德墨忒尔1 小时前
使用 Apache POI 实现 Java Word 模板占位符替换功能
java·word·apache
兜兜 里 有糖1 小时前
laravel 查询数据对象转数组
android·java·laravel
一休哥助手1 小时前
Redis单机、集群、哨兵、主从架构详解
redis·架构
来一杯龙舌兰2 小时前
【JAVA】Undertow的使用及性能优化,以及Undertow与Tomcat的对比
java·性能优化·tomcat·undertow·容器配置
洁洁!2 小时前
理解 C 语言:从基础到高级的全面介绍
java·c语言·开发语言