目录
首先是控制层代码
@RestController
@RequestMapping("/admin/category")
@Api(tags = "分类相关接口")
@Slf4j
public class CategoryController {
@Autowired
private CategoryService categoryService;
}
新增分类功能
@PostMapping
@ApiOperation("新增分类")
public Result<String> save(@RequestBody CategoryDTO categoryDTO){
log.info("新增分类:{}", categoryDTO);
categoryService.save(categoryDTO);
return Result.success();
}
根据前端提交过来的请求,只需要将部分参数提交给前端,故使用CategoryDTO做参数,dto里的参数与前端页面的参数一一对应,可以拿图来对比一下
public class CategoryDTO implements Serializable {
//主键
private Long id;
//类型 1 菜品分类 2 套餐分类
private Integer type;
//分类名称
private String name;
//排序
private Integer sort;
}
我们设计dto的时候也可以根据前端页面一一对应来设计。
接着再实现业务层方法(因为我们只传入了dto里面的数据,所以其他菜品分类数据我们需要在后端去实现,最后传给数据库)。
代码如下。
public void save(CategoryDTO categoryDTO) {
Category category = new Category();
//属性拷贝
BeanUtils.copyProperties(categoryDTO, category);
//分类状态默认为禁用状态0
category.setStatus(StatusConstant.DISABLE);
//设置创建时间、修改时间、创建人、修改人
category.setCreateTime(LocalDateTime.now());
category.setUpdateTime(LocalDateTime.now());
category.setCreateUser(BaseContext.getCurrentId());
category.setUpdateUser(BaseContext.getCurrentId());
categoryMapper.insert(category);
}
分类分页查询功能
@GetMapping("/page")
@ApiOperation("分类分页查询")
public Result<PageResult> page(CategoryPageQueryDTO categoryPageQueryDTO){
log.info("分页查询:{}", categoryPageQueryDTO);
PageResult pageResult = categoryService.pageQuery(categoryPageQueryDTO);
return Result.success(pageResult);
}
也是由dto来接收,再去实现业务层
public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
//下一条sql进行分页,自动加入limit关键字分页
Page<Category> page = categoryMapper.pageQuery(categoryPageQueryDTO);
return new PageResult(page.getTotal(), page.getResult());
}
其中sql执行方法写在mapper中
public interface CategoryMapper extends BaseMapper<Category> {
/**
* 分页查询
* @param categoryPageQueryDTO
* @return
*/
Page<Category> pageQuery(CategoryPageQueryDTO categoryPageQueryDTO);
}
具体sql语句我们配置在CategoryMapper.xml中(list是后面我们要完成的)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.sky.mapper.CategoryMapper">
<select id="list" resultType="com.sky.entity.Category">
select * from category
where status = 1
<if test="type != null">
and type = #{type}
</if>
order by sort asc,create_time desc
</select>
<select id="pageQuery" resultType="com.sky.entity.Category">
select * from category
<where>
<if test="name != null and name != ''">
and name like concat('%',#{name},'%')
</if>
<if test="type != null">
and type = #{type}
</if>
</where>
order by sort asc , create_time desc
</select>
删除分类
这个操作比较简单,只需要根据前端传过来的id进行delete就行了,但由于分类关联着菜品,所以我们要进行一下判断,如果该分类含有关联的菜品,我们将无法删除。具体代码如下
@DeleteMapping
public Result<String> deleteById(Long id){
log.info("删除分类:{}", id);
categoryService.deleteById(id);
return Result.success();
}
业务层代码如下
public void deleteById(Long id) {
//查询当前分类是否关联了菜品,如果关联了就抛出业务异常
Integer count = dishMapper.countByCategoryId(id);
if(count > 0){
//当前分类下有菜品,不能删除
throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
}
//查询当前分类是否关联了套餐,如果关联了就抛出业务异常
count = setmealMapper.countByCategoryId(id);
if(count > 0){
//当前分类下有菜品,不能删除
throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
}
//删除分类数据
categoryMapper.deleteById(id);
}
查询分类是否关联菜品我们在dishMapper中去实现,实现思路为:查询数据库菜品表中分类id字段与前端传过来的id为相同的所有菜品,如果查询到的菜品数不为0,就无法删除。删除套餐分类也类似。
代码如下
@Mapper
public interface DishMapper extends BaseMapper<Dish> {
/**
* 根据分类id查询菜品数量
* @param categoryId
* @return
*/
@Select("select count(id) from dish where category_id = #{categoryId}")
Integer countByCategoryId(Long categoryId);
}
@Mapper
public interface SetmealMapper extends BaseMapper<Setmeal> {
/**
* 根据分类id查询套餐的数量
* @param id
* @return
*/
@Select("select count(id) from setmeal where category_id = #{categoryId}")
Integer countByCategoryId(Long id);
}
修改分类
控制层代码如下,前端只需要修改dto里面封装的数据,其他数据我们在后端修改,该功能与新增分类类似。
/**
* 修改分类
* @param categoryDTO
* @return
*/
@PutMapping
@ApiOperation("修改分类")
public Result<String> update(@RequestBody CategoryDTO categoryDTO){
categoryService.update(categoryDTO);
return Result.success();
}
业务层代码如下
public void update(CategoryDTO categoryDTO) {
Category category = new Category();
BeanUtils.copyProperties(categoryDTO,category);
//构造查询条件
LambdaQueryWrapper<Category> qw = new LambdaQueryWrapper<>();
qw.eq(Category::getId, categoryDTO.getId());
//设置修改时间、修改人
category.setUpdateTime(LocalDateTime.now());
category.setUpdateUser(BaseContext.getCurrentId());
categoryMapper.update(category,qw);
}
启用、禁用分类
代码如下
@PostMapping("/status/{status}")
@ApiOperation("启用禁用分类")
public Result<String> startOrStop(@PathVariable("status") Integer status, Long id){
categoryService.startOrStop(status,id);
return Result.success();
}
业务层代码
public void startOrStop(Integer status, Long id) {
LambdaQueryWrapper<Category> qw = new LambdaQueryWrapper<>();
qw.eq(Category::getId, id);
Category category = categoryMapper.selectOne(qw);
category.setStatus(status);
category.setUpdateTime(LocalDateTime.now());
category.setUpdateUser(BaseContext.getCurrentId());
category.setId(id);
categoryMapper.update(category, qw);
}
由于该功能比较简单,可以使用Mybatis-Plus来实现,就不需要在xml中配置sql语句了,这样比较便捷。
根据类型查询分类
控制层代码如下
@GetMapping("/list")
@ApiOperation("根据类型查询分类")
public Result<List<Category>> list(Integer type){
List<Category> list = categoryService.list(type);
return Result.success(list);
}
业务层代码如下
public List<Category> list(Integer type) {
return categoryMapper.list(type);
}
其中categoryMapper.list()在前面已经配置过了。
最终效果如下