【增加商品】接口介绍;
该接口是后台系统的;
【增加商品】接口文档;
【增加商品】接口:在界面上的效果;
【增加商品】接口:和【上传片】接口的关系;因为增加商品时候,需要附带上传商品片;就【增加商品】接口来说,其中的image属性,就是我们【把片上传到服务器后的,片的地址】;而,这个地址,是只要把片上传到服务器后,才能得到的;我们在开发【增加商品】接口时,也要开发【上传片】接口;【上传片】接口会把片上传到服务器,并返回片在服务器上的地址;然后我们在把这个地址拿过来,作为【增加商品】接口的image参数;
正式开发;
创建ProductAdminController类;创建增加商品的方法:addProduct()方法;ProductAdminController类:packagecoimoomalcontroller;importcoimoomalcommoApiRestResponse;importcoimoomalmoderequest.AddProductReq;importcoimoomalservicProductService;importio.swagger.annotations.ApiOperation;importorspringframeworbeans.factory.annotatioAutowired;importorspringframeworstereotypController;importorspringframeworwebinannotatioPostMapping;importorspringframeworwebinannotatioRequestBody;importorspringframeworwebinannotatioResponseBody;importjavax.validatioValid;/***描述:【商品模块】后台的Controller*/@ControllerpublicclassProductAdminController{@AutowiredProductServiceproductService;@ApiOperation('新增商品')@PostMapping('/admin/product/add')@ResponseBodypublicApiRestResponseaddProduct(@Valid@RequestBodyAddProductReqaddProductReq){productServicadd(addProductReq);returnApiRestResponssuccess();}}说明:【url、请求方式:要符合接口规范】,【Swagger文档,添加接口说明】,【返回值要序列化为JSON】;……………………………………………………因为接口参数很多,所以我们额外创建【AddProductRequest】这个bean去接收参数;因为这儿需要参数校验,所以我们使用了Validation参数校验;AddProductReq类:packagecoimoomalmoderequest;importjavax.validatioconstraints.Max;importjavax.validatioconstraints.Min;importjavax.validatioconstraints.NotNull;importjavutiDate;publicclassAddProductReq{@NotNull(message='商品名称不能为null')privateStringname;@NotNull(message='商品片不能为null')privateStringimage;privateStringdetail;@NotNull(message='商品分类不能为null')privateIntegercategoryId;@NotNull(message='商品价格不能为null')@Min(value=1,message='价格不能小于1')privateIntegerprice;@NotNull(message='商品库存不能为null')@Max(value=10000,message='库存不能大于10000')privateIntegerstock;privateIntegerstatus;publicStringgetName(){returnname;}publicvoidsetName(Stringnam{this.name=name;}publicStringgetImage(){returnimage;}publicvoidsetImage(Stringimag{this.image=image;}publicStringgetDetail(){returndetail;}publicvoidsetDetail(Stringdetai{this.detail=detail;}publicIntegergetCategoryId(){returncategoryId;}publicvoidsetCategoryId(IntegercategoryI{this.categoryId=categoryId;}publicIntegergetPrice(){returnprice;}publicvoidsetPrice(Integerpric{this.price=price;}publicIntegergetStock(){returnstock;}publicvoidsetStock(Integerstoc{this.stock=stock;}publicIntegergetStatus(){returnstatus;}publicvoidsetStatus(Integerstatus){this.status=status;}}这个类就是用来接收前端传过来的参数的;这个类要比照接口的参数来写;然后,这儿校验规则:我们即参考了实际的业务需求:比如image;虽然在数据库中这个对应的字段,我们要求不能为null,但是我们是设有默认值的;纯技术来说,这个接口的image参数可以不传;;;从业务的角度来说,我们又要求用户传image;;;;;我们的image也要求不为空了;又比如status,虽然数据库要求是非空,但其设置了默认值;;;;综合业务角度来说,可以不传status,如果不传,我们就使用默认值1就行了;又比如,我们要求price不能低于1分,stock库存不能超过10000;否则就给出对应的提示信息;……………………………………………………因为要用到Validation参数校验,需要使用@Valid注解;;因为这儿是POST请求,而且参数是放在Body中的,所以需要使用@RequestBody注解;……………………………………………………有关Service层的listCategoryForCustomer()方法,在下一部分介绍;……………………………………………………【新增商品】接口,其实还没开发完;完整的增加商品,需要上传片,而这就涉及到了后面要介绍的【上传片】接口;
创建ProductService接口和ProductServiceImpl实现类;在ProductServiceImpl实现类中,编写新增商品的方法add();然后在ProductService接口中,反向生成方法的声明;在ProductServiceImpl实现类中,编写新增商品的方法add();packagecoimoomalservicimpl;importcoimoomalexceptioImoocMallException;importcoimoomalexceptioImoocMallExceptionEnum;importcoimoomalmodedao.ProductMapper;importcoimoomalmodepojo.Product;importcoimoomalmoderequest.AddProductReq;importcoimoomalservicProductService;importorspringframeworbeans.BeanUtils;importorspringframeworbeans.factory.annotatioAutowired;importorspringframeworstereotypService;/***描述:商品Service实现类;*/@ServicepublicclassProductServiceImplimplementsProductService{@AutowiredProductMapperproductMapper;/***新增商品*@paramaddProductReq*/@Overridepublicvoidadd(AddProductReqaddProductReq){//因为,我们是通过Product这个类,和数据库打交道的;//我们把addProductReq的内容,复制成一个product;Productproduct=newProduct();BeanUtils.copyProperties(addProductReq,product);//我们要看下,商品时候重名ProductproductOld=productMapper.selectByName(product.getName());//如果查出来了,表示数据库中已经有叫这个名字的商品了;那么我们就抛出一个名字已存在的异常if(productOld!=nul{thrownewImoocMallException(ImoocMallExceptionEnuNAME_EXISTE;}//如果上面没问题,就去调用方法,向数据库中插入数据;intcount=productMapper.insertSelective(product);//如果count=0,表示插入失败了;我们就抛出一个新增失败的异常;if(count==0){thrownewImoocMallException(ImoocMallExceptionEnuCREATE_FAILE;}}}说明:看注释;其中,在看商品是否重名的时候,我们在Mapper中创建了一个根据name查商品的方法;有关这个方法在下一部分介绍;其中用到的insertSelective()方法,是我们前面利用【mybatis-generator】插件生成的;……………………………………………………然后在ProductService接口中,反向生成方法的声明;
在ProductMapper接口中,定义【根据】name查询商品】的方法selectByName();然后在ProductMapper.xml中编写方法的SQL;在ProductMapper接口中,定义【根据】name查询商品】的方法selectByName();……………………………………………………然后在ProductMapper.xml中编写方法的SQL;
启动项目,测试【增加商品】接口;
文章为作者独立观点,不代表观点