需求分析和设计
分类名称必须是唯一的?
分类主要可分为 菜品分类和套餐分类
新添加的分类状态默认为“禁用”
分页查询流程基本与员工分页查询一致
1.Controller接收传来的参数,并调用categoryService方法获取传回的分页信息。
@GetMapping("/admin/category/page") //请求的路径
@ApiOperation("分类分页查询")
public Result<PageResult> pageQuery(CategoryPageQueryDTO categoryPageQueryDTO ){
log.info("分类的分页查询{}",categoryPageQueryDTO );
PageResult pageResult = categoryService.pageQuery(categoryPageQueryDTO ); // 得到分页查询的信息
return Result.success(pageResult ); //将获取的分页查询信息返回给前端以便展示
}
PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO );
实现类
@Override
public PageResult pageQuery1(CategoryPageQueryDTO categoryPageQueryDTO) {
//开始分页查询
PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
Page<Category> page = categoryMapper.pageQuery(categoryPageQueryDTO);
long total = page.getTotal();
List<Category> records = page.getResult();
return new PageResult(total, records);
}
Mapper
Page<Category> pageQuery(CategoryPageQueryDTO categoryPageQueryDTO);
xml
<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参数
对于路径参数的接收需要使用@PathVariable,在Spring Boot或Spring MVC中,@PathVariable注解是必要的,用于将URL路径中的占位符(例如/items/{id}中的{id})绑定到控制器处理方法的参数上。如果你去掉了@PathVariable注解,方法参数将不再与路径变量相关联,而是变成了一个普通的参数,Spring MVC也不会自动地将其与URL路径中的值绑定。
Controller层
@PostMapping("/status/{statuslianxi}")
@ApiOperation("启用禁用自己练习")
public Result<String> startOrStop1(@PathVariable Integer status, Long id) {
log.info("id为{},启用还是禁用:{}", id, status);
categoryService.startOrStop1(status, id);
return Result.success();
}
service层
void startOrStop1(Integer status, Long id);
impl
@Override
public void startOrStop1(Integer status, Long id) {
Category category = new Category();
category.setId(id);
category.setStatus(status);
category.setUpdateTime(LocalDateTime.now());
category.setUpdateUser(BaseContext.getCurrentId());
categoryMapper.update(category);
}
mapper
更新操作操作可重复使用,因此将需要更新的参数封装进category类中,需要更新的进行更新即可
void update(Category category)
xml
<update id="update" parameterType="Category">
update category
<set>
<if test="type != null">
type = #{type},
</if>
<if test="name != null">
name = #{name},
</if>
<if test="sort != null">
sort = #{sort},
</if>
<if test="status != null">
status = #{status},
</if>
<if test="updateTime != null">
update_time = #{updateTime},
</if>
<if test="updateUser != null">
update_user = #{updateUser}
</if>
</set>
where id = #{id}
</update>
新增分类
Controller层
@PostMapping("/lianxi")
@ApiOperation("新增分类自己练习")
public Result<String> addCategor(@RequestBody CategoryDTO categoryDTO) {
log.info("新增分类信息{}", categoryDTO);
categoryService.save1(categoryDTO);
return Result.success();
}
service层
void save1(CategoryDTO categoryDTO);
impl
@Override
public void save1(CategoryDTO categoryDTO) {
Category category = new Category();
BeanUtils.copyProperties(categoryDTO,category);
category.setStatus(StatusConstant.DISABLE);
//设置创建时间、修改时间、创建人、修改人
category.setCreateTime(LocalDateTime.now());
category.setUpdateTime(LocalDateTime.now());
category.setCreateUser(BaseContext.getCurrentId());
categoryMapper.insert(category);
}
mapper
@Insert("insert into category(type, name, sort, status, create_time, update_time, create_user, update_user)" +
" VALUES" +
" (#{type}, #{name}, #{sort}, #{status}, #{createTime}, #{updateTime}, #{createUser}, #{updateUser})")
void insert(Category category);
根据id删除分类
(注意:因为分类表包含了菜品表和套餐表,因此如果删除的分类表中所包含的菜品或者套餐还存在时,就会出现错误。)
controller层
@DeleteMapping("/lianxi")
@ApiOperation("根据id删除分类")
public Result<String> deleteById1(Long id) {
log.info("要删除的分类id为{}", id);
categoryService.deleteById1(id);
return Result.success();
}
Service层
void deleteById1(Long id);
impl层
@Override
public void deleteById1(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);
}
mapper层
@Delete("delete from category where id = #{id}")
void deleteById(Long id);
根据类型查询分类
Controller层
@GetMapping("/listlianxi")
@ApiOperation("根据类型查询分类")
public Result<List> queryByCategory(Integer type) {
log.info("根据类型{}查询分类", type);
List<Category> category = categoryService.list1(type);
return Result.success(category);
}
service层
List<Category> list1(Integer type);
impl层
@Override
public List<Category> list1(Integer type) {
List<Category> list = categoryMapper.list(type);
return list;
}
mapper层
List<Category> list(Integer type);
xml映射
<select id="list" resultType="Category">
select * from category
where status = 1
<if test="type != null">
and type = #{type}
</if>
order by sort asc,create_time desc
</select>