当前位置:网站首页>员工管理系统编写过程中摘要
员工管理系统编写过程中摘要
2022-07-16 20:21:00 【月阴荒】
第一步。导入静态资源
第二步。编写pojo层
pojo层:用于存放普通的javaBean类
在此次系统中我们编写了,员工表和部门表两张表,两张表其内的属性均设计为private属性
其注解有
@Data
@NoArgsConstructor
@NoArgsConstructor: 自动生成无参数构造函数。
@AllArgsConstructor: 自动生成全参数构造函数。
@Data的功能为:
1.为类提供读写功能,从而不用写get,set方法
@NoArgsConstructor的功能: 自动生成无参数构造函数。
@AllArgsConstructor: 自动生成全参数构造函数。
员工表的属性有 id,姓名,邮箱,性别,部门,生日
private Integer id;
private String lastName;
private String email;
private Integer gender;
private Department department;
private Date birth;
再定义员工方法用this调用员工的属性
public Employee(Integer id,String lastName,String email,Integer gender,Department department){
this.id=id;
this.lastName =lastName;
this.email =email;
this.gender =gender;
this.department=department;
this.birth = new Date();
}
部门表的属性有id 和部门名字
其注解有
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Depatement{
private int id; //部门id
private String departmentName; //部门名字
}
以上操作都注意要添加lombok依赖
第三步:编写dao层
此处我们先使用dao层来模拟数据库,springboot和数据库的连接在后续的课程中
部门dao
对应的是pojo层的部门表
先设置一个map结构给department数据
private static Map<Integer,Department>departments=null; //创建一个部门表并设置初始为空
然后用static静态数据模拟数据库中的数据
static{
departments =new HashMap<Integer,Department>(); //创建一个部门表
departments.put(101,new Department(101,"教学部"));
departments.put(102,new Department(102,"市场部"));
departments.put(103,new Department(103,"教研部"));
departments.put(104,new Department(104,"运营部"));
departments.put(105,new Department(105,"后勤部"));
}
//获取所有的部门信息
public Collection<Department> getDepartments(){
return departments.values();
}
//通过id得到部门
public Department getDepartmentById(Integer id){
return departments.get(id);
}
员工dao,对应员工表
public class EmployeeDao{
//模拟数据库中的数据
private static Map<Integer, Employee> employees= null;
//员工所属的部门
@Autowired
private DepartmentDao departmentDao;
static {
employees = new HashMap<Integer,Employee>(); //创建一个部门表
employees.put(1001,new Employee( 1001,"AA","[email protected]",1,new Department(101,"教学部")));
employees.put(1002,new Employee( 1002,"BB","[email protected]",0,new Department(102,"市场部")));
employees.put(1003,new Employee( 1003,"CC","[email protected]",1,new Department(103,"教研部")));
employees.put(1004,new Employee( 1004,"DD","[email protected]",0,new Department(104,"运营部")));
employees.put(1005,new Employee( 1005,"FF","[email protected]",1,new Department(105,"后勤部")));
}
//主键自增
private static Integer ininId = 1006;
//增加员工
public void save(Employee employee){
if(employee.getId() == null){
employee.setId(ininId++);
}
employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));
employees.put(employee.getId(),employee);
}
//查询员工getAll
//查询全部的员工
public Collection<Employee>getALL(){
return employees.values();
}
}
4.首页引入
首先要引入Thymeleaf
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>
2.2编写MyMvcConfig
2.3 更改静态资源路径
改为
让所有静态资源都被thymeleaf接管:@{}
3.国际化(省略)
4.登录+拦截器
说明:页面存在缓存,所以我们需要禁用模板引擎的缓存
#禁用模板缓存
spring.thymeleaf.cache=false
模板引擎修改后,想要实时生效,页面修改完毕后,Ctrl + F9 重新编译!即可生效!
登录
我们这里就不先连接数据库了,输入任意用户名都可以登录成功
1.我们把登录页面的表单提交地址写一个Controller
<form class="form-signin" th:action="@{/user/login}" method="post">
//这里面的所有表单标签都需要加上一个name属性
</form>
2.编写对应的Controller来控制登录模块
@Controller
public class LoginController {
@RequestMapping("/user/login")
public String login(
@RequestParam("username") String username ,
@RequestParam("password") String password,
Model model){
//具体的业务
if(!StringUtils.isEmpty(username)&&"123456".equals(password)){
return "redirect:/main.html";
}
else{
//告诉用户,你登录失败
model.addAttribute("msg","用户名或者密码错误!");
return "index";
}
}
}
3.登录失败的话,我们需要将后台信息输出到前台,可以首页标题下面加上判断
<!--判断是否显示,使用if, ${
}可以使用工具类,可以看thymeleaf的中文文档-->
<p style="color: red" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}">
</p>
5.员工信息页面展示
<thead>
<tr>
<th>id</th>
<th>lastName</th>
<th>email</th>
<th>gender</th>
<th>department</th>
<th>birth</th>
</tr>
</thead>
<tbody>
<tr th:each="emp:${emps}">
<td th:text="${emp.getId()}"></td>
<td th:text="${emp.getLastName()}"></td>
<td th:text="${emp.getEmail()}"></td>
<td th:text="${emp.getGender()==0?'女':'男'}"></td>
<td th:text="${emp.department.getDepartmentName()}"></td>
<td th:text="${#dates.format(emp.getBirth(),'yyyy-MM-dd HH:mm:ss')}"></td>
<td>
<button class="btn btn-sm btn-primary">编辑</button>
<button class="btn btn-sm btn-danger">删除</button>
</td>
</tr>
</tbody>
6.表单细节处理
1.将添加员工信息改为超链接
<h2><a class="btn btn-sm btn-success" th:href="@{/emp}">添加员工</a></h2>
2.编写对应的Controller
//to员工添加页面
@GetMapping("/emp")
public String toAddPage(){
return "emp/add";
}
3.添加前端页面;复制list页面,修改即可
bootstrap官网文档 :https://v4.bootcss.com/docs/4.0/components/forms/
4.部门信息下拉框应该选择的是我们提供的数据,所以我们要修改一下前端和后端
@GetMapping("/emp")
public String toAddPage(Model model){
//查询所有的部门信息
Collection<Department> departments = departmentDao.getDepartments();
model.addAttribute("departments",departments);
return "emp/add";
}
边栏推荐
- 封装、获取系统用户信息、角色及权限控制
- Win11怎么进行长截图?Win11长截图的方法
- A New Optimizer Using Particle Swarm Theory
- [UCOS III source code analysis] - system initialization
- 技术干货 | 模型优化精度、速度我全都要!MindSpore模型精度调优实战(二)
- How to use mitmproxy to get data return in automated testing
- Topic selection recommendation of the latest information security graduation project
- 哪个证券公司开户股票手续费低,哪个股票开户安全
- 看完这篇 教你玩转渗透测试靶机vulnhub——EvilBox-One
- 2-conan 二進制包依賴管理方案
猜你喜欢

Which brand of Bluetooth headset has good noise reduction? Top 10 active noise reduction headphones

8080端口被占用怎么解决?Win11 8080端口被占用解决方法

SCI paper submission process

After reading this article, I will teach you to play with vulnhub, the penetration test target machine -- evilbox one

技术干货 | 模型优化精度、速度我全都要!MindSpore模型精度调优实战(二)

MySQL 5.7.37 database download and installation tutorial (no installation required for Windows)

uCOSIIV2.93移植时为什么会出错,错误信息为undefined symbol ‘_OSTaskReturnHook‘

How to record sound on win11 screen? Win11 method of recording screen video with sound

千亿元宇宙市场,Soul、映客的新动力
![[UCOS III source code analysis] - message queue](/img/e9/04fb629caa78513a1a6e8a4a98f452.png)
[UCOS III source code analysis] - message queue
随机推荐
开源十问, 社区新人快速上手指南
Huawei od JS splicing URL
PHP memory overflow? How to solve it?
Lingo operators and built-in functions
Which brand of Bluetooth headset has good noise reduction? Top 10 active noise reduction headphones
Error in WordPress establishing database connection
The observation returned by the 'reset()' method is not contained with the
移动端设置小于12px字体,script标签
(manual) [sqli-labs58-61] limit the injection times: error injection, get injection, character / number type
封装、获取系统用户信息、角色及权限控制
What about the update error of win11 preview? Solutions to the failure of win11 preview installation
A New Optimizer Using Particle Swarm Theory
Three sides of headlines + four sides of Alibaba + five sides of Tencent took the offer to share the summary, and finally joined Alibaba
[UCOS III source code analysis] - wait for multiple kernel objects
在匿名内部类中访问局部变量
[applet project development -- JD mall] product list page of uni app (Part 1)
KDD 2017 | metapath2vec:异质图的可扩展表示学习
LINGO求解分段函数最大(小)值
Dajiang school recruitment evaluation question -- round robin problem
Visual Studio 生产环境配置方案:SlowCheetah