0
点赞
收藏
分享

微信扫一扫

亿级高并发电商项目-- 实战篇 --万达商城项目 七(品牌模块、商品类型模块等开发)


 

亿级高并发电商项目-- 实战篇 --万达商城项目 七(品牌模块、商品类型模块等开发)_mybatis

亿级高并发电商项目-- 实战篇 --万达商城项目 七(品牌模块、商品类型模块等开发)_mybatis_02

  

  

专栏:高并发---分布式 

在管理商品时,除了商品名、价格、商品介绍等基本参数外。还需 要给商品添加品牌、商品类型、商品规格等参数。比如Iphone13的 品牌是苹果。商品类型属于手机通讯>手机>手机。规格有机身颜色: 星光色、版本:128G。品牌、商品类型、商品规格都需要我们在后 台进行管理。

亿级高并发电商项目-- 实战篇 --万达商城项目 七(品牌模块、商品类型模块等开发)_mybatis_03

编写品牌服务接口 

接下来我们编写品牌相关的CRUD方法,首先在通用模块编写品牌服务接口:

// 品牌服务
public interface BrandService {
    // 根据id查询品牌
    Brand findById(Long id);
    // 查询所有品牌
    List<Brand> findAll();
    // 新增品牌
    void add(Brand brand);
    // 修改品牌
    void update(Brand brand);
    // 删除品牌
    void delete(Long id);
    // 分页查询品牌
    Page<Brand> search(Brand brand, int page, int size);
}

 编写品牌服务实现类

商品服务模块编写品牌服务实现类

@DubboService
public class BrandServiceImpl implements
BrandService {
    @Autowired
    private BrandMapper brandMapper;
    @Override
    public Brand findById(Long id){
        if (id == 0){
            int i = 1/0; // 模拟系统异常
       }else if (id == -1){
            throw new BusException(CodeEnum.PARAMETER_ERROR); //模拟业务异常
       }
        return brandMapper.selectById(id);
   }
    @Override
    public List<Brand> findAll() {
        return brandMapper.selectList(null);
     }
    @Override
    public void add(Brand brand) {
        brandMapper.insert(brand);
   }
    @Override
    public void update(Brand brand) {
        brandMapper.updateById(brand);
   }
    @Override
    public void delete(Long id) {
        brandMapper.deleteById(id);
   }
    @Override
    public Page<Brand> search(Brand brand, int page, int size) {
        QueryWrapper<Brand> queryWrapper = new QueryWrapper();
        // 判断品牌名不为空
        if (brand != null && StringUtils.hasText(brand.getName())){
           queryWrapper.like("name",brand.getName());
       }
        Page<Brand> page1 = brandMapper.selectPage(new Page(page, size),queryWrapper);
        return page1;
    }
}

编写品牌控制器

在后台管理API模块编写品牌控制器:

/**
* 品牌
*/
@RestController
@RequestMapping("/brand")
public class BrandController {
    // 远程注入
    @DubboReference
    private BrandService brandService;
    /**
     * 根据id查询品牌
     *
     * @param id 品牌id
     * @return 查询结果
     */
    @GetMapping("/findById")
    public BaseResult<Brand> findById(Long id) {
        Brand brand = brandService.findById(id);
        return BaseResult.ok(brand);
   }
    /**
     * 查询所有品牌
     *
     * @return 所有品牌
     */
    @GetMapping("/all")
    public BaseResult<List<Brand>> findAll()
      {
        List<Brand> brands = brandService.findAll();
        return BaseResult.ok(brands);
   }
    /**
     * 新增品牌
     *
     * @param brand 品牌对象
     * @return 执行结果
     */
    @PostMapping("/add")
    public BaseResult add(@RequestBody Brand brand) {
        brandService.add(brand);
        return BaseResult.ok();
      }
    /**
     * 修改品牌
     *
     * @param brand 品牌对象
     * @return 执行结果
     */
      @PutMapping("/update")
    public BaseResult update(@RequestBody
Brand brand) {
        brandService.update(brand);
        return BaseResult.ok();
   }
    /**
     * 删除品牌
     *
     * @param id 品牌id
     * @return 执行结果
     */
    @DeleteMapping("/delete")
    public BaseResult delete(Long id) {
        brandService.delete(id);
        return BaseResult.ok();
   }
    /**
     * 分页查询品牌
     *
     * @param brand 查询条件对象
     * @param page 页码
     * @param size 每页条数
     * @return 查询结果
     */
    @GetMapping("/search")
    public BaseResult<Page<Brand>> search(Brand brand, int page, int size) {
        Page<Brand> page1 = brandService.search(brand, page, size);
        return BaseResult.ok(page1);
   }
}

启动服务,测试品牌控制器方法

编写商品类型服务接口

接下来我们编写商品类型相关的CRUD方法,首先在通用模块编写商品类型服务接口:

// 商品类型
public interface ProductTypeService {
    // 新增商品类型
    void add(ProductType productType);
    // 修改商品类型
    void update(ProductType productType);
    // 根据id查询商品类型
    ProductType findById(Long id);
    // 删除商品类型
    void delete(Long id);
    // 分页查询
    Page<ProductType> search(ProductType productType,int page, int size);
    // 根据条件查询商品类型
    List<ProductType> findProductType(ProductType productType);
}

 编写商品类型服务实现类

1、在商品服务模块编写商品类型Mapper

public interface ProductTypeMapper extends BaseMapper<ProductType> {
}

2、在商品服务模块编写商品类型服务实现类

@DubboService
public class ProductTypeServiceImpl
implements ProductTypeService {
    @Autowired
    private ProductTypeMapper productTypeMapper;
    @Override
    public void add(ProductType productType) {
        // 查询父类型
        ProductType productTypeParent = productTypeMapper.selectById(productType.getParentId());
        if (productTypeParent == null){ //如果没有父类型,则为1级类型
            productType.setLevel(1);
       }else if(productTypeParent.getLevel() < 3){ //如果父类型级别<3,则级别为父级别+1
           productType.setLevel(productTypeParent.getLevel()+1);
       }else if(productTypeParent.getLevel() >= 3){ // 如果父类型级别>=3,则不能添加子类型
            throw new BusException(CodeEnum.INSERT_PRODUCT_TYPE_ERROR);
       }
      productTypeMapper.insert(productType);
   }
    @Override
    public void update(ProductType productType) {
        // 查询父类型
        ProductType productTypeParent = productTypeMapper.selectById(productType.getParentId());
        if (productTypeParent == null){ //如果没有父类型,则为1级类型
            productType.setLevel(1);
       }else if(productTypeParent.getLevel() < 3){ //如果父类型级别<3,则级别为父级别+1
           productType.setLevel(productTypeParent.getLevel()+1);
       }else if(productTypeParent.getLevel() >= 3){ //如果父类型级别>=3,则不能添加子类型
  throw new
BusException(CodeEnum.INSERT_PRODUCT_TYPE_ERROR);
       }
      productTypeMapper.updateById(productType);
   }
    @Override
    public void delete(Long id) {
        // 查询该类型的子类型
        QueryWrapper<ProductType> queryWrapper = new QueryWrapper();
        queryWrapper.eq("parentId",id);
        List<ProductType> productTypes = productTypeMapper.selectList(queryWrapper);
        // 如果该类型有子类型,删除失败
        if (productTypes != null && productTypes.size() > 0){
            throw new BusException(CodeEnum.DELETE_PRODUCT_TYPE_ERROR);
       }
        productTypeMapper.deleteById(id);
   }
    @Override
    public ProductType findById(Long id) {
 return productTypeMapper.selectById(id);
   }
    @Override
    public Page<ProductType> search(ProductType productType,int page,int size) {
        QueryWrapper<ProductType> queryWrapper = new QueryWrapper();
        if (productType != null){
            // 类型名不为空时
            if (StringUtils.hasText(productType.getName())){
              queryWrapper.like("name",productType.getName());
           }
            // 上级类型id不为空
            if (productType.getParentId()!= null){
              queryWrapper.eq("parentId",productType.getParentId());
           }
       }
        return productTypeMapper.selectPage(new Page(page,size),queryWrapper);
   }
 @Override
    public List<ProductType> findProductType(ProductType productType) {
        QueryWrapper<ProductType> queryWrapper = new QueryWrapper();
        if (productType != null){
            // 类型名不为空时
            if (StringUtils.hasText(productType.getName())){
              queryWrapper.like("name",productType.getName());
           }
            // 上级类型id不为空
            if (productType.getParentId()!= null){
              queryWrapper.eq("parentId",productType.getParentId());
           }
       }
        List<ProductType> productTypes = productTypeMapper.selectList(queryWrapper);
        return productTypes;
   }
}

编写商品类型控制器

后台管理API模块编写商品类型控制器:

/**
* 商品类型
*/
@RestController
@RequestMapping("/productType")
public class ProductTypeController {
    @DubboReference
    private ProductTypeService productTypeService;
    /**
     * 新增商品类型
     * @param productType 商品类型
     * @return 执行结果
     */
    @PostMapping("/add")
    public BaseResult add(@RequestBody ProductType productType){
        productTypeService.add(productType);
        return BaseResult.ok();
   }
    /**
     * 修改商品类型
     * @param productType 商品类型
     * @return 执行结果
     */
    @PutMapping("/update")
    public BaseResult update(@RequestBody ProductType productType){
      productTypeService.update(productType);
      return BaseResult.ok();
   }
    /**
     * 删除商品类型
     * @param id 商品类型id
     * @return 执行结果
     */
    @DeleteMapping("/delete")
    public BaseResult delete(Long id){
        productTypeService.delete(id);
        return BaseResult.ok();
   }
    /**
     * 根据id查询商品类型
     * @param id 商品类型id
     * @return 查询结果
     */
    @GetMapping("/findById")
    public BaseResult<ProductType> findById(Long id){
        ProductType productType = productTypeService.findById(id);
        return BaseResult.ok(productType);
   }
    /**
     * 分页查询商品类型
     * @param productType 查询条件对象
     * @param page 页码
     * @param size 每页条数
     * @return 查询结果
     */
    @GetMapping("/search")
    public BaseResult<Page<ProductType>> search(ProductType productType,int page,int size){
        Page<ProductType> page1 = productTypeService.search(productType, page,size);
        return BaseResult.ok(page1);
   }
    /**
     * 查询商品类型列表
     * @param productType 查询条件对象
     * @return 查询结果
     */
    @GetMapping("/findProductType")
    public BaseResult<List<ProductType>> findProductType(ProductType productType){
        List<ProductType> productType1 = productTypeService.findProductType(productType);
        return BaseResult.ok(productType1);
   }
    /**
     * 根据父类型id查询商品类型列表
     * @param parentId 父类型id
     * @return 查询结果
     */
     @GetMapping("/findByParentId")
    public BaseResult<List<ProductType>> findByParentId(Long parentId){
        ProductType productType = new ProductType();
        productType.setParentId(parentId);
        List<ProductType> productType1 = productTypeService.findProductType(productType);
        return BaseResult.ok(productType1);
   }
}

启动服务,测试商品类型控制器方法

编写商品规格服务接口

接下来我们编写商品规格相关的CRUD方法,首先在通用模块编写商品规格服务接口:

// 商品规格服务
public interface SpecificationService {
    // 新增商品规格
    void add(Specification specification);
    // 修改商品规格
    void update(Specification specification);
    // 删除商品规格
    void delete(Long[] ids);
    // 根据id查询商品规格
    Specification findById(Long id);
    // 分页查询商品规格
     Page<Specification> search(int page, int size);
    // 查询某种商品类型下的所有规格
    List<Specification> findByProductTypeId(Long id);
    // 新增商品规格项
    void addOption(SpecificationOption specificationOption);
    // 删除商品规格项
    void deleteOption(Long[] ids);
}

 编写商品规格Mapper

1、在商品服务模块编写商品规格和商品规格项Mapper

public interface SpecificationMapper
extends BaseMapper<Specification> {
    Specification findById(Long id);
    // 根据商品类型查询商品规格
    List<Specification> findByProductTypeId(Long productTypeId); }
    public interface SpecificationOptionMapper extends BaseMapper<SpecificationOption> {
}

2、在 resources 中创建 SpecificationMapper 的同级包,编写映射文件 SpecificationMapper.xml

<?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="com.itbaizhan.shopping_goods_service.mapper.SpecificationMapper">
    <resultMap id="specificationMapper" type="com.itbaizhan.shopping_common.pojo.Specification">
        <id property="id" column="bid"></id>
        <result property="specName" column="specName"></result>
        <result property="productTypeId" column="productTypeId"></result>
        <collection property="specificationOptions" column="specId" ofType="com.itbaizhan.shopping_common.pojo.SpecificationOption">
            <id property="id" column="oid"></id>
            <result property="optionName" column="optionName"></result>
            <result property="specId" column="specId"></result>
        </collection>
    </resultMap>
<select id="findById" parameterType="long" resultMap="specificationMapper">
       SELECT
           bz_specification.id AS bid,
           bz_specification.specName,
           bz_specification.productTypeId,
           bz_specification_option.id AS oid,
           bz_specification_option.optionName,
           bz_specification_option.specId
       FROM bz_specification
                 LEFT JOIN bz_specification_option
                           on  bz_specification.id = bz_specification_option.specId
       where bz_specification.id = #{id}
    </select>
    <select id="findByProductTypeId" parameterType="long" resultMap="specificationMapper">
       SELECT
           bz_specification.id AS bid,
           bz_specification.specName,
           bz_specification.productTypeId,
           bz_specification_option.id AS oid,bz_specification_option.optionName,
           bz_specification_option.specId
       FROM bz_specification
                 LEFT JOIN bz_specification_option
                           on bz_specification.id = bz_specification_option.specId
       where bz_specification.productTypeId = # {productTypeId}
    </select>
</mapper>

编写商品规格服务实现类

在商品服务模块编写商品规格服务实现类

@DubboService
public class SpecificationServiceImpl
implements SpecificationService {
    @Autowired
    private SpecificationMapper specificationMapper;
    @Autowired
    private SpecificationOptionMapper specificationOptionMapper;
    @Override
    public void add(Specification specification) {
         specificationMapper.insert(specification);
   }
    @Override
    public void update(Specification specification) {
        specificationMapper.updateById(specification);
   }
    @Override
    public void delete(Long[] ids) {
        for (Long id : ids) {
            // 删除商品规格项
           QueryWrapper<SpecificationOption> queryWrapper = new QueryWrapper();
            queryWrapper.eq("specId",id);
            specificationOptionMapper.delete(queryWrapper);
            // 删除商品规格
            specificationMapper.deleteById(id);
       }
   }
    @Override
    public Specification findById(Long id) {
         return  specificationMapper.findById(id);
   }
    @Override
    public Page<Specification> search(int page, int size) {
        return specificationMapper.selectPage(new Page(page,size),null);
   }
    @Override
    public List<Specification> findByProductTypeId(Long id) {
        return specificationMapper.findByProductTypeId(id);
   }
    @Override
    public void addOption(SpecificationOptions specificationOptions) {
        String[] optionNames = specificationOptions.getOptionName();
        Long specId = specificationOptions.getSpecId();
        for (String optionName : optionNames) {
            SpecificationOption specificationOption = new SpecificationOption();
            specificationOption.setSpecId(specId);
            specificationOption.setOptionName(optionName);
            specificationOptionMapper.insert(specificationOption);
       }
   }
    @Override
    public void deleteOption(Long[] ids) {
      specificationOptionMapper.deleteBatchIds(Arrays.asList(ids));
   }
}

编写商品规格控制器

后台管理API模块编写商品规格控制器:

/**
* 商品规格
*/
@RestController
@RequestMapping("/specification")
public class SpecificationController {
    @DubboReference
    private SpecificationService specificationService;
    /**
     * 新增商品规格
     * @param specification 商品规格
     * @return 执行结果
     */
    @PostMapping("/add")
    public BaseResult add(@RequestBody Specification specification){
        specificationService.add(specification);
        return BaseResult.ok();
   }
    /**
     * 修改商品规格
     * @param specification 商品规格
     * @return 执行结果
     */
    @PutMapping("/update")
    public BaseResult update(@RequestBody Specification specification){
        specificationService.update(specification);
        return BaseResult.ok();
   }
    /**
     * 删除商品规格
     * @param ids 商品规格id集合
     * @return 执行结果
     */
    @DeleteMapping("/delete")
    public BaseResult delete(Long[] ids){
        specificationService.delete(ids);
        return BaseResult.ok();
   }
    /**
     * 根据id查询商品规格
     * @param id 商品规格id
     * @return 查询结果
     */
    @GetMapping("/findById")
    public BaseResult findById(Long id){
        Specification specification = specificationService.findById(id);
        return BaseResult.ok(specification);
   }
    /**
     * 分页查询商品规格
     * @param page 页码
     * @param size 每页条数
     * @return 查询结果
     */
    @GetMapping("/search")
    public BaseResult<Page<Specification>> search(int page,int size){
        Page<Specification> page1 = specificationService.search(page, size);
        return BaseResult.ok(page1);
 }
    /**
     * 查询某种商品类型下的所有规格
     * @param id 商品类型id
     * @return 查询结果
     */
    @GetMapping("/findByProductTypeId")
    public BaseResult<List<Specification>> findByProductTypeId(Long id){
        List<Specification> specifications = specificationService.findByProductTypeId(id);
        return BaseResult.ok(specifications);
   }
    /**
     * 新增商品规格项
     * @param specificationOptions 商品规格项集合
     * @return 执行结果
     */
    @PostMapping("/addOption")
    public BaseResult addOption(@RequestBody SpecificationOptions specificationOptions){
        specificationService.addOption(specificationOptions);
        return BaseResult.ok();
   }
    /**
     * 删除商品规格项
     * @param ids 商品规格项id集合
     * @return 执行结果
     */
    @DeleteMapping("/deleteOption")
    public BaseResult deleteOption(Long[] ids){
        specificationService.deleteOption(ids);
        return BaseResult.ok();
   }
}

启动服务,测试商品类型控制器方法

亿级高并发电商项目-- 实战篇 --万达商城项目 七(品牌模块、商品类型模块等开发)_maven_04

  

举报

相关推荐

0 条评论