当前位置: 首页 > 工具软件 > mall-learning > 使用案例 >

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

吴山
2023-12-01

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


前言

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

品牌管理。


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

一、实现过程

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什么的可以不用深究,注销或者跟-着原始代码的注解走一遍也可以。

 类似资料: