Springboot搭建MVC项目过程

Springboot-MVC项目搭建过程

1、项目工作
  1. 建包。包括:config、controller、dao、pojo

    资源文件下:public、resources

  2. 导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>
    
  3. 编写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();
        }
    }
    
  4. 编写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);
        }
    
    }
    
    
  5. 编写首页(登录功能)

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-k5OdAtHD-1666932617801)(img/image-20221025181900257.png)]

  6. 编写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();
        }
    }
    
  7. 实现登录功能

  8. 设置拦截器

  9. 列表展示

    • 列表循环展示
    • 添加员工
      • 按钮提交
      • 跳转到添加页面
      • 添加员工成功
      • 返回首页
    • CRUD
  10. 404

2、前端
  • 模板:别人写好的,我们拿来改成自己需要的
  • 框架:组件:自己手动组合拼接!Bootstrap,Layui,semantic-ui
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值