1.在实体类中,需要填充的字段加上@TableField注解
2.新建一个类实现
/**
* 基于ThreadLocal封装工具类,用户保存和获取当前用户id
*/
public class BaseContext {
private static ThreadLocal<Long> threadLocal = new ThreadLocal<>();
public static void setCurrentId(Long id){
threadLocal.set(id);
}
public static Long getCurrentId(){
return threadLocal.get();
}
}
菜品分类页面分析
/**
* 分页展示
* @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);
}
删除分类要判断该分类是否包含菜品或者套餐信息
在CategoryServiceImpl添加方法,进行判断是否删除
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
@Autowired
private DishService dishService;
@Autowired
private SetmealService setmealService;
@Override
public void remove(Long ids) {
LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
//添加查询条件
dishLambdaQueryWrapper.eq(Dish::getCategoryId,ids);
//sql语句查询有多少条
int count1 = dishService.count(dishLambdaQueryWrapper);//
//1.查询当前分类是否关联了菜品,如果关联了抛出业务异常
if (count1 > 0){
//关联菜品,抛出异常
throw new CustomException("当前分类下关联了菜品,无法删除");
}
//2.查询当前分类是否关联了套餐,如果关联了抛出业务异常
LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
//添加查询条件
setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,ids);
//执行查询
int count2 = setmealService.count(setmealLambdaQueryWrapper);
if (count2 > 0){
//关联套餐,抛出异常
throw new CustomException("当前分类下关联了套餐,无法删除");
}
//3.正常删除
super.removeById(ids);
}
}