素材巴巴 > 程序开发 >

Springboot之整合Springmvc

程序开发 2023-09-11 19:31:43

 结构:

config:用来存放springmvc配置

package com.example.shiper.config;import org.springframework.context.annotation.Configuration;
 import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 @Configuration
 public class MvcConfiguation implements WebMvcConfigurer {@Overridepublic void addViewControllers(ViewControllerRegistry registry) {registry.addViewController("/").setViewName("login");}
 }
 

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

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


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

package com.example.shiper.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;@Controller
 public class LoginController {@RequestMapping("/user/demo")public String login(@RequestParam("username") String username,@RequestParam("password") String password,Model model){if (!StringUtils.isEmpty(username) && "123456".equals(password)){return "index";}else {model.addAttribute("msg","用户名或密码错误");return "da";}}
 }
 

dao:用来操作数据库

DepatrmentDao类

package com.example.shiper.dao;import com.example.shiper.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;//部门dao@Repository
 public class DepartmentDao {//模拟数据库中的数据private static Map departments = null;static {departments = new HashMap();//创建一个部门表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 getDepartments(){return departments.values();}//通过id得到部门public Department getDepartmentByID(Integer id){return departments.get(id);}}
 

EmployeeDao类 

底层数据库方法

package com.example.shiper.dao;import com.example.shiper.pojo.Department;
 import com.example.shiper.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
 public class EmployeeDao {//模拟数据库中的数据private static Map employeeMap = null;@Autowiredprivate  DepartmentDao departmentDao;static {employeeMap = new HashMap();//创建一个部门表employeeMap.put(101, new Employee(1001,"AA","21361@qq.com",0,new Department(101,"教学部")));employeeMap.put(102, new Employee(1002,"BB","21361@qq.com",1,new Department(102,"教研部")));employeeMap.put(103, new Employee(1003,"CC","21361@qq.com",1,new Department(103,"市场部")));employeeMap.put(104, new Employee(1004,"DD","21361@qq.com",0,new Department(104,"运营部")));employeeMap.put(105, new Employee(1005,"EE","21361@qq.com",1,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()));employeeMap.put(employee.getId(),employee);}//查询全部员工信息public Collection getAll(){return  employeeMap.values();}//通过id查询员工public Employee getEmployeeById(Integer id){return employeeMap.get(id);}//删除员工public void  delete(Integer id){employeeMap.remove(id);}
 }
 

pojo:存放实体类


Department类
package com.example.shiper.pojo;import lombok.AllArgsConstructor;
 import lombok.Data;
 import lombok.NoArgsConstructor;//部门表
 @Data
 @AllArgsConstructor
 @NoArgsConstructor
 public class Department {private Integer id;private String name;
 }
 

Employee类

底层数据库方法实现

package com.example.shiper.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();}
 }
 


标签:

上一篇: 2022 前端发展趋势 下一篇:
素材巴巴 Copyright © 2013-2021 http://www.sucaibaba.com/. Some Rights Reserved. 备案号:备案中。