Springboot-MVC项目搭建过程
1、项目工作
-
建包。包括:config、controller、dao、pojo
资源文件下:public、resources
-
导jar包
<!--Thymeleaf--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency>
-
编写pojo层
//部门表 @Data @AllArgsConstructor @NoArgsConstructor public class Department { private Integer id; private String departmentName; }
//员工表 @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(); } }
-
编写dao层
通过static静态来模拟数据库操作,通过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> getDepartments(){ return departments.values(); } //通过id得到部门 public Department getDepartmentById(Integer id){ return departments.get(id); } }
//员工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, "AA", "323823832@qq.com", 1, new Department(101, "教学部"))); employees.put(1002, new Employee(1002, "BB", "23231@qq.com", 0, new Department(102, "市场部"))); employees.put(1003, new Employee(1003, "CC", "757466@qq.com", 1, new Department(103, "教研部"))); employees.put(1004, new Employee(1004, "DD", "26465@qq.com", 0, new Department(104, "运营部"))); employees.put(1005, new Employee(1005, "EE", "827254@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())); 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); } }
-
编写首页(登录功能)
-
编写config层,实现添加视图跳转和自定义组件生效(语言切换)
@Configuration public class MyMvcConfig implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("index"); registry.addViewController("/index.html").setViewName("index"); registry.addViewController("/main.html").setViewName("dashboard"); } //自定义的国际化组件就生效了 @Bean public LocaleResolver localeResolver(){ return new MyLocaleResolver(); } }
-
实现登录功能
-
设置拦截器
-
列表展示
- 列表循环展示
- 添加员工
- 按钮提交
- 跳转到添加页面
- 添加员工成功
- 返回首页
- CRUD
-
404
2、前端
- 模板:别人写好的,我们拿来改成自己需要的
- 框架:组件:自己手动组合拼接!Bootstrap,Layui,semantic-ui