springboot增删改查管理系统(无数据库版本)
目录截图
程序截图
Config
登录拦截器的配置
LoginHandlerInterceptor
package com.atguigu.springboot.config;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//登录拦截器
//HandlerInterceptor是springboot自带的拦截器功能
//标准写法
//定义一个Interceptor 非常简单方式也有几种,我这里简单列举两种
//1、类要实现Spring 的HandlerInterceptor 接口
//2、类继承实现了HandlerInterceptor 接口的类,例如 已经提供的实现了HandlerInterceptor 接口的抽象类HandlerInterceptorAdapter
public class LoginHandlerInterceptor implements HandlerInterceptor {
@Override
//preHandle:在业务处理器处理请求之前被调用。预处理,可以进行编码、安全控制、权限校验等处理
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//这里主要是查看有没有记录用户session,session 是借助 cookie 实现的。另外,清空客户端cookie,session就会丢失。
//登录成功之后,应该有用户的session
//request.getSession()一般是存储信息到session中或者从session中获取信息。
//getAttribute()取值admin{loginUser:admin}
Object loginUser = request.getSession().getAttribute("loginUser");
if(loginUser == null){
request.setAttribute("msg","没有权限,请先登录");
request.getRequestDispatcher("/index.html").forward(request,response);
return false;
}else {
return true;
}
}
}
MyMvcConfig
package com.atguigu.springboot.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;
//连接前端????的,应该是连接前端,他用这个代替了controller
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
//WebMvcConfigurer配置类其实是Spring内部的一种配置方式
//采用JavaBean的形式来代替传统的xml配置文件形式进行针对框架个性化定制
//可以自定义一些Handler,Interceptor,ViewResolver,MessageConverter。
//基于java-based方式的spring mvc配置,需要创建一个配置类并实现WebMvcConfigurer 接口;
// addViewControllers:页面跳转
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
registry.addViewController("/main.html").setViewName("dashboard");
}
//addInterceptor:需要一个实现HandlerInterceptor接口的拦截器实例
//addPathPatterns:用于设置拦截器的过滤路径规则;addPathPatterns("/**")对所有请求都拦截
//excludePathPatterns:用于设置不需要拦截的过滤规则
//拦截器主要用途:进行用户登录状态的拦截,日志的拦截等。
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginHandlerInterceptor())
.addPathPatterns("/**")
.excludePathPatterns("/index.html","/","/user/login","/css/**","/js/**","/img/**");
}
}
Controller层
业务逻辑
EmployeeController
package com.atguigu.springboot.controller;
import com.atguigu.springboot.dao.DepartmentDao;
import com.atguigu.springboot.dao.EmployeeDao;
import com.atguigu.springboot.pojo.Department;
import com.atguigu.springboot.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.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Collection;
@Controller
public class EmployeeController {
//将DAO注入到Controller
//不注入就无法使用DAO里面的方法
@Autowired
EmployeeDao employeeDao;
@Autowired
DepartmentDao departmentDao;
@RequestMapping("/emps")
//model 就是后端数据与前端的承载物
public String list(Model model){
Collection<Employee> employees = employeeDao.getAll();
model.addAttribute("emps",employees);
return "emp/list";
}
@GetMapping("/emp")
//设置添加页面
public String toAddpage(Model model){
//查询所有的部门的信息
Collection<Department> departments = departmentDao.getDepartments();
model.addAttribute("departments",departments);
return "emp/add";
}
@PostMapping("/emp")
//设置添加页面
public String addEmp(Employee employee){
employeeDao.save(employee);
//添加操作重定向回列表页面
return "redirect:/emps";
}
//去员工的修改页面
@GetMapping("/emp/{id}")
public String toUpdateEmp(@PathVariable("id") Integer id,Model model){
//查出原来得数据
Employee employee = employeeDao.getEmployeeById(id);
model.addAttribute("emp",employee);
//查询所有的部门的信息
Collection<Department> departments = departmentDao.getDepartments();
model.addAttribute("departments",departments);
return "emp/update";
}
@PostMapping("/updateEmp")
public String updateEmp(Employee employee){
employeeDao.save(employee);
return "redirect:/emps";
}
//删除员工
@GetMapping("/delemp/{id}")
public String deleteEmp(@PathVariable("id") Integer id){
employeeDao.delete(id);
return "redirect:/emps";
}
}
登录的业务逻辑
LoginController
package com.atguigu.springboot.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.springframework.web.bind.annotation.ResponseBody;
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)&&"1".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";
}
}
DAO层
因为不连接数据库所以模拟一些初始的数据
DepartmentDao
package com.atguigu.springboot.dao;
import com.atguigu.springboot.pojo.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@Repository
//部门DAO
public class DepartmentDao {
//模拟数据库中的数据
//初始化数据
@Autowired
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> getDepartments(){
return departments.values();
}
public Department getDepartmentById(Integer id){
return departments.get(id);
}
}
EmployeeDao
package com.atguigu.springboot.dao;
import com.atguigu.springboot.pojo.Department;
import com.atguigu.springboot.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@Repository
//员工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,"教学的人","15874833156@qq.com",0,new Department(101,"后勤部")));
employees.put(1002,new Employee(1002,"市场的人","15874833156@qq.com",1,new Department(102,"市场部")));
employees.put(1003,new Employee(1003,"教研的人","15874833156@qq.com",0,new Department(103,"教研部")));
employees.put(1004,new Employee(1004,"运营的人","15874833156@qq.com",1,new Department(104,"运营部")));
employees.put(1005,new Employee(1005,"后勤的人","15874833156@qq.com",0,new Department(105,"后勤部")));
}
//主键自增
private static Integer initId= 1006;
//增加
public void save(Employee employee){
if (employee.getId() == null){
employee.setId(initId++);
}
employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));
employees.put(employee.getId(),employee);
}
//查询全部员工
public Collection<Employee> getAll(){
return employees.values();
}
public Employee getEmployeeById(Integer id){
return employees.get(id);
}
public void delete(Integer id){
employees.remove(id);
}
}
pojo
Department
package com.atguigu.springboot.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Objects;
//部门表
@Data
public class Department {
private Integer id;
private String departmentName;
public Department(Integer id, String departmentName) {
this.id = id;
this.departmentName = departmentName;
}
public Department() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
@Override
public String toString() {
return "Department{" +
"id=" + id +
", departmentName='" + departmentName + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Department that = (Department) o;
return Objects.equals(id, that.id) &&
Objects.equals(departmentName, that.departmentName);
}
@Override
public int hashCode() {
return Objects.hash(id, departmentName);
}
}
Employee
package com.atguigu.springboot.pojo;
import lombok.Data;
import java.util.Date;
import java.util.Objects;
@Data
//员工表
public class Employee {
private Integer id;
private String lastname;
private String email;
private Integer gender;
private Department department;
private Date birth;
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();
}
public Employee() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee employee = (Employee) o;
return Objects.equals(id, employee.id) &&
Objects.equals(lastname, employee.lastname) &&
Objects.equals(email, employee.email) &&
Objects.equals(gender, employee.gender) &&
Objects.equals(department, employee.department) &&
Objects.equals(birth, employee.birth);
}
@Override
public int hashCode() {
return Objects.hash(id, lastname, email, gender, department, birth);
}
}