瑞吉外卖 03 CRUD分类

公共字段自动填充

预准备

image

image

实现步骤:

image

image

image

image

image

image

代码实现

在commons下创建MyMetaObjecthandler类,编写:

package com.itheima.reggie.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author 自定义元数据对象处理器
 * @create 2023-06-25 22:40
 */
@Component
@Slf4j
public class MyMetaObjecthandler implements MetaObjectHandler{
    
    
    /**
     * 插入操作,自动填充
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
    
    
        log.info("公共字段自动填充[insert]");
        log.info(metaObject.toString());
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("createUser", BaseContext.getCurrentId());
        metaObject.setValue("updateUser", BaseContext.getCurrentId());
    }

    /**
     * 更新操作,自动填充
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
    
    
        log.info("公共字段自动填充[update]");
        log.info(metaObject.toString());

        long id =Thread.currentThread().getId();
        log.info("线程id为{}",id);

        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateUser", BaseContext.getCurrentId());
    }
}

在commons创建BaseContext类,编写:

package com.itheima.reggie.common;

/**
 * @author 基于ThreadLocal封装工具类,用户保存和获取当前登录用户的id
 * @create 2023-06-26 10:38
 */
public class BaseContext {
    
    
    private static ThreadLocal<Long> threadLocal=new ThreadLocal<>();

    /**
     * 设置值
     * @param id
     */
    public static void setCurrentId(Long id){
    
    
        threadLocal.set(id);
    }

    /**
     * 获取值
     * @return
     */
    public static Long getCurrentId(){
    
    
        return threadLocal.get();
    }
}

在filter下将id放入线程当中:


        //4.判断登陆状态,如果已登录,则直接放行
        if(request.getSession().getAttribute("employee")!=null){
    
    
            log.info("用户已登录,用户id为:{}",request.getSession().getAttribute("employee"));

            //存入id到线程当中
            Long empId=(Long)request.getSession().getAttribute("employee"));
            BaseContext.setCurrentId(empId);

新增分类

预准备

image

image

image

image

image

image

前端代码:

image

代码实现

把资料包下的Category类复制到entity下,创建好相对应的mapper、controller、service和serviceimpl。

在controller下编写:

package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.service.CategoryService;
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;

/**
 * @author 分类管理
 * @create 2023-06-26 17:20
 */
@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
    
    
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类
     * @param category
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Category category){
    
    
        log.info("category:{}",category);
        categoryService.save(category);
        return R.success("新增分类成功");
    }
}

分类信息分页查询

预准备

image

image

前端代码

image

image

代码实现

/**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize){
    
    
        //分页构造器
        Page<Category> pageInfo=new Page<>(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Category> queryWrapper=new LambdaQueryWrapper<>();
        //添加排序条件,根据sort进行排序
        queryWrapper.orderByAsc(Category::getSort);
        //分页查询
        categoryService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);
    }

这里要将category下的is_Deleted字段给注释掉。

删除分类

预准备

image

image

image

image

image

代码开发

在controller下,编写代码:

/**
     * 根据id删除分类
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long ids){
    
    
        log.info("删除分类,id为:{}",ids);

        categoryService.remove(ids);
        return R.success("分类信息删除成功");
    }

但是还要检测要删除的套餐有没有关联的数据。

从资料中复制dish和setmeal到entity下,再创建相应的类。

之前是mybatisplus的iservice接口的一个方法帮我们删除的分类数据,但是我们这里就需要加一个功能,所以在categoryService下面新建一个public void remove(Long id)方法,再在CategoryServiceImpl实现类下面完成具体功能的实现:

package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author shkstart
 * @create 2023-06-26 17:18
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper,Category> implements CategoryService{
    
    
    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    /**
     * 根据ids删除分类,删除之前需要进行判断
     * @param ids
     */
    @Override
    public void remove(Long ids) {
    
    
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper=new LambdaQueryWrapper<>();
        //添加查询条件,根据分类id进行查询
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,ids);
        int count=dishService.count(dishLambdaQueryWrapper);
        //查询当前分类是否关联了菜品,如果已经关联,抛出一个业务异常
        if(count>0){
    
    
            //已经关联菜品,抛出一个业务异常
            throw new CustomException("当前分类下关联了菜品,不能删除!");
        }
        //查询当前分类是否关联了套餐,如果已经关联,抛出一个业务异常
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper=new LambdaQueryWrapper<>();
        //添加查询条件,根据分类id进行查询
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,ids);
        int count2=setmealService.count(setmealLambdaQueryWrapper);
        if(count2>0){
    
    
            //已经关联套餐,抛出一个业务异常
            throw new CustomException("当前分类下关联了套餐,不能删除!");
        }
        //正常删除分类
        super.removeById(ids);
    }
}

在commons新建一个CustomException类,用来抛出异常,编写代码:

package com.itheima.reggie.common;

/**
 * @author 自定义业务异常类
 * @create 2023-06-26 21:55
 */
public class CustomException extends RuntimeException {
    
    
    public CustomException(String message){
    
    
        super(message);
    }
}

修改分类

预准备

image

在弹窗里面回显数据的前端代码:

image

image

image

image

代码实现

在categorycontroller下编写:

/**
     * 根据id查询员工信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
    
    
        log.info("根据id查询员工信息...");
        Employee employee=employeeService.getById(id);
        if(employee!=null) return R.success(employee);
        return R.error("没有查询到对应员工信息!");
    }

猜你喜欢

转载自blog.csdn.net/weixin_46066669/article/details/131406849