乐优商城个人笔记Day03 (包括了FirstDFS的安装和配置)

乐优商城学习Day03:

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

第二天的链接如下:

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


此次笔记内容主要为:
1.后台实现新增
2.绕过ZUUL缓存
3.FastDFS安装
4.FastDFS的Nginx模块
5.配置nginx开机启动
6.实现图片上传

下面开始第三天的学习:

1.后台实现新增

首先在ly-item-interface中创建名为Brand的实体类,具体位置如下:
在这里插入图片描述

Brand代码如下:

package com.leyou.item.pojo;

import lombok.Data;
import tk.mybatis.mapper.annotation.KeySql;

import javax.persistence.Id;
import javax.persistence.Table;

@Data
@Table(name = "tb_brand")
public class Brand {
   
    @Id
    @KeySql(useGeneratedKeys = true)
    private Long id;
    private String name;// 品牌名称
    private String image;// 品牌图片
    private Character letter;
    // getter setter 略
}

然后在ly-item-service中创建
1.BrandMapper 接口
2.BrandService 类
3.BrandController 类
具体位置如下:
在这里插入图片描述

BrandMapper
分析:通用Mapper只能处理单表,也就是Brand的数据,因此我们手动编写一个方法及sql,实现中间表的新增:

全部代码如下

package com.leyou.item.mapper;

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

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);
}

BrandService
分析:这里要注意,我们不仅要新增品牌,还要维护品牌和商品分类的中间表。这里调用了brandMapper中的一个自定义方法,来实现中间表的数据新增。
在这里插入图片描述
这里要再去枚举里添加一个新的量

全部 代码如下

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.insert(brand);
        if(count!=1){
   
            throw new LyException(ExceptionEnum.BRAND_SAVE_ERROR);
        }
        //新增中间表
        for (Long cid : cids) {
   
            count=brandMapper.insertCategoryBrand(cid,brand.getId());
            if(count != 1){
   
                throw new LyException(ExceptionEnum.BRAND_SAVE_ERROR);
            }
        }

    }
}


BrandController
分析:还是一样,先分析四个内容:

  • 请求方式:刚才看到了是POST
  • 请求路径:/brand
  • 请求参数:brand对象,外加商品分类的id数组cids
  • 返回值:无

代码如下

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
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值