基于SpringBoot+Redis的前后端分离外卖项目-苍穹外卖(四)
作者:mmseoamin日期:2023-12-05

编辑员工和分类模块功能开发

    • 1. 编辑员工
      • 1.1 需求分析与设计
        • 1.1.1 产品原型
        • 1.1.2 接口设计
        • 1.2 代码开发
          • 1.2.1 回显员工信息功能
          • 1.2.2 修改员工信息功能
          • 1.3 功能测试
          • 2. 分类模块功能开发
            • 2.1 需求分析与设计
              • 2.1.1 产品原型
              • 2.1.2 接口设计
              • 2.1.3 表设计
              • 2.2 代码实现
                • 2.2.1 Mapper层
                • 2.2.2 Service层
                • 2.2.3 Controller层
                • 2.3 功能测试

                  1. 编辑员工

                  1.1 需求分析与设计

                  1.1.1 产品原型

                  员工管理列表页面点击 “编辑” 按钮,跳转到编辑页面,在编辑页面回显员工信息并进行修改,最后点击 “保存” 按钮完成编辑操作。

                  修改页面原型:

                  注:点击修改时,数据应该正常回显到修改页面。

                  基于SpringBoot+Redis的前后端分离外卖项目-苍穹外卖(四),在这里插入图片描述,第1张

                  1.1.2 接口设计

                  根据上述原型图分析,编辑员工功能涉及到两个接口:

                  • 根据id查询员工信息
                  • 编辑员工信息

                    1). 根据id查询员工信息

                    基于SpringBoot+Redis的前后端分离外卖项目-苍穹外卖(四),在这里插入图片描述,第2张

                    基于SpringBoot+Redis的前后端分离外卖项目-苍穹外卖(四),在这里插入图片描述,第3张

                    2). 编辑员工信息

                    基于SpringBoot+Redis的前后端分离外卖项目-苍穹外卖(四),在这里插入图片描述,第4张

                    基于SpringBoot+Redis的前后端分离外卖项目-苍穹外卖(四),在这里插入图片描述,第5张

                    注:因为是修改功能,请求方式可设置为PUT。

                    1.2 代码开发

                    1.2.1 回显员工信息功能

                    1). Controller层

                    在 EmployeeController 中创建 getById 方法:

                    	/**
                         * 根据id查询员工信息
                         * @param id
                         * @return
                         */
                        @GetMapping("/{id}")
                        @ApiOperation("根据id查询员工信息")
                        public Result getById(@PathVariable Long id){
                            Employee employee = employeeService.getById(id);
                            return Result.success(employee);
                        }
                    

                    2). Service层接口

                    在 EmployeeService 接口中声明 getById 方法:

                        /**
                         * 根据id查询员工
                         * @param id
                         * @return
                         */
                        Employee getById(Long id);
                    

                    3). Service层实现类

                    在 EmployeeServiceImpl 中实现 getById 方法:

                     	/**
                         * 根据id查询员工
                         *
                         * @param id
                         * @return
                         */
                        public Employee getById(Long id) {
                            Employee employee = employeeMapper.getById(id);
                            employee.setPassword("****");
                            return employee;
                        }
                    

                    4). Mapper层

                    在 EmployeeMapper 接口中声明 getById 方法:

                    	/**
                         * 根据id查询员工信息
                         * @param id
                         * @return
                         */
                        @Select("select * from employee where id = #{id}")
                        Employee getById(Long id);
                    
                    1.2.2 修改员工信息功能

                    1). Controller层

                    在 EmployeeController 中创建 update 方法:

                    	/**
                         * 编辑员工信息
                         * @param employeeDTO
                         * @return
                         */
                        @PutMapping
                        @ApiOperation("编辑员工信息")
                        public Result update(@RequestBody EmployeeDTO employeeDTO){
                            log.info("编辑员工信息:{}", employeeDTO);
                            employeeService.update(employeeDTO);
                            return Result.success();
                        }
                    

                    2). Service层接口

                    在 EmployeeService 接口中声明 update 方法:

                        /**
                         * 编辑员工信息
                         * @param employeeDTO
                         */
                        void update(EmployeeDTO employeeDTO);
                    

                    3). Service层实现类

                    在 EmployeeServiceImpl 中实现 update 方法:

                     	/**
                         * 编辑员工信息
                         *
                         * @param employeeDTO
                         */
                        public void update(EmployeeDTO employeeDTO) {
                            Employee employee = new Employee();
                            BeanUtils.copyProperties(employeeDTO, employee);
                            employee.setUpdateTime(LocalDateTime.now());
                            employee.setUpdateUser(BaseContext.getCurrentId());
                            employeeMapper.update(employee);
                        }
                    

                    1.3 功能测试

                    进入到员工列表查询。

                    基于SpringBoot+Redis的前后端分离外卖项目-苍穹外卖(四),在这里插入图片描述,第6张

                    对员工姓名为杰克的员工数据修改,点击修改,数据已回显。

                    基于SpringBoot+Redis的前后端分离外卖项目-苍穹外卖(四),在这里插入图片描述,第7张

                    修改后,点击保存。

                    基于SpringBoot+Redis的前后端分离外卖项目-苍穹外卖(四),在这里插入图片描述,第8张

                    2. 分类模块功能开发

                    2.1 需求分析与设计

                    2.1.1 产品原型

                    后台系统中可以管理分类信息,分类包括两种类型,分别是 菜品分类 和 套餐分类 。

                    菜品分类相关功能。

                    新增菜品分类:当我们在后台系统中添加菜品时需要选择一个菜品分类,在移动端也会按照菜品分类来展示对应的菜品。

                    菜品分类分页查询:系统中的分类很多的时候,如果在一个页面中全部展示出来会显得比较乱,不便于查看,所以一般的系统中都会以分页的方式来展示列表数据。

                    根据id删除菜品分类:在分类管理列表页面,可以对某个分类进行删除操作。需要注意的是当分类关联了菜品或者套餐时,此分类不允许删除。

                    修改菜品分类:在分类管理列表页面点击修改按钮,弹出修改窗口,在修改窗口回显分类信息并进行修改,最后点击确定按钮完成修改操作。

                    启用禁用菜品分类:在分类管理列表页面,可以对某个分类进行启用或者禁用操作。

                    分类类型查询:当点击分类类型下拉框时,从数据库中查询所有的菜品分类数据进行展示。

                    分类管理原型:

                    基于SpringBoot+Redis的前后端分离外卖项目-苍穹外卖(四),在这里插入图片描述,第9张

                    业务规则:

                    • 分类名称必须是唯一的
                    • 分类按照类型可以分为菜品分类和套餐分类
                    • 新添加的分类状态默认为“禁用”
                      2.1.2 接口设计

                      根据上述原型图分析,菜品分类模块共涉及6个接口。

                      • 新增分类
                      • 分类分页查询
                      • 根据id删除分类
                      • 修改分类
                      • 启用禁用分类
                      • 根据类型查询分类

                        接下来,详细地分析每个接口。

                        1). 新增分类

                        基于SpringBoot+Redis的前后端分离外卖项目-苍穹外卖(四),在这里插入图片描述,第10张

                        基于SpringBoot+Redis的前后端分离外卖项目-苍穹外卖(四),在这里插入图片描述,第11张

                        2). 分类分页查询

                        基于SpringBoot+Redis的前后端分离外卖项目-苍穹外卖(四),在这里插入图片描述,第12张

                        基于SpringBoot+Redis的前后端分离外卖项目-苍穹外卖(四),在这里插入图片描述,第13张

                        3). 根据id删除分类

                        基于SpringBoot+Redis的前后端分离外卖项目-苍穹外卖(四),在这里插入图片描述,第14张

                        4). 修改分类

                        基于SpringBoot+Redis的前后端分离外卖项目-苍穹外卖(四),在这里插入图片描述,第15张

                        基于SpringBoot+Redis的前后端分离外卖项目-苍穹外卖(四),在这里插入图片描述,第16张

                        5). 启用禁用分类

                        基于SpringBoot+Redis的前后端分离外卖项目-苍穹外卖(四),在这里插入图片描述,第17张

                        基于SpringBoot+Redis的前后端分离外卖项目-苍穹外卖(四),在这里插入图片描述,第18张

                        6). 根据类型查询分类

                        基于SpringBoot+Redis的前后端分离外卖项目-苍穹外卖(四),在这里插入图片描述,第19张

                        基于SpringBoot+Redis的前后端分离外卖项目-苍穹外卖(四),在这里插入图片描述,第20张

                        2.1.3 表设计

                        category表结构:

                        字段名数据类型说明备注
                        idbigint主键自增
                        namevarchar(32)分类名称唯一
                        typeint分类类型1菜品分类 2套餐分类
                        sortint排序字段用于分类数据的排序
                        statusint状态1启用 0禁用
                        create_timedatetime创建时间
                        update_timedatetime最后修改时间
                        create_userbigint创建人id
                        update_userbigint最后修改人id

                        2.2 代码实现

                        2.2.1 Mapper层

                        DishMapper.java

                        package com.sky.mapper;
                        @Mapper
                        public interface DishMapper {
                            /**
                             * 根据分类id查询菜品数量
                             * @param categoryId
                             * @return
                             */
                            @Select("select count(id) from dish where category_id = #{categoryId}")
                            Integer countByCategoryId(Long categoryId);
                        }
                        

                        SetmealMapper.java

                        package com.sky.mapper;
                        @Mapper
                        public interface SetmealMapper {
                            /**
                             * 根据分类id查询套餐的数量
                             * @param id
                             * @return
                             */
                            @Select("select count(id) from setmeal where category_id = #{categoryId}")
                            Integer countByCategoryId(Long id);
                        }
                        

                        CategoryMapper.java

                        package com.sky.mapper;
                        import java.util.List;
                        @Mapper
                        public interface CategoryMapper {
                            /**
                             * 插入数据
                             * @param category
                             */
                            @Insert("insert into category(type, name, sort, status, create_time, update_time, create_user, update_user)" +
                                    " VALUES" +
                                    " (#{type}, #{name}, #{sort}, #{status}, #{createTime}, #{updateTime}, #{createUser}, #{updateUser})")
                            void insert(Category category);
                            /**
                             * 分页查询
                             * @param categoryPageQueryDTO
                             * @return
                             */
                            Page pageQuery(CategoryPageQueryDTO categoryPageQueryDTO);
                            /**
                             * 根据id删除分类
                             * @param id
                             */
                            @Delete("delete from category where id = #{id}")
                            void deleteById(Long id);
                            /**
                             * 根据id修改分类
                             * @param category
                             */
                            void update(Category category);
                            /**
                             * 根据类型查询分类
                             * @param type
                             * @return
                             */
                            List findByType(Integer type);
                        }
                        

                        CategoryMapper.xml,进入到resources/mapper目录下

                        
                        
                        
                            
                            
                                update category
                                
                                    
                                        type = #{type},
                                    
                                    
                                        name = #{name},
                                    
                                    
                                        sort = #{sort},
                                    
                                    
                                        status = #{status},
                                    
                                    
                                        update_time = #{updateTime},
                                    
                                    
                                        update_user = #{updateUser}
                                    
                                
                                where id = #{id}
                            
                            
                        
                        
                        2.2.2 Service层

                        CategoryService.java

                        package com.sky.service;
                        public interface CategoryService {
                            /**
                             * 新增分类
                             * @param categoryDTO
                             */
                            void save(CategoryDTO categoryDTO);
                            /**
                             * 分页查询
                             * @param categoryPageQueryDTO
                             * @return
                             */
                            PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO);
                            /**
                             * 根据id删除分类
                             * @param id
                             */
                            void deleteById(Long id);
                            /**
                             * 修改分类
                             * @param categoryDTO
                             */
                            void update(CategoryDTO categoryDTO);
                            /**
                             * 启用、禁用分类
                             * @param status
                             * @param id
                             */
                            void startOrStop(Integer status, Long id);
                            /**
                             * 根据类型查询分类
                             * @param type
                             * @return
                             */
                            List findByType(Integer type);
                        }
                        

                        CategoryServiceImpl.java

                        @Service
                        public class CategoryServiceImpl implements CategoryService {
                        @Autowired
                        private CategoryMapper categoryMapper;
                        @Autowired
                        private SetmealMapper setmealMapper;
                        @Autowired
                        private DishMapper dishMapper;
                        //菜品添加
                            @Override
                            public void add(CategoryDTO categoryDTO) {
                                Category category=new Category();
                                BeanUtils.copyProperties(categoryDTO,category);
                                category.setCreateTime(LocalDateTime.now());
                                category.setUpdateTime(LocalDateTime.now());
                                category.setUpdateUser(BaseContext.getCurrentId());
                                category.setCreateUser(BaseContext.getCurrentId());
                                category.setStatus(StatusConstant.DISABLE);
                                categoryMapper.add(category);
                            }
                           //菜品分页查询
                            @Override
                            public PageResult page(CategoryPageQueryDTO categoryPageQueryDTO) {
                                PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
                                  Page page= categoryMapper.page(categoryPageQueryDTO);
                                  PageResult pageResult=new PageResult();
                                  pageResult.setTotal(page.getTotal());
                                  pageResult.setRecords( page.getResult());
                                return pageResult;
                            }
                        //启用禁用
                            @Override
                            public void startOrStop(Integer status, Long id) {
                                Category category = Category.builder().id(id).status(status).build();
                                categoryMapper.update(category);
                            }
                                 //根据类型查询
                            @Override
                            public List findByType(Integer type) {
                                List categoryList=categoryMapper.findByType(type);
                                return categoryList;
                            }
                        //菜品的删除
                            @Override
                            public void delete(Long id) {
                        if (dishMapper.countByCategoryid(id)>0){
                        throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
                        }if (setmealMapper.selectCount(id)>0){
                            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
                                }
                                categoryMapper.delete(id);
                            }
                          //菜品的修改
                            @Override
                            public void update( CategoryDTO categoryDTO) {
                                Category category=new Category();
                                BeanUtils.copyProperties(categoryDTO,category);
                                category.setUpdateTime(LocalDateTime.now());
                                category.setUpdateUser(BaseContext.getCurrentId());
                                categoryMapper.update(category);
                            }
                        }
                        
                        2.2.3 Controller层

                        CategoryController.java

                        package com.sky.controller.admin;
                        /**
                         * 分类管理
                         */
                        @RestController
                        @RequestMapping("/admin/category")
                        @Api(tags = "分类相关接口")
                        @Slf4j
                        public class CategoryController {
                            @Autowired
                            private CategoryService categoryService;
                            /**
                             * 新增分类
                             * @param categoryDTO
                             * @return
                             */
                            @PostMapping
                            @ApiOperation("新增分类")
                            public Result save(@RequestBody CategoryDTO categoryDTO){
                                log.info("新增分类:{}", categoryDTO);
                                categoryService.save(categoryDTO);
                                return Result.success();
                            }
                            /**
                             * 分类分页查询
                             * @param categoryPageQueryDTO
                             * @return
                             */
                            @GetMapping("/page")
                            @ApiOperation("分类分页查询")
                            public Result page(CategoryPageQueryDTO categoryPageQueryDTO){
                                log.info("分页查询:{}", categoryPageQueryDTO);
                                PageResult pageResult = categoryService.pageQuery(categoryPageQueryDTO);
                                return Result.success(pageResult);
                            }
                            /**
                             * 删除分类
                             * @param id
                             * @return
                             */
                            @DeleteMapping
                            @ApiOperation("删除分类")
                            public Result deleteById(Long id){
                                log.info("删除分类:{}", id);
                                categoryService.deleteById(id);
                                return Result.success();
                            }
                            /**
                             * 修改分类
                             * @param categoryDTO
                             * @return
                             */
                            @PutMapping
                            @ApiOperation("修改分类")
                            public Result update(@RequestBody CategoryDTO categoryDTO){
                                categoryService.update(categoryDTO);
                                return Result.success();
                            }
                            /**
                             * 启用、禁用分类
                             * @param status
                             * @param id
                             * @return
                             */
                            @PostMapping("/status/{status}")
                            @ApiOperation("启用禁用分类")
                            public Result startOrStop(@PathVariable("status") Integer status, Long id){
                                categoryService.startOrStop(status,id);
                                return Result.success();
                            }
                            /**
                             * 根据类型查询分类
                             * @param type
                             * @return
                             */
                            @GetMapping("/list")
                            @ApiOperation("根据类型查询分类")
                            public Result> list(Integer type){
                                List list = categoryService.list(type);
                                return Result.success(list);
                            }
                        }
                        

                        2.3 功能测试

                        重启服务,访问http://localhost:80,进入分类管理

                        分页查询:

                        基于SpringBoot+Redis的前后端分离外卖项目-苍穹外卖(四),在这里插入图片描述,第21张

                        分类类型:

                        基于SpringBoot+Redis的前后端分离外卖项目-苍穹外卖(四),在这里插入图片描述,第22张

                        启用禁用:

                        基于SpringBoot+Redis的前后端分离外卖项目-苍穹外卖(四),在这里插入图片描述,第23张

                        点击禁用

                        基于SpringBoot+Redis的前后端分离外卖项目-苍穹外卖(四),在这里插入图片描述,第24张

                        修改:

                        回显

                        基于SpringBoot+Redis的前后端分离外卖项目-苍穹外卖(四),在这里插入图片描述,第25张

                        修改后

                        基于SpringBoot+Redis的前后端分离外卖项目-苍穹外卖(四),在这里插入图片描述,第26张

                        新增:

                        基于SpringBoot+Redis的前后端分离外卖项目-苍穹外卖(四),在这里插入图片描述,第27张

                        点击确定,查询列表

                        基于SpringBoot+Redis的前后端分离外卖项目-苍穹外卖(四),在这里插入图片描述,第28张

                        删除:

                        基于SpringBoot+Redis的前后端分离外卖项目-苍穹外卖(四),在这里插入图片描述,第29张

                        删除后,查询分类列表

                        基于SpringBoot+Redis的前后端分离外卖项目-苍穹外卖(四),在这里插入图片描述,第30张

                        删除成功

                        后记

                        👉👉💕💕美好的一天,到此结束,下次继续努力!欲知后续,请看下回分解,写作不易,感谢大家的支持!! 🌹🌹🌹