乐优商城 Day05(非教程)

乐优商城学习Day05:

注意:此次代码都是在第四天的基础上

第四天的链接如下:

https://blog.csdn.net/zcylxzyh/article/details/98972565


此次笔记内容主要为:
1.实现新增功能和修改功能
a.品牌查询
b.根据商品分类(cid)查询规格参数
c.实现SKU
2.导入前台页面

下面开始第五天的学习:


1.实现新增功能和修改功能

a.品牌查询

先做品牌查询,只查询当前分类下的品牌
在这里插入图片描述
在BrandController中新加方法
在这里插入图片描述
BrandController代码如下:

package com.leyou.item.web;

import com.leyou.common.vo.PageResult;
import com.leyou.item.pojo.Brand;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("brand")
public class BrandController {
   
    @Autowired
    private BrandService brandService;

    /*
     * 1.请求方式:Get
     * 2.请求路径:
     * 3.请求参数:5个
     * 4.返回值类型:封装成一个对象。因此我们需要封装一个类PageResult来表示分页结果。
     * */
//分页查询品牌:
    //1,2
    @GetMapping("page")
    //4 ResponseEntity<PageResult<Brand>>
    //rest风格都是ResponseEntity<PageResult//返回值<Brand>//PageResult里面装的>
    public ResponseEntity<PageResult<Brand>> queryBrandByPage(
            //3.参数
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "sortBy", required = false) String sortBy,
            @RequestParam(value = "desc", defaultValue = "false") Boolean desc,
            @RequestParam(value = "key",required = false) String key
    ) {
   
        //编写业务

        return ResponseEntity.ok(brandService.queryBrandByPage(page, rows, sortBy, desc, key));
    }

    //新增品牌
    @PostMapping
    public ResponseEntity<Void> saveBrand(Brand brand , @RequestParam("cids")List<Long> cids){
   
        brandService.saveBrand(brand,cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    //根据cid查询品牌
    @GetMapping("/cid/{cid}")
    public ResponseEntity<List<Brand>> queryBrandByCid(@PathVariable("cid") Long cid){
   
        return ResponseEntity.ok(brandService.queryBrandCid(cid));
    }
}

然后因为要多表查询,所以sql要自己写在BrandMapper里
在这里插入图片描述
BrandMapper代码如下:

package com.leyou.item.mapper;

        import com.leyou.item.pojo.Brand;
        import org.apache.ibatis.annotations.Insert;
        import org.apache.ibatis.annotations.Param;
        import org.apache.ibatis.annotations.Select;
        import tk.mybatis.mapper.common.Mapper;

        import java.util.List;

public interface BrandMapper extends Mapper<Brand> {
   

    @Insert("INSERT INTO tb_category_brand (category_id,brand_id) VALUES (#{cid},#{bid})")
    int insertCategoryBrand(@Param("cid") Long cid ,@Param("bid") Long bid);

    @Select("SELECT b.* FROM tb_category_brand cb INNER JOIN tb_brand b ON b.id = cb.brand_id WHERE cb.category_id = #{cid}")
    List<Brand> queryByCategoryId(@Param("cid") Long cid);

}

然后去BrandService实现方法。
BrandService代码如下:

package com.leyou.item.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.vo.PageResult;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.pojo.Brand;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class BrandService {
   
    @Autowired
    private BrandMapper brandMapper;
//sortBy 根据什么排序,比如id
    public PageResult<Brand> queryBrandByPage(Integer page, Integer rows, String sortBy, Boolean desc, String key) {
   
        //分页,使用分页助手,在查询语句前调用这个方法就能实现分页,这里是page,rows
        PageHelper.startPage(page,rows);//第一个参数:当前页,第二个参数:每页大小

        //Example需要一个实体类(字节码),让他知道去哪个表查
        Example example = new Example(Brand.class);

        //条件过滤,查询用的,这里是key
        if (StringUtils.isNotBlank(key)){
   
            //过滤条件,也就是sql语句
            /*
            * WHERE 'name' LIKE "%X%" OR letter =='X'
            * ORDER BY id DESC
            * */
            //key.toUpperCase()转大写
            example.createCriteria().orLike("name","%"+key+"%")
                    .orEqualTo("letter",key.toUpperCase());
        }
        //排序
        if(StringUtils.isNotBlank(sortBy)){
   
            String orderByClause = sortBy+ (desc ? "DESC":"ASC");
            example.setOrderByClause(orderByClause);
        }
        //查询
        List<Brand> list = brandMapper.selectByExample(example);
        //如果为空,证明没查到
        if(CollectionUtils.isEmpty(list)){
   
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        //解析分页结果
        PageInfo<Brand> info = new PageInfo<>(list);
        //返回总条数和list
        return new PageResult<>(info.getTotal(),list);
    }

    //新增,因为复杂需要加上事务注解
    @Transactional
    public void saveBrand(Brand brand, List<Long> cids) {
   
        //新增品牌
        brand.setId(null);
        int count = brandMapper.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值