一、菜品分类管理实现
1.1 创建菜品分类DTO
名称:DishesTypeDTO
/** * @Package: cn.liuliang.quickdinesysstore.entity.dto * @Author: liuliang * @CreateTime: 2020/10/28 - 16:54 * @Description: */ @Data @ApiModel(value="菜品种类dto", description="") public class DishesTypeDTO implements Serializable { private static final long serialVersionUID=1L; @ApiModelProperty(value = "ID") private Long id; @ApiModelProperty(value = "分类名称:唯一") private String typeName; }
1.2 创建菜品分类VO
名称:DishesTypeVO
/** * @Package: cn.liuliang.quickdinesysstore.entity.vo * @Author: liuliang * @CreateTime: 2020/10/28 - 17:33 * @Description: */ @Data @ApiModel(value="菜品分类vo", description="") public class DishesTypeVO { @ApiModelProperty(value = "ID") private Long id; @ApiModelProperty(value = "菜名:唯一") private String dishesName; }
1.3 效果
1.4 编写controller
/** * <p> * 前端控制器 * </p> * * @author j3_liuliang * @since 2020-10-24 */ @Api(value = "菜品分类管理", tags = "菜品分类管理") @CrossOrigin @RestController @RequestMapping("/dishes-type") public class DishesTypeController { @Autowired private DishesTypeService dishesTypeService; @ApiOperation(value = "条件分页查看所有菜品分类信息", notes = "条件分页查看所有菜品分类信息") @ApiResponses({@ApiResponse(code = 200, message = "请求成功!", response = DishesTypeDTO.class)}) @GetMapping("/select-all") public ResultDTO selectAll( @ApiParam("分类名称查询值") @RequestParam(value = "selectValue", required = false, defaultValue = "") String selectValue, @ApiParam("当前页码") @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum, @ApiParam("每页显示条数") @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) { return dishesTypeService.selectPage(selectValue, pageNum, pageSize); } @ApiOperation(value = "添加或修改菜品分类", notes = "添加或修改菜品分类") @PostMapping("/add-or-update") public ResultDTO addOrUpdateDishesType(@RequestBody DishesTypeVO dishesTypeVO) { return dishesTypeService.addOrUpdateDishesType(dishesTypeVO); } @ApiOperation(value = "删除菜品分类", notes = "删除菜品分类") @GetMapping("/delete") public ResultDTO delete(@ApiParam("分类id") @RequestParam(value = "分类id", required = true) Long id) { return dishesTypeService.delete(id); } }
1.5 编写service
/** * <p> * 服务类 * </p> * * @author j3_liuliang * @since 2020-10-24 */ public interface DishesTypeService extends IService<DishesType> { /** * 分页条件查询菜品种类列表 * @param selectValue 查询条件 * @param pageNum 当前页 * @param pageSize 每页数量 * @return */ ResultDTO selectPage(String selectValue, Integer pageNum, Integer pageSize); /** * 添加或修改菜品分类 * @param dishesTypeVO 分类名称相关信息 * @return */ ResultDTO addOrUpdateDishesType(DishesTypeVO dishesTypeVO); /** * 删除菜品分类 * @param id 菜品分类id * @return */ ResultDTO delete(Long id); }
/** * <p> * 服务实现类 * </p> * * @author j3_liuliang * @since 2020-10-24 */ @Service public class DishesTypeServiceImpl extends ServiceImpl<DishesTypeMapper, DishesType> implements DishesTypeService { @Autowired private DishesTypeMapper dishesTypeMapper; @Override public ResultDTO selectPage(String selectValue, Integer pageNum, Integer pageSize) { // 设置分页参数 Page<DishesType> dishesTypePage = new Page<>(pageNum, pageSize); // 定义创建条件对象 QueryWrapper<DishesType> queryWrapper = null; //判断是否有查询值 if (!StringUtils.isEmpty(selectValue)) { // 创建查询对象 queryWrapper = new QueryWrapper<>(); // 定义查询条件 like全查 queryWrapper.like("type_name", selectValue); } // 执行查询 IPage<DishesType> dishesTypePageModel = dishesTypeMapper.selectPage(dishesTypePage, queryWrapper); // 获取总条数 long total = dishesTypePageModel.getTotal(); // 数据转换及获取数据 List<DishesTypeDTO> dishesTypeDTOList = new ArrayList<>(); List<DishesType> records = dishesTypePageModel.getRecords(); records.forEach(DishesType -> { DishesTypeDTO dishesTypeDTO = new DishesTypeDTO(); BeanUtils.copyProperties(DishesType, dishesTypeDTO); dishesTypeDTOList.add(dishesTypeDTO); }); // 返回数据 return ResultDTO.success().data("total", total).data("rows", dishesTypeDTOList); } @Override public ResultDTO addOrUpdateDishesType(DishesTypeVO dishesTypeVO) { if (null == dishesTypeVO.getId()) { // 插入 // 构造插入对象 DishesType dishesType = new DishesType(); // 设置数据 dishesType.setTypeName(dishesTypeVO.getDishesName()); // 执行插入 dishesTypeMapper.insert(dishesType); } else { // 修改 // 先根据id查询数据 DishesType dishesType = dishesTypeMapper.selectById(dishesTypeVO.getId()); // 修改数据 dishesType.setTypeName(dishesTypeVO.getDishesName()); // 执行修改 dishesTypeMapper.updateById(dishesType); } return ResultDTO.success(); } @Override public ResultDTO delete(Long id) { dishesTypeMapper.deleteById(id); return ResultDTO.success(); } }
1.6 编写mapper
mapper类中用了MyBatisPlus封装好的方法,所以没有编写代码
1.7 启动访问swagger
http://localhost:9110/swagger-ui.html#/
二、菜品管理实现
2.1 创建菜品DTO
/** * @Package: cn.liuliang.quickdinesysstore.entity.vo * @Author: liuliang * @CreateTime: 2020/10/24 - 15:02 * @Description: */ @Data @EqualsAndHashCode() @ApiModel(value="菜品信息DTO", description="") public class DishesDTO implements Serializable { private static final long serialVersionUID = 1L; @ApiModelProperty(value = "id") private Long id; @ApiModelProperty(value = "图片:存储图片的url访问地址") private String imageUrl; @ApiModelProperty(value = "菜名:唯一") private String dishesName; @ApiModelProperty(value = "价格") private BigDecimal price; @ApiModelProperty(value = "所属类别id") private Long dishesTypeId; @ApiModelProperty(value = "所属类别名称") private String dishesTypeName; @ApiModelProperty(value = "简介:菜品的描述") private String briefIntroduction; @ApiModelProperty(value = "创建时间") private Date createTime; }
2.2 创建菜品VO
/** * @Package: cn.liuliang.quickdinesysstore.entity.vo * @Author: liuliang * @CreateTime: 2020/10/29 - 11:47 * @Description: */ @Data @ApiModel(value = "菜品信息vo", description = "") public class DishesVO { @ApiModelProperty(value = "ID") private Long id; @ApiModelProperty(value = "菜名:唯一") private String dishesName; @ApiModelProperty(value = "所属类别:什么类型的菜,关联下表(菜品分类表)id") private Long dishesTypeId; @ApiModelProperty(value = "简介:菜品的描述") private String briefIntroduction; @ApiModelProperty(value = "图片:存储图片的url访问地址") private String imageUrl; @ApiModelProperty(value = "价格") private BigDecimal price; }
2.3 创建菜品查询条件VO
/** * @Package: cn.liuliang.quickdinesysstore.entity.vo * @Author: liuliang * @CreateTime: 2020/10/28 - 19:30 * @Description: */ @Data @ApiModel(value="菜品查询条件vo", description="") public class DishesQueryConditionVO { @ApiModelProperty(value = "菜名:唯一") private String dishesName; @ApiModelProperty(value = "菜品类别") private String dishesTypeName; @ApiModelProperty(value = "价格") private BigDecimal price; @ApiModelProperty(value = "创建时间:格式(2020-10-10)") private String createTime; }
1.4 效果
1.5 编写controller
/** * <p> * 前端控制器 * </p> * * @author j3_liuliang * @since 2020-10-24 */ @Api(value = "菜品管理", tags = "菜品管理") @CrossOrigin @RestController @RequestMapping("/dishes") public class DishesController { @Autowired private DishesService dishesService; @ApiOperation(value = "根据条件分页获取所有菜品信息", notes = "根据条件分页获取所有菜品信息") @GetMapping("/select-all") public ResultDTO selectAll( DishesQueryConditionVO dishesQueryConditionVO, @ApiParam("当前页码") @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum, @ApiParam("每页显示条数") @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) { return dishesService.selectAll(dishesQueryConditionVO, pageNum, pageSize); } @ApiOperation(value = "添加或修改菜品", notes = "添加或修改菜品") @PostMapping("/add-or-update") public ResultDTO addOrUpdate(@RequestBody DishesVO dishesVO) { return dishesService.addOrUpdate(dishesVO); } @ApiOperation(value = "根据id获取菜品信息", notes = "根据id获取菜品信息") @ApiResponses({@ApiResponse(code = 200, message = "请求成功!", response = DishesDTO.class)}) @GetMapping("/select-one") public ResultDTO selectOne(@ApiParam("菜品id") @RequestParam(value = "id", required = true) Long id) { return dishesService.selectOne(id); } @ApiOperation(value = "根据id删除菜品", notes = "根据id删除菜品") @GetMapping("/delete") public ResultDTO delete(@ApiParam("菜品id") @RequestParam(value = "id", required = true) Long id) { dishesService.removeById(id); return ResultDTO.success(); } }
1.6 编写service
/** * <p> * 服务类 * </p> * * @author j3_liuliang * @since 2020-10-24 */ public interface DishesService extends IService<Dishes> { /** * 添加菜品或修改菜品 * * @param dishesVO 菜品信息 * @return */ ResultDTO addOrUpdate(DishesVO dishesVO); /** * 根据条件分页获取所有菜品信息 * * @param dishesQueryConditionVO 查询条件对象 * @param pageNum 当前页 * @param pageSize 每页数量 * @return */ ResultDTO selectAll(DishesQueryConditionVO dishesQueryConditionVO, Integer pageNum, Integer pageSize); /** * 根据id获取菜品 * @param id 菜品id * @return */ ResultDTO selectOne(Long id); }
/** * <p> * 服务实现类 * </p> * * @author j3_liuliang * @since 2020-10-24 */ @Service public class DishesServiceImpl extends ServiceImpl<DishesMapper, Dishes> implements DishesService { @Autowired private DishesMapper dishesMapper; @Autowired private DishesTypeMapper dishesTypeMapper; @Override public ResultDTO addOrUpdate(DishesVO dishesVO) { // 判断条件是否为空 if (null == dishesVO.getDishesName() || "".equals(dishesVO.getDishesName()) || null == dishesVO.getDishesTypeId() || "".equals(dishesVO.getDishesTypeId().toString()) || null == dishesVO.getImageUrl() || "".equals(dishesVO.getImageUrl()) || null == dishesVO.getPrice() || "".equals(dishesVO.getPrice().toString())) { // 为空,抛出自定义异常 throw new QuickException(ResultCodeEnum.PAEAMETER_IS_EMPTY); } // 判断是插入还是修改 if (null == dishesVO.getId()) { // 插入 // 构造插入对象,并赋值 Dishes dishes = new Dishes(); BeanUtils.copyProperties(dishesVO, dishes); dishesMapper.insert(dishes); } else { // 修改 // 根据id查找数据 QueryWrapper<Dishes> dishesQueryWrapper = new QueryWrapper<>(); dishesQueryWrapper .select("dishes_name", "dishes_type_id", "brief_introduction", "image_url", "price") .eq("id", dishesVO.getId()); Dishes dishes = dishesMapper.selectOne(dishesQueryWrapper); BeanUtils.copyProperties(dishesVO, dishes); dishesMapper.updateById(dishes); } return ResultDTO.success(); } @Override public ResultDTO selectAll(DishesQueryConditionVO dishesQueryConditionVO, Integer pageNum, Integer pageSize) { // 构造分页 Page<Dishes> dishesPage = new Page<>(pageNum, pageSize); // 执行条件查询 List<Dishes> dishesList = dishesMapper.selectAll(dishesPage, dishesQueryConditionVO); // 获取相关数据 long total = dishesPage.getTotal(); List<DishesDTO> dishesDTOList = selectDishesTypeName(dishesList); // 返回结果 return ResultDTO.success().data("total", total).data("rows", dishesDTOList); } @Override public ResultDTO selectOne(Long id) { Dishes dishes = dishesMapper.selectById(id); // 构造数据 DishesDTO dishesDTO = new DishesDTO(); BeanUtils.copyProperties(dishes, dishesDTO); // 定义查询条件 QueryWrapper<DishesType> dishesTypeQueryWrapper = new QueryWrapper<>(); // 查询分类名称并赋值 dishesTypeQueryWrapper.select("type_name").eq("id", dishesDTO.getDishesTypeId()); dishesDTO.setDishesTypeName(dishesTypeMapper.selectOne(dishesTypeQueryWrapper).getTypeName()); return ResultDTO.success().data("data", dishesDTO); } /** * 设置传入的菜品列表数据的菜品分类名称 * * @param dishesList 菜品列表,没有菜品分类名称 * @return */ private List<DishesDTO> selectDishesTypeName(List<Dishes> dishesList) { // 定义返回结果列表 List<DishesDTO> dishesDTOList = new ArrayList<>(dishesList.size()); // 遍历 dishesList.forEach(dishes -> { // 填充数据 DishesDTO dishesDTO = new DishesDTO(); BeanUtils.copyProperties(dishes, dishesDTO); // 定义查询条件 QueryWrapper<DishesType> dishesTypeQueryWrapper = new QueryWrapper<>(); // 查询分类名称并赋值 dishesTypeQueryWrapper.select("type_name").eq("id", dishesDTO.getDishesTypeId()); dishesDTO.setDishesTypeName(dishesTypeMapper.selectOne(dishesTypeQueryWrapper).getTypeName()); // 将结果放入返回列表中 dishesDTOList.add(dishesDTO); }); // 返回数据列表 return dishesDTOList; } }
1.7 编写mapper
/** * <p> * Mapper 接口 * </p> * * @author j3_liuliang * @since 2020-10-24 */ public interface DishesMapper extends BaseMapper<Dishes> { /** * 根据条件分页查询菜品列表 * * @param dishesPage 分页对象 * @param dishesQueryConditionVO 条件对象 * @return */ List<Dishes> selectAll(@Param("dishesPage") Page<Dishes> dishesPage, @Param("dishesQueryConditionVO") DishesQueryConditionVO dishesQueryConditionVO); }
<?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="cn.liuliang.quickdinesysstore.mapper.DishesMapper"> <resultMap id="dishes" type="cn.liuliang.quickdinesysstore.entity.Dishes"> <result property="id" javaType="long" column="dishes_id"/> <result property="imageUrl" javaType="string" column="image_url"/> <result property="dishesName" javaType="string" column="dishes_name"/> <result property="price" javaType="java.math.BigDecimal" column="price"/> <result property="dishesTypeId" javaType="long" column="dishes_type_id"/> <result property="createTime" javaType="date" column="create_time"/> </resultMap> <select id="selectAll" resultMap="dishes"> SELECT t_dishes.`id` AS dishes_id, t_dishes.`image_url`, t_dishes.`dishes_name`, t_dishes.`price`, t_dishes.`dishes_type_id`, t_dishes.`brief_introduction`, t_dishes.`create_time` FROM t_dishes LEFT JOIN t_dishes_type ON t_dishes_type.`id` = t_dishes.`dishes_type_id` <where> <if test="dishesQueryConditionVO.dishesName != null and dishesQueryConditionVO.dishesName != ''"> AND t_dishes.`dishes_name` LIKE CONCAT('%', #{dishesQueryConditionVO.dishesName}, '%') </if> <if test="dishesQueryConditionVO.dishesTypeName !=null and dishesQueryConditionVO.dishesTypeName !=''"> AND t_dishes_type.`type_name` = #{dishesQueryConditionVO.dishesTypeName} </if> <if test="dishesQueryConditionVO.price != null"> AND t_dishes.`price` = #{dishesQueryConditionVO.price} </if> <if test="dishesQueryConditionVO.createTime != null and dishesQueryConditionVO.createTime != ''"> AND DATE_FORMAT(t_dishes.`create_time`, '%Y-%m-%d') = DATE_FORMAT(#{dishesQueryConditionVO.createTime}, '%Y-%m-%d') </if> </where> </select> </mapper>
1.8 启动访问swagger
http://localhost:9110/swagger-ui.html#/
结束语
- 由于博主才疏学浅,难免会有纰漏,假如你发现了错误或偏见的地方,还望留言给我指出来,我会对其加以修正。
- 如果你觉得文章还不错,你的转发、分享、点赞、留言就是对我最大的鼓励。
- 感谢您的阅读,十分欢迎并感谢您的关注。