从明天起,做一个幸福的人 喂马、劈柴,周游世界 从明天起,关心粮食和蔬菜 我有一所房子,面朝大海,春暖花开 从明天起,和每一个亲人通信 告诉他们我的幸福 那幸福的闪电告诉我的 我将告诉每一个人 给每一条河每一座山取一个温暖的名字 陌生人,我也为你祝福 愿你有一个灿烂的前程 愿你有情人终成眷属 愿你在尘世获得幸福 我只愿面朝大海,春暖花开
一、基础搭建
1. 界面预览
2. 实例类
新建 src/main/java/com/reggie/entity/Category 类:
package com.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;
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;
}
3. Mapper接口
新建 src/main/java/com/reggie/mapper/CategoryMapper 接口:
package com.reggie.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.reggie.entity.Category;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface CategoryMapper extends BaseMapper<Category> {
}
4. 业务层接口
新建 src/main/java/com/reggie/service/CategoryService 类:
package com.reggie.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.reggie.entity.Category;
public interface CategoryService extends IService<Category> {
}
5. 业务实现类
新建 src/main/java/com/reggie/service/impl/CategoryServiceImpl 类:
package com.reggie.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reggie.entity.Category;
import com.reggie.service.CategoryService;
import org.springframework.stereotype.Service;
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
}
6. 控制层
新建 src/main/java/com/reggie/controller/CategoryController 类:
package com.reggie.controller;
@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
}
二、新增分类
1. 界面样式
2. 编码
编辑 src/main/java/com/reggie/controller/CategoryController 类:
...
@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
@Autowired
private CategoryService categoryService;
@PostMapping
public R<String> save(@RequestBody Category category) {
log.info("新增分类:{}", category);
categoryService.save(category);
return R.success("新增分类成功");
}
}
三、分页查询
1. 界面样式
2. 编码
编辑 src/main/java/com/reggie/controller/CategoryController 类:
...
@GetMapping("/page")
public R<Page> page(int page, int pageSize) {
Page<Category> pageInfo = new Page<>(page, pageSize);
LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.orderByAsc(Category::getSort);
categoryService.page(pageInfo, queryWrapper);
return R.success(pageInfo);
}
四、删除分类
1. 界面样式
2. 编码
编辑 src/main/java/com/reggie/controller/CategoryController 类:
...
@DeleteMapping
public R<String> delete(Long id) {
log.info("删除分类,id 为:{}", id);
categoryService.remove(id);
return R.success("删除分类成功");
}
五、删除分类 2(关联查询)
删除分类,需要检查该分类下是否关联了菜品和套餐
1. 基础搭建
⑴. 实体类
新建 src/main/java/com/reggie/entity/Dish 类:
package com.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 java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
@Data
public class Dish implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String name;
private Long categoryId;
private BigDecimal price;
private String code;
private String image;
private String description;
private Integer status;
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;
}
新建 src/main/java/com/reggie/entity/Setmeal 类:
package com.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 java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
@Data
public class Setmeal implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private Long categoryId;
private String name;
private BigDecimal price;
private Integer status;
private String code;
private String description;
private String image;
@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 接口
新建 src/main/java/com/reggie/mapper/DishMapper 接口:
package com.reggie.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.reggie.entity.Dish;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface DishMapper extends BaseMapper<Dish> {
}
新建 src/main/java/com/reggie/mapper/SetmealMapper 接口:
package com.reggie.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.reggie.entity.Setmeal;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface SetmealMapper extends BaseMapper<Setmeal> {
}
⑶. Service 接口
新建 src/main/java/com/reggie/service/DishService 类:
package com.reggie.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.reggie.entity.Dish;
public interface DishService extends IService<Dish> {
}
新建 src/main/java/com/reggie/service/CategoryService 类:
package com.reggie.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.reggie.entity.Setmeal;
public interface SetmealService extends IService<Setmeal> {
}
⑷. Service 实现类
新建 src/main/java/com/reggie/service/impl/DishServiceImpl 类:
package com.reggie.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reggie.entity.Dish;
import com.reggie.mapper.DishMapper;
import com.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
}
新建 src/main/java/com/reggie/service/impl/SetmealServiceImpl 类:
package com.reggie.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reggie.entity.Setmeal;
import com.reggie.mapper.SetmealMapper;
import com.reggie.service.SetmealService;
import org.springframework.stereotype.Service;
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
}
2. 编码实现
⑴. 自定义业务异常类
新建 src/main/java/com/reggie/common/CustomException 类:
package com.reggie.common;
public class CustomException extends RuntimeException{
public CustomException(String message) {
super(message);
}
}
⑵. 扩展 remove 方法
编辑 src/main/java/com/reggie/service/CategoryService 类:
package com.reggie.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.reggie.entity.Category;
public interface CategoryService extends IService<Category> {
public void remove(Long id);
}
⑶. 实现 remove 方法
编辑 src/main/java/com/reggie/service/impl/CategoryServiceImpl 类:
package com.reggie.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reggie.common.CustomException;
import com.reggie.entity.Category;
import com.reggie.entity.Dish;
import com.reggie.entity.Setmeal;
import com.reggie.mapper.CategoryMapper;
import com.reggie.service.CategoryService;
import com.reggie.service.DishService;
import com.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
@Autowired
private DishService dishService;
@Autowired
private SetmealService setmealService;
@Override
public void remove(Long id) {
LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
dishLambdaQueryWrapper.eq(Dish::getCategoryId, id);
int count1 = dishService.count(dishLambdaQueryWrapper);
if(count1 > 0) {
throw new CustomException("当前分类下关联了菜品,不能删除!");
}
LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, id);
int count2 = setmealService.count(setmealLambdaQueryWrapper);
if(count2 > 0 ) {
throw new CustomException("当前分类下关联了套餐,不能删除!");
}
super.removeById(id);
}
}
⑷. 处理自定义异常
新建 src/main/java/com/reggie/common/GlobalExceptionHandler 类:
...
@ExceptionHandler(CustomException.class)
public R<String> exceptionHandler(CustomException ex) {
log.info(ex.getMessage());
return R.error(ex.getMessage());
}
}
六、修改分类
1. 界面预览
2. 编码
编辑 src/main/java/com/reggie/controller/CategoryController 类:
...
@PutMapping
public R<String> update(@RequestBody Category category) {
log.info("修改分类信息:{}", category);
categoryService.updateById(category);
return R.success("修改分类信息成功 ");
}
}
|