文章目录
需求分析
数据模型
代码开发
结构搭建
实体类Category.java
package com.taotao.reggie.entity;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* 分类
*/
@Data
public class Category implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
//类型 1 菜品分类 2 套餐分类
private Integer type;
//分类名称
private String name;
//顺序
private Integer sort;
//创建时间
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
//更新时间
@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;
//创建人
@TableField(fill = FieldFill.INSERT)
private Long createUser;
//修改人
@TableField(fill = FieldFill.INSERT_UPDATE)
private Long updateUser;
}
Mapper接口开发
package com.taotao.reggie.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.taotao.reggie.entity.Category;
import org.apache.ibatis.annotations.Mapper;
/**
* create by 刘鸿涛
* 2022/6/7 7:07
*/
@SuppressWarnings({
"all"})
@Mapper
public interface CategoryMapper extends BaseMapper<Category> {
}
业务层接口CategoryService.java
package com.taotao.reggie.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.taotao.reggie.entity.Category;
/**
* create by 刘鸿涛
* 2022/6/7 7:10
*/
@SuppressWarnings({
"all"})
public interface CategoryService extends IService<Category> {
}
业务层实现类CategoryServiceImpl.java
package com.taotao.reggie.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taotao.reggie.entity.Category;
import com.taotao.reggie.mapper.CategoryMapper;
import com.taotao.reggie.service.CategoryService;
import org.springframework.stereotype.Service;
/**
* create by 刘鸿涛
* 2022/6/7 7:12
*/
@SuppressWarnings({
"all"})
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
}
控制层CategoryController.java
package com.taotao.reggie.controller;
import com.taotao.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* create by 刘鸿涛
* 2022/6/7 8:09
* 分类管理
*/
@SuppressWarnings({
"all"})
@RestController
@RequestMapping("/category")
public class CategoryController {
@Autowired
private CategoryService categoryService;
}
新增分类-功能开发
前端分析
每个功能对应一个type
“菜品分类”为1
“套餐分类”为2
新增分类方法CategoryController.java
/**
* 新增分类
* @param category
* @return
*/
@PostMapping
public R<String> save(@RequestBody Category category){
log.info("category:{}",category);
categoryService.save(category);
return R.success("新增分类成功");
}
功能测试
遇到问题
再次添加
因为我们的全局异常处理在起作用,(这就是全局异常处理的亮点)
在数据库中删除再次添加即可