当前位置:   article > 正文

Springboot之整合Springmvc_springboot整合springmvc

springboot整合springmvc

 结构:

config:用来存放springmvc配置

  1. package com.example.shiper.config;
  2. import org.springframework.context.annotation.Configuration;
  3. import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
  4. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  5. @Configuration
  6. public class MvcConfiguation implements WebMvcConfigurer {
  7. @Override
  8. public void addViewControllers(ViewControllerRegistry registry) {
  9. registry.addViewController("/").setViewName("login");
  10. }
  11. }

 实现WebMvcConfigurer接口重写其内部方法

 上图重写了添加视图控制器


controller:用来控制页面的逻辑跳转功能

  1. package com.example.shiper.controller;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.ui.Model;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RequestParam;
  6. import org.thymeleaf.util.StringUtils;
  7. @Controller
  8. public class LoginController {
  9. @RequestMapping("/user/demo")
  10. public String login(
  11. @RequestParam("username") String username,
  12. @RequestParam("password") String password,
  13. Model model)
  14. {
  15. if (!StringUtils.isEmpty(username) && "123456".equals(password))
  16. {
  17. return "index";
  18. }
  19. else {
  20. model.addAttribute("msg","用户名或密码错误");
  21. return "da";
  22. }
  23. }
  24. }

dao:用来操作数据库

DepatrmentDao类

  1. package com.example.shiper.dao;
  2. import com.example.shiper.pojo.Department;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Repository;
  5. import java.util.Collection;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. //部门dao
  9. @Repository
  10. public class DepartmentDao {
  11. //模拟数据库中的数据
  12. private static Map<Integer, Department> departments = null;
  13. static {
  14. departments = new HashMap<Integer, Department>();//创建一个部门表
  15. departments.put(101, new Department(101, "教学部"));
  16. departments.put(102, new Department(102, "教研部"));
  17. departments.put(103, new Department(103, "市场部"));
  18. departments.put(104, new Department(104, "运营部"));
  19. departments.put(105, new Department(105, "后勤部"));
  20. }
  21. //获得所有部门信息
  22. public Collection<Department> getDepartments(){
  23. return departments.values();
  24. }
  25. //通过id得到部门
  26. public Department getDepartmentByID(Integer id){
  27. return departments.get(id);
  28. }
  29. }

EmployeeDao类 

底层数据库方法

  1. package com.example.shiper.dao;
  2. import com.example.shiper.pojo.Department;
  3. import com.example.shiper.pojo.Employee;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Repository;
  6. import java.util.Collection;
  7. import java.util.HashMap;
  8. import java.util.Map;
  9. @Repository
  10. public class EmployeeDao {
  11. //模拟数据库中的数据
  12. private static Map<Integer, Employee> employeeMap = null;
  13. @Autowired
  14. private DepartmentDao departmentDao;
  15. static {
  16. employeeMap = new HashMap<Integer, Employee>();//创建一个部门表
  17. employeeMap.put(101, new Employee(1001,"AA","21361@qq.com",0,new Department(101,"教学部")));
  18. employeeMap.put(102, new Employee(1002,"BB","21361@qq.com",1,new Department(102,"教研部")));
  19. employeeMap.put(103, new Employee(1003,"CC","21361@qq.com",1,new Department(103,"市场部")));
  20. employeeMap.put(104, new Employee(1004,"DD","21361@qq.com",0,new Department(104,"运营部")));
  21. employeeMap.put(105, new Employee(1005,"EE","21361@qq.com",1,new Department(105,"后勤部")));
  22. }
  23. //主键自增
  24. private static Integer initId = 1006;
  25. //增加员工
  26. public void save(Employee employee){
  27. if (employee.getId()==null){
  28. employee.setId(initId++);
  29. }
  30. employee.setDepartment(departmentDao.getDepartmentByID(employee.getDepartment().getId()));
  31. employeeMap.put(employee.getId(),employee);
  32. }
  33. //查询全部员工信息
  34. public Collection<Employee> getAll(){
  35. return employeeMap.values();
  36. }
  37. //通过id查询员工
  38. public Employee getEmployeeById(Integer id){
  39. return employeeMap.get(id);
  40. }
  41. //删除员工
  42. public void delete(Integer id){
  43. employeeMap.remove(id);
  44. }
  45. }

pojo:存放实体类


Department类
  1. package com.example.shiper.pojo;
  2. import lombok.AllArgsConstructor;
  3. import lombok.Data;
  4. import lombok.NoArgsConstructor;
  5. //部门表
  6. @Data
  7. @AllArgsConstructor
  8. @NoArgsConstructor
  9. public class Department {
  10. private Integer id;
  11. private String name;
  12. }

Employee类

底层数据库方法实现

  1. package com.example.shiper.pojo;
  2. import lombok.AllArgsConstructor;
  3. import lombok.Data;
  4. import lombok.NoArgsConstructor;
  5. import java.util.Date;
  6. @Data
  7. @NoArgsConstructor
  8. //员工表
  9. public class Employee {
  10. private Integer id;
  11. private String lastName;
  12. private String email;
  13. private Integer gender; //0 ;女 1:男
  14. private Department department;
  15. private Date birth;
  16. public Employee(Integer id, String lastName, String email, Integer gender, Department department ) {
  17. this.id = id;
  18. this.lastName = lastName;
  19. this.email = email;
  20. this.gender = gender;
  21. this.department = department;
  22. //默认日期
  23. this.birth = new Date();
  24. }
  25. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/435313
推荐阅读
相关标签
  

闽ICP备14008679号