前端页面下载:👆👆👆👆👆👆👆👆
没有链接数据库的版本,后面会更新带有Mybatis和Mybatis_plus版本的
首先创建一个Springboot项目。 之前的教程
项目结构
pom.xml 引入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.wang</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- thymeleaf-->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>
<!-- @Date 省去get set toString-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
创建实体类(员工和部门)lombok@Date
类加了@Data注解,所有Java代码中不需要生成getter and setter,而在编译的时候会自动生成getter and setter。
Employee员工
package com.wang.pojo;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
//员工表
@Data
@NoArgsConstructor
public class Employee {
private Integer id;
private String lastName;
private String email;
private Integer sex;//0:女 1:男
private Department department;
private Date birth;
public Employee(Integer id, String lastName, String email, Integer sex, Department department) {
this.id = id;
this.lastName = lastName;
this.email = email;
this.sex = sex;
this.department = department;
//默认日期
this.birth = new Date();
}
}
Department部门
package com.wang.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
//部门表
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Department {
private Integer id;
private String departmentName;
}
这里没有链接数据库,所以伪造了数据
DepartmentDao
package com.wang.dao;
import com.wang.pojo.Department;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
//部门dao
@Repository
public class DepartmentDao {
//模拟数据库中的数据
private static Map<Integer, Department> departments = null;
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> getDepartment() {
return departments.values();
}
//通过id得到部门
public Department getDepartmentById(Integer id) {
return departments.get(id);
}
}
员工Dao
package com.wang.dao;
import com.wang.pojo.Department;
import com.wang.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.*;
//员工Dao
@Repository
public class EmployeeDao {
//模拟数据库中的数据
private static Map<Integer, Employee> employees = null;
//员工有所属的部门
@Autowired
private DepartmentDao departmentDao;
static {
employees = new HashMap<Integer, Employee>();//创建一个部门表
employees.put(101, new Employee(1001, "AA", "A2919543658@qq.com", 1, new Department(101, "教学部")));
employees.put(102, new Employee(1002, "BB", "B2919543658@qq.com", 0, new Department(102, "市场部")));
employees.put(103, new Employee(1003, "CC", "C2919543658@qq.com", 1, new Department(103, "教研部")));
employees.put(104, new Employee(1004, "DD", "D2919543658@qq.com", 0, new Department(104, "运营部")));
employees.put(105, new Employee(1005, "EE", "E2919543658@qq.com", 1, new Department(105, "后勤部")));
}
//主键自增
private static Integer integer = 1006;
//增加一个员工
public void save(Employee employee) {
if (employee.getId() == null) {
employee.setId(integer++);
}
employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));
employees.put(employee.getId(), employee);
}
//查询所有员工信息
public Map<Integer,Employee> getAll() {
List<Employee> value = new ArrayList<>();
// for (Integer k : employees.keySet()) {
// key.add(k);
// }
// for (Employee k : employees.values()) {
// value.add(k);
// }
// Map<Integer,Employee> map = new HashMap<>();
for (int i=0;i<value.size();i++){
Employee employee = value.get(i);
}
return EmployeeDao.employees;
}
//通过id查询员工
public Employee getEmployeeById(Integer id) {
System.out.println("daoid:"+id);
return employees.get(101);
}
//通过id删除员工
public void delete(Integer id) {
employees.remove(id);
}
}
员工登录的Controller
package com.wang.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.thymeleaf.util.StringUtils;
import javax.servlet.http.HttpSession;
@Controller
public class LoginController {
@RequestMapping("/user/login")
public String login(@RequestParam("username") String username, @RequestParam("password") String password, Model model, HttpSession session) {
//具体的业务
if (!StringUtils.isEmpty(username) && "123456".equals(password)) {
session.setAttribute("loginUser",username);
//登录成功,重定向跳转
return "redirect:/main.html";
} else {
//告诉用户,登陆失败
model.addAttribute("msg", "用户名或密码错误!");
return "index";
}
}
@RequestMapping("/user/logout")
public String logout(HttpSession session){
session.invalidate();
return "redirect:/index.html";
}
}
增删改查方法体
package com.wang.controller;
import com.wang.dao.DepartmentDao;
import com.wang.dao.EmployeeDao;
import com.wang.pojo.Department;
import com.wang.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.*;
@Controller
public class EmployeeController {
@Autowired
private EmployeeDao employeeDao;
@Autowired
DepartmentDao departmentDao;
//查询并跳转
@RequestMapping("/emps")
public String list(Model model) {
Map<Integer, Employee> employees = employeeDao.getAll();
// List<Map<Integer,Employee>> list = new ArrayList<>();
// for (Integer k : employees.keySet()) {
// Map<Integer,Employee> map = new HashMap<>();
// map.put(k,employees.get(k));
// }
// employees.forEach(System.out::println);
model.addAttribute("emps", employees);
return "emp/list";
}
@GetMapping("/emp")
public String toAddpage(Model model){
//查出部门的信息
Collection<Department> department = departmentDao.getDepartment();
model.addAttribute("department",department);
return "emp/add";
}
//添加页面
@PostMapping("/emp")
public String addEmp(Employee employee){
//添加的操作,添加成功后重定向到首页 forward
employeeDao.save(employee);//调用底层的方法保存员工的信息
return "redirect:/emps";
}
//去员工的修改页面
@GetMapping("/emp/update/{id}")
public String toUpdateEmp(@PathVariable("id")Integer id,Model model){
System.out.println("id:"+id);
Employee employee = employeeDao.getEmployeeById(id);
System.out.println("employee:"+employee.toString());
model.addAttribute("emp",employee);
//查出部门的信息
Collection<Department> department = departmentDao.getDepartment();
model.addAttribute("department",department);
return "update";
}
//修改员工
@PostMapping("/updateEmp")
public String updateEmp(Employee employee){
employeeDao.save(employee);
return "redirect:/emps";
}
//删除员工
@GetMapping("/emp/del/{id}")
public String deleteEmp(@PathVariable("id") Integer id){
employeeDao.delete(id);
return "redirect:/emps";
}
}
扩展Mvc
1.实现WebMvcConfigurer类,重写addViewControllers方法,做到无业务逻辑的页面跳转
2.配置一个过滤器,做到必须登录才能进入系统
package com.wang.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
@Override
public