完成新增菜品分类
前言
1、前期准备

定义实体类和实体对象

java
package com.example.ruiji_demo.entity;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;
import java.time.LocalDateTime;
/**
* 分类
* @author jitwxs
* @date 2024年04月01日 21:30
*/
@Data
public class Category {
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;
}

java
package com.example.ruiji_demo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.ruiji_demo.entity.Category;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface CategoryMapper extends BaseMapper<Category> {
}

java
package com.example.ruiji_demo.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.ruiji_demo.entity.Category;
import com.example.ruiji_demo.mapper.CategoryMapper;
import com.example.ruiji_demo.service.CategoryService;
import org.springframework.stereotype.Service;
/**
* @author jitwxs
* @date 2024年04月01日 21:44
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
}

java
package com.example.ruiji_demo.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.ruiji_demo.entity.Category;
public interface CategoryService extends IService<Category> {
}
2、创建修改的接口
由于我们之前写过字段自动补全添加修改和创建的id和时间

java
package com.example.ruiji_demo.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.ruiji_demo.common.R;
import com.example.ruiji_demo.entity.Category;
import com.example.ruiji_demo.service.CategoryService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
/**
* @author jitwxs
* @date 2024年04月01日 21:53
*/
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
// 注册创建的接口实体类
@Autowired
private CategoryService categoryService;
@PostMapping
public R<String> CreateCp(HttpServletRequest request, @RequestBody Category category){
// 获取用户id
categoryService.save(category);
return R.success("添加成功");
}
}

