当前位置: 首页> 科技> 名企 > 中国计算机软考网_怎么做网页连接数据库显示信息_进行seo网站建设_西昌seo快速排名

中国计算机软考网_怎么做网页连接数据库显示信息_进行seo网站建设_西昌seo快速排名

时间:2025/7/11 23:27:53来源:https://blog.csdn.net/weixin_42739799/article/details/144658038 浏览次数:0次
中国计算机软考网_怎么做网页连接数据库显示信息_进行seo网站建设_西昌seo快速排名

瑞吉外卖项目学习笔记(一)准备工作、员工登录功能实现
瑞吉外卖项目学习笔记(二)Swagger、logback、表单校验和参数打印功能的实现
瑞吉外卖项目学习笔记(三)过滤器实现登录校验、添加员工、分页查询员工信息
瑞吉外卖项目学习笔记(四)@TableField(fill = FieldFill.INSERT)公共字段填充、启用/禁用/修改员工信息

文章目录

  • 8 分类管理
    • 8.1 需求分析
    • 8.2 数据准备
    • 8.3 分页查询分类列表
    • 8.4 新增菜品/套餐分类
    • 8.5 修改分类
    • 8.6 删除分类

8 分类管理

8.1 需求分析


在“分类管理”页面,实现以下功能:

  • 分页查询分类列表
  • 新增菜品/套餐分类
  • 修改分类
  • 删除分类

8.2 数据准备

在数据库新建分类表t_category

-- ----------------------------
-- Table structure for category
-- ----------------------------
DROP TABLE IF EXISTS `t_category`;
CREATE TABLE `t_category` (`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',`type` int(11) DEFAULT NULL COMMENT '类型   1 菜品分类 2 套餐分类',`name` varchar(64) COLLATE utf8_bin NOT NULL COMMENT '分类名称',`sort` int(11) NOT NULL DEFAULT '0' COMMENT '顺序',`create_time` datetime NOT NULL COMMENT '创建时间',`update_time` datetime NOT NULL COMMENT '更新时间',`create_user` bigint(20) NOT NULL COMMENT '创建人',`update_user` bigint(20) NOT NULL COMMENT '修改人',PRIMARY KEY (`id`) USING BTREE,UNIQUE KEY `idx_category_name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='菜品及套餐分类';-- ----------------------------
-- Records of category
-- ----------------------------
INSERT INTO `t_category` VALUES ('1397844263642378242', '1', '湘菜', '1', '2021-05-27 09:16:58', '2021-07-15 20:25:23', '1', '1');
INSERT INTO `t_category` VALUES ('1397844303408574465', '1', '川菜', '2', '2021-05-27 09:17:07', '2021-06-02 14:27:22', '1', '1');
INSERT INTO `t_category` VALUES ('1397844391040167938', '1', '粤菜', '3', '2021-05-27 09:17:28', '2021-07-09 14:37:13', '1', '1');
INSERT INTO `t_category` VALUES ('1413341197421846529', '1', '饮品', '11', '2021-07-09 11:36:15', '2021-07-09 14:39:15', '1', '1');
INSERT INTO `t_category` VALUES ('1413342269393674242', '2', '商务套餐', '5', '2021-07-09 11:40:30', '2021-07-09 14:43:45', '1', '1');
INSERT INTO `t_category` VALUES ('1413384954989060097', '1', '主食', '12', '2021-07-09 14:30:07', '2021-07-09 14:39:19', '1', '1');
INSERT INTO `t_category` VALUES ('1413386191767674881', '2', '儿童套餐', '6', '2021-07-09 14:35:02', '2021-07-09 14:39:05', '1', '1');

使用MyBatisPlus插件生成代码:


Category实体类的公共字段添加注解:

// com.itweid.takeout.entity.Category@ApiModelProperty(value = "创建时间")
@TableField(fill = FieldFill.INSERT)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime createTime;@ApiModelProperty(value = "更新时间")
@TableField(fill = FieldFill.INSERT_UPDATE)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime updateTime;@ApiModelProperty(value = "创建人")
@TableField(fill = FieldFill.INSERT)
private Long createUser;@ApiModelProperty(value = "修改人")
@TableField(fill = FieldFill.INSERT_UPDATE)
private Long updateUser;

创建查询对象CategoryQuery

@Data
@EqualsAndHashCode(callSuper=false)
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(value="CategoryQuery", description="分类信息查询对象")
public class CategoryQuery extends BaseQuery {@ApiModelProperty(value = "ID")private Long id;@ApiModelProperty(value = "类型 1 菜品分类 2 套餐分类")private Integer type;@ApiModelProperty(value = "分类名称")private String name;@ApiModelProperty(value = "顺序")private Integer sort;}

8.3 分页查询分类列表

功能请求方法请求路径请求参数
分页查询分类列表GET/category/page?page=1&pageSize=10
  • 1)在CategoryController类创建page方法
@RestController
@RequestMapping("/category")
@RequiredArgsConstructor
@Api(value = "CategoryController", tags = "分类管理")
@Validated
public class CategoryController {private final ICategoryService categoryService;@ApiOperation("分页查询分类列表")@GetMapping("/page")public BaseResult<Page<Category>> page(CategoryQuery categoryQuery) {return categoryService.pageQueryCategory(categoryQuery);}
}
  • 2)在CategoryServiceImpl类中具体实现pageQueryCategory方法
@Service
@Transactional(rollbackFor = Exception.class)
@Slf4j
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {@Overridepublic BaseResult<Page<Category>> pageQueryCategory(CategoryQuery categoryQuery) {Page<Category> page = new Page<>();lambdaQuery().page(page);return BaseResult.success(page);}
}
  • 3)重启服务,测试分页查询功能

8.4 新增菜品/套餐分类

功能请求方法请求路径请求参数
新增菜品/套餐分类POST/category/add{name: “xxx”, type: 1, sort: 1}
  • 1)在CategoryController类创建add方法,并添加参数校验
// com.itweid.takeout.controller.CategoryController@ApiOperation("新增分类")
@PostMapping("/add")
public BaseResult add(@RequestBody @Validated(Add.class) CategoryQuery categoryQuery) {return categoryService.addCategory(categoryQuery);
}
  • 2)在CategoryServiceImpl类中具体实现addCategory方法
// com.itweid.takeout.service.impl.CategoryServiceImpl@Override
public BaseResult addCategory(CategoryQuery categoryQuery) {// 1 判断名称是否重复Category exCategory = lambdaQuery().eq(Category::getName, categoryQuery.getName()).one();if(exCategory != null) {return BaseResult.error(ErrorCode.CATEGORY_EXIST);}// 2 封装新分类对象(公共字段已由MyBatisPlus自动赋值)Category category = new Category();category.setName(categoryQuery.getName());category.setType(categoryQuery.getType());category.setSort(categoryQuery.getSort());// 3 新增分类save(category);return BaseResult.success();
}
  • 3)重启服务,测试新增菜品/套餐分类功能

8.5 修改分类

功能请求方法请求路径请求参数
修改分类PUT/category/exit{id: 1, name: “湘菜”, sort: 1}
  • 1)在CategoryController类创建edit方法,并添加参数校验
// com.itweid.takeout.controller.CategoryController@ApiOperation("修改分类")
@PutMapping("/edit")
public BaseResult edit(@RequestBody @Validated(Edit.class) CategoryQuery categoryQuery) {return categoryService.editCategory(categoryQuery);
}
  • 2)在CategoryServiceImpl类中具体实现editCategory方法
// com.itweid.takeout.service.impl.CategoryServiceImpl@Override
public BaseResult editCategory(CategoryQuery categoryQuery) {// 1 查询原分类信息Category exCategory = getById(categoryQuery.getId());if(exCategory == null) {return BaseResult.error(ErrorCode.CATEGORY_NOT_EXIST);}// 2 封装新分类对象Category category = new Category();category.setId(exCategory.getId());// 3 判断分类名称是否修改if(!exCategory.getName().equals(categoryQuery.getName())) {category.setName(categoryQuery.getName());}// 4 判断排序是否修改if(!exCategory.getSort().equals(categoryQuery.getSort())) {category.setSort(categoryQuery.getSort());}// 5 根据ID修改分类updateById(category);return BaseResult.success();
}
  • 3)重启服务,测试修改分类功能


发现修改不成功。检查后发现前端传递的ID和数据库的ID不一致,这是因为前端JS对Long类型数据丢失精度导致的。

我们可以给id字段添加一个注解,使得服务端给页面响应json数据前将Long型数据统一转为String字符串:

// com.itweid.takeout.entity.Category@ApiModelProperty(value = "主键")
@TableId(value = "id", type = IdType.AUTO)
@JsonSerialize(using = ToStringSerializer.class)
private Long id;
  • 4)重启服务,测试修改分类功能

8.6 删除分类

功能请求方法请求路径请求参数
删除分类DELETE/category/delete/{id}{id}
  • 1)在CategoryController类创建delete方法,并添加参数校验
// com.itweid.takeout.controller.CategoryController@ApiOperation("删除分类")
@DeleteMapping("/delete/{id}")
public BaseResult delete(@PathVariable @NotNull(message = "分类ID不能为空") Long id) {return categoryService.deleteCategory(id);
}
  • 2)在CategoryServiceImpl类中具体实现deleteCategory方法
// com.itweid.takeout.service.impl.CategoryServiceImpl@Override
public BaseResult deleteCategory(Long id) {boolean remove = removeById(id);return remove ? BaseResult.success() : BaseResult.error(ErrorCode.CATEGORY_NOT_EXIST);
}
  • 3)重启服务,测试删除分类功能

  • 4)功能完善

上面的逻辑是直接删除分类,但正确的逻辑是,当菜品分类或套餐分类关联了其他菜品或套餐时,该分类将不允许被删除。这个功能完善先放着,等菜品、套餐的功能完成后再补上。

本节完,更多内容查阅:瑞吉外卖项目实战

关键字:中国计算机软考网_怎么做网页连接数据库显示信息_进行seo网站建设_西昌seo快速排名

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: