基于springboot+thymeleaf+mybatis的员⼯管理系统——增删改查
员⼯管理系统 - 增删改查
前⾯我们已经实现了注册与登录功能,整体的开发流程从 entity -> dao -> service -> controller 已经很详细了,这篇博客就不按照那么详细的流程记录了,根据这个流程看代码⾃⼰体会吧。
entity
在 ity 下创建 Emp.java:
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Emp {
private String id;
private String name;
private String salary;
private Double age;
private Date bir;
}
查询所有功能
在 com.yusal.dao包下创建 EmpDAO.java 接⼝:
package com.yusael.dao;
import ity.Emp;
import java.util.List;
public interface EmpDAO {
List<Emp>findAll();// 查询所有
}
在 resources/com/yusael/mapper 下创建 l:
<!DOCTYPE mapper PUBLIC "-////DTD Mapper 3.0//EN" "/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yusael.dao.EmpDAO">
<!--查询所有-->
<select id="findAll"resultType="Emp">
select id, name, salary, age, bir from t_emp
</select>
</mapper>
在 com.yusael.service 包下创建 EmpService.java 接⼝:
package com.yusael.service;
import ity.Emp;
import java.util.List;
public interface EmpService {
List<Emp>findAll();
}
在 com.yusael.service 包创建 EmpServiceImpl.java 中增加:
package com.yusael.service;
import com.yusael.dao.EmpDAO;
import ity.Emp;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
public class EmpServiceImp implements EmpService {
@Autowired
private EmpDAO empDAO;
@Override
public List<Emp>findAll(){
return empDAO.findAll();
}
}
ller 包下创建 EmpController.java:
package ller;
import ity.Emp;
import com.yusael.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
@RequestMapping("/emp")
public class EmpController {
@Autowired
private EmpService empService;
@GetMapping("findAll")
public String findAll(Model model){
List<Emp> emps = empService.findAll();
model.addAttribute("emps", emps);
return"/ems/emplist";
}
}
查询所有的页⾯ emplist.html
emplist.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "/TR/html4/loose.dtd"> <html th="">
<head>
<title>emplist</title>
<meta http-equiv="Content-Type"content="text/html; charset=UTF-8">
<link rel="stylesheet"type="text/css"href="@{/css/style.css}"/>
</head>
<body>
<div id="wrap">
<div id="top_content">
<div id="header">
<div id="rightheader">
<p>
2009/11/20
<br />
</p>
</div>
<div id="topheader">
<h1 id="title">
<a href="#">main</a>
</h1>
</div>
<div id="navigation">
</div>
</div>
<div id="content">
<p id="whereami">
</p>
<h1>
Welcome <span text="${session.user.username}"/>!
</h1>
<table class="table">
<tr class="table_header">
<td>
ID
</td>
<td>
Name
</td>
<td>
Salary
</td>
<td>
Age
</td>
<td>
Bir
</td>
<td>
Operation
</td>
</tr>
<tr th:class=${empStat.odd}?'row1':'row2' th:each="emp,empStat : ${emps}"> <td>
<span text="${emp.id}"/>
</td>
<td>
<span text="${emp.name}"/>
</td>
<td>
<span text="${emp.salary}"/>
</td>
<td>
<span text="${emp.age}"/>
</td>
<td>
<span text="${#dates.format(emp.bir,'yyyy-MM-dd')}"/>
</td>
<td>
<a href="@{/emp/delete(id=${emp.id})}">delete emp</a> |
<a href="@{/emp/find(id=${emp.id})}">update emp</a>
</td>
</tr>
</table>
<p>
<input type="button"class="button"value="Add Employee"onclick="location.href='/ems/toSave'">
</p>
</div>
</div>
<div id="footer">
<div id="footer_bg">
ABC@126
</div>
</div>
</div>
</body>
</html>
⾄此,接着上篇⽂章的登录功能,点击 Submit 以后,可以跳转到查询所有的界⾯了。
保存员⼯
com.yusal.dao包: EmpDAO.java 接⼝中增加⼀个⽅法:
void save(Emp emp);
resources/com/yusael/mapper ⽬录下: l 增加如下代码:
<!--保存员⼯-->
<insert id="save"parameterType="Emp">
insert into t_emp values (#{id}, #{name}, #{salary}, #{age}, #{bir})
</insert>
com.yusael.service 包:EmpService.java 接⼝增加⼀个⽅法:
void save(Emp emp);
com.yusael.service 包:EmpServiceImpl.java 增加⼀个⽅法实现:
@Override
public void save(Emp emp){
emp.setId(UUID.randomUUID().toString());
empDAO.save(emp);
}
ller 包:EmpController.java 增加:
@GetMapping("/save")
public String findAll(Emp emp){
empService.save(emp);
return"redirect:/emp/findAll";
}
保存员⼯的页⾯ addEmp.html
addEmp.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "/TR/html4/loose.dtd"> <html th="">
<head>
<title>add Emp</title>
<title>add Emp</title>
<meta http-equiv="Content-Type"content="text/html; charset=UTF-8"> <link rel="stylesheet"type="text/css"
href="css/style.css"/>
</head>
<body>
<div id="wrap">
<div id="top_content">
<div id="header">
<div id="rightheader">
<p>
2009/11/20
<br />
</p>
</div>
<div id="topheader">
<h1 id="title">
<a href="#">Main</a>
</h1>
</div>
<div id="navigation">
</div>
</div>
<div id="content">
<p id="whereami">
</p>
<h1>
add Emp info:
</h1>
<form action="@{/emp/save}"method="post">
<table cellpadding="0"cellspacing="0"border="0"
class="form_table">
<tr>
<td valign="middle"align="right">
name:
</td>
<td valign="middle"align="left">
<input type="text"class="inputgri"name="name"/>
</td>
</tr>
<tr>
<td valign="middle"align="right">
thymesalary:
</td>
<td valign="middle"align="left">
<input type="text"class="inputgri"name="salary"/>
</td>
</tr>
<tr>
<td valign="middle"align="right">
age:
</td>
<td valign="middle"align="left">
<input type="text"class="inputgri"name="age"/>
</td>
</tr>
<tr>
<td valign="middle"align="right">
bir:
</td>
<td valign="middle"align="left">
<input type="text"class="inputgri"name="bir"/>
</td>
</tr>
</table>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论