电商项目mall-learning学习笔记(2)——品牌管理

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

提示:这里可以添加本文要记录的大概内容:

品牌管理。


提示:以下是本篇文章正文内容,下面案例可供参考

一、实现过程

controller


@Tag(name = "PmsBrandController品牌管理")
@Slf4j
@Controller
@RequestMapping(value = "/brand")
public class PmsBrandController {

    @Autowired
    private PmsBrandService pmsBrandService;

    private static final Logger LOGGE = LoggerFactory.getLogger(PmsBrandController.class);

    @Operation(summary="获取所有品牌列表")
    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<PmsBrand>> getBrandList(){
        log.info("success gain listbrand");
        log.debug("success gain listbrand");
        return CommonResult.success(pmsBrandService.listAllBrand());
    }

    @Operation(summary="创建一个品牌")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult createRrand(@RequestBody PmsBrand pmsBrand){
        /**
         * 1 添加信息(后台),2 刷新
         */
        CommonResult commonResult;
        int count = pmsBrandService.createBrand(pmsBrand);
        if (count == 1) {
            commonResult = CommonResult.success(pmsBrand);
            LOGGE.debug("create success:{}", pmsBrand);
        } else {
            commonResult = CommonResult.failed("操作失败");
            LOGGE.debug("create failed:{}", pmsBrand);
        }
        return commonResult;
    }

    @Operation(summary="根据指定id更新信息")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateBrand(@PathVariable("id") Long id, @RequestBody PmsBrand pmsBrandDto, BindingResult result) {

        CommonResult commonResult;
        int count = pmsBrandService.updateBrand(id, pmsBrandDto);
        if (count == 1) {
            commonResult = CommonResult.success(pmsBrandDto);
            LOGGE.debug("update success:{}", pmsBrandDto);
        } else {
            commonResult = CommonResult.failed("更新失败");
            LOGGE.debug("udpate failed:{}", pmsBrandDto);
        }
        return commonResult;
    }

    @Operation(summary="删除指定的id品牌")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult deleteBrand(@PathVariable("id") Long id) {
        int count = pmsBrandService.deleteBrand(id);
        if (count == 1) {
            LOGGE.debug("deleteBrand success :id={}", id);
            return CommonResult.success(null);
        } else {
            LOGGE.debug("deleteBrand failed :id={}", id);
            return CommonResult.failed("操作失败");
        }
    }

    @Operation(summary="分页查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<PmsBrand>> listBrand(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                        @RequestParam(value = "pageSize", defaultValue = "3") Integer pageSize) {

        List<PmsBrand> pmsBrands = pmsBrandService.listBrand(pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(pmsBrands));
    }

//    @ApiImplicitParam(name = "id", paramType = "path", dataType = "Long")
    @Operation(summary="获取指定id的品牌")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PmsBrand> brand(@PathVariable("id") Long id) {
        return CommonResult.success(pmsBrandService.getBrand(id));
    }
}

Service


@Service
public class PmsBrandServiceImpl implements PmsBrandService {

    @Autowired
    PmsBrandMapper pmsBrandMapper;

    @Override
    public List<PmsBrand> listAllBrand() {
        return pmsBrandMapper.selectByExample(new PmsBrandExample());
    }

    @Override
    public int createBrand(PmsBrand pmsBrand) {
        return pmsBrandMapper.insertSelective(pmsBrand);
    }

    @Override
    public int updateBrand(Long id, PmsBrand pmsBrandDto) {
        pmsBrandDto.setId(id);
        return pmsBrandMapper.updateByPrimaryKeySelective(pmsBrandDto);
    }

    @Override
    public int deleteBrand(Long id) {
        return pmsBrandMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<PmsBrand> listBrand(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return pmsBrandMapper.selectByExample(new PmsBrandExample());
    }

    @Override
    public PmsBrand getBrand(Long id) {
        return pmsBrandMapper.selectByPrimaryKey(id);
    }
}

mapper

由于数据库是借鉴别人家的,所以mapper大家直接借鉴原作者的mapper文件吧,有点长就不放了

二、实现检验

可以在浏览器或者postman输入:localhost:8080/brand/iistAll等命令查看信息

总结

  • 本文基本是代码,其实就是想要通过对brand这个对象的基本操作,让学习者熟悉以下业务流程,controller service mapper pojo。
    假设没有security,也不去深究拦截器。请求发过来,会先到controller,controller就是接受请求并将请求传递给service,service真正开始执行相关的业务操作,但是想要获取数据库里面的数据就必须和数据库打交道,所以service把和数据库打交道的操作变成mapper或者说dao,service调用mapper或dao获取信息,再通过一些处理把这个信息返回给controller,controller收到信息,包装成统一的自定义的信息载体,发送给客户端。

  • 至于代码中的注解关于swagger、Operation什么的可以不用深究,注销或者跟-着原始代码的注解走一遍也可以。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值