模拟数据库
pojo层
1.Employee
package com.liang.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 gender;//0女 1男
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();//默认日期
}
}
Department部门表
package com.liang.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
//部门表
@Data
@AllArgsConstructor //有参
@NoArgsConstructor //无参
public class Department {
private Integer id;
private String departmentName;
}
dao层
Employee员工表
package com.liang.dao;
import com.liang.pojo.Department;
import com.liang.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;
//员工dao
@Repository
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,"a","1212121@qq.com",0,new Department(1001,"研发部")));
employees.put(1002,new Employee(1002,"a","1212121@qq.com",1,new Department(1002,"java部")));
employees.put(1003,new Employee(1003,"a","1212121@qq.com",1,new Department(1003,"学习部")));
employees.put(1004,new Employee(1004,"a","1212121@qq.com",0,new Department(1004,"快乐部")));
employees.put(1005,new Employee(1005,"a","1212121@qq.com",1,new Department(1005,"开心部")));
}
//主键自增
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();
}
//通过id查找员工
public Employee getEmplyeeById(Integer id){
return employees.get(id);
}
//删除员工
public void delete(Integer id){
employees.remove(id);
}
}
Department部门表
package com.liang.dao;
import com.liang.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,"java部"));
}
//获得所有部门的信息
public Collection<Department> getDepartments(){
return departments.values();
}
//通过id得到部门
public Department getDepartmentById(Integer id){
return departments.get(id);
}
}
员工管理系统


谨记啊自己改的地方如果有问题,一定要首先看自己改的地方
模拟书籍库
dao层
DepartmentDao
package com.liang.dao;
import com.liang.pojo.Department;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
//部门dao
@Repository //通过这个注解注入给spring托管
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> getDepartments(){
return departments.values();
}
//通过id得到部门
public Department getDepartmentById(Integer id){
return departments.get(id);
}
}
EmployeeDao
package com.liang.dao;
import com.liang.pojo.Department;
import com.liang.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;
//员工dao
@Repository
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,"a","1212121@qq.com",0,new Department(1001,"研发部")));
employees.put(1002,new Employee(1002,"a","1212121@qq.com",1,new Department(1002,"java部")));
employees.put(1003,new Employee(1003,"a","1212121@qq.com",1,new Department(1003,"学习部")));
employees.put(1004,new Employee(1004,"a","1212121@qq.com",0,new Department(1004,"快乐部")));
employees.put(1005,new Employee(1005,"a","1212121@qq.com",1,new Department(1005,"开心部")));
}
//主键自增
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();
}
//通过id查找员工
public Employee getEmployeeById(Integer id){
return employees.get(id);
}
//删除员工
public void delete(Integer id){
employees.remove(id);
}
}
pojo层
package com.liang.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
//部门表
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Department {
private Integer id;
private String departmentName;
}
package com.liang.pojo;
import lombok.AllArgsConstructor;
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 gender;//0:女 1:男
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();
}
}
首页
- 首页配置:注意点,所以页面的静态资源都需要使用thymeleaf接管;
- @{}
首先在每个页面开头导入thymeleaf命名空间,不然无法生效
<html xmlns:th="http://www.thymeleaf.org">
创建一个config配置类自定义扩展类
MyMvcConfig
package com.liang.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
//如果我们要扩展springmvc,官方建议我们这样去做!
@Configuration
//@EnableWebMvc //这玩意导入一个类:DelegatingWebMvcConfiguration:从容器中获取所有的WebMvcconfig
public class MyMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("login");
registry.addViewController("/index.html").setViewName("index");
}
}
在application.properties配置文件这里配置访问首页
server.servlet.context-path=/liang

页面国际化
首先你的idea得保证这些配置编码都是utf-8



按钮默认的值是value
原登录页面

国际化后

thymeleaf 链接用 @符号


国际化注意点:
- 我们需要配置i18n配置文件
- 我们如果需要在项目中进行按钮自动切换,我们需要定义一个组件LocaleResolver
- 记得将自己写的组件配置到spring容器中@bean
- #{}
登录功能实现
首先改登录页面看是否能跳转成功,再继续写下面的代码

Model 回显参数


<p style="color: #34ce57" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p>

拦截器
显示session登录的信息,就是用户名

登录controller代码
package com.liang.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("login-username") String loginUsername, @RequestParam("login-password") String loginPassword,
Model model, HttpSession session){
//具体的业务
if (!StringUtils.isEmpty(loginUsername) && "123456".equals(loginPassword)){
session.setAttribute("loginUser", loginUsername);
return "404";
}else {
//告诉用户,你登录失败了
model.addAttribute("msg","用户名或者密码错误");
return "index";
}
}
}
登录拦截器LoginHandlerInterceptor
package com.liang.config;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginHandlerInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//登录成功之后,应该有用户的session;
Object loginuser = request.getSession().getAttribute("loginuser");
if (loginuser==null){//没有session信息,没有登录保存到
request.setAttribute("msg", "没有权限,请先登录");
System.out.println("session==>"+loginuser);
request.getRequestDispatcher("/index.html").forward(request,response);
return false;//session为空,拦截器不放行
}else {
return true;
}
}
}
自定义扩展类 添加拦截器
package com.liang.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleContextResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
//如果我们要扩展springmvc,官方建议我们这样去做!
@Configuration
//@EnableWebMvc //这玩意导入一个类:DelegatingWebMvcConfiguration:从容器中获取所有的WebMvcconfig
public class MyMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("login");
registry.addViewController("/index.html").setViewName("index");
registry.addViewController("/main.html").setViewName("404");
}
// //自定义的国际化组件就生效了!
// public LocaleContextResolver localeContextResolver(){
// return new MyLocaleResolver();
// }
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginHandlerInterceptor())
.addPathPatterns("/**")//所有东西都拦截
.excludePathPatterns("/index.html","/","/user/login","css/*","/js/**","/img/**");//这些资源将被放行
}
}
展示员工列表

创建公共文件夹,提取出公共的代码,然后 以组件的形式插入各个页面中,提高代码复用性、
遍历数据到前端

高亮和三元表达式

遍历数据到前端

三元表达式判断男还是女
格式化日期

添加修改员工
按钮,跳转到一个页面,
<h2><a class="btn btn-sm btn-success" th:href="@{/emp}">添加员工</a> </h2>
点击添加会跳到这页面






EmployeeController员工
package com.liang.controller;
import com.liang.dao.DepartmentDao;
import com.liang.dao.EmployeeDao;
import com.liang.pojo.Department;
import com.liang.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.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Collection;
@Controller
public class EmployeeController {
@Autowired
EmployeeDao employeeDao;
@Autowired
DepartmentDao departmentDao;
@RequestMapping("/emps")
public String list(Model model){
Collection<Employee> employees = employeeDao.getAll();
model.addAttribute("emps",employees);
return "/emp/charts";
}
@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){
System.out.println("save+>"+employee);
//添加操作 forward
employeeDao.save(employee);//调用底层业务方法保存员工信息
return "redirect:/emps";
}
}

删除员工

注销

LoginController登录代码
package com.liang.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("login-username") String loginUsername, @RequestParam("login-password") String loginPassword,
Model model, HttpSession session){
//具体的业务
if (!StringUtils.isEmpty(loginUsername) && "123456".equals(loginPassword)){
session.setAttribute("loginUser", loginUsername);
return "404";
}else {
//告诉用户,你登录失败了
model.addAttribute("msg","用户名或者密码错误");
return "index";
}
}
@RequestMapping("/user/logout")
public String logout(HttpSession session){
session.invalidate();
return "redirect:/index.html";
}
}
EmployeeController员工管理代码
package com.liang.controller;
import com.liang.dao.DepartmentDao;
import com.liang.dao.EmployeeDao;
import com.liang.pojo.Department;
import com.liang.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 {
@Autowired
EmployeeDao employeeDao;
@Autowired
DepartmentDao departmentDao;
@RequestMapping("/emps")
public String list(Model model){
Collection<Employee> employees = employeeDao.getAll();
model.addAttribute("emps",employees);
return "/emp/charts";
}
@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){
System.out.println("save+>"+employee);
//添加操作 forward
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") int id){//@PathVariable("id") 路径变量
employeeDao.delete(id);
return "redirect:/emps";
}
}
后台模板:x-admin

该博客展示了如何使用Spring Boot构建一个简单的员工和部门管理应用。通过模拟数据库,实现了DAO层的 Employee 和 Department 类,包含增删查改操作。同时,配置了拦截器实现登录验证,并在登录控制器中处理登录和登出功能。此外,还涉及到页面国际化、Thymeleaf模板引擎的使用以及员工信息的前端展示。
602

被折叠的 条评论
为什么被折叠?



