商城项目实战3.商品模块的编写

商品实体类

/**
 * 商品
 */
@Data
public class Goods {
    @TableId
    private Integer goodsId;
    private String goodsImg;//图片
    private String goodsName;//名称
    private String goodsCaption; // 副标题
    private String goodsPrice;//价格
    private String goodsStock;//库存
    private String goodsSalvesVolume;//销量
    private String goodsType;//商品类型
    private String goodsSpecification;//商品规格
    private String goodsDesc;//简介
    private Boolean goodsMarketable; // 是否上架


}

mapper接口

public interface GoodsMapper extends BaseMapper {
}

服务层

package jkw.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jkw.pojo.Goods;

public interface GoodsService {
    void add(Goods goods);
    void update(Goods goods);
    void delete(Integer id);
    Goods findById(Integer id);
    Page search(String search,int page,int size);
    void updateMarketable(Integer id);
}
package jkw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jkw.mapper.GoodsMapper;
import jkw.pojo.Goods;
import jkw.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public void add(Goods goods) {
        goodsMapper.insert(goods);
    }

    @Override
    public void update(Goods goods) {
        goodsMapper.updateById(goods);
    }

    @Override
    public void delete(Integer id) {
        goodsMapper.deleteById(id);
    }

    @Override
    public Goods findById(Integer id) {
        return goodsMapper.selectById(id);
    }

    @Override
    public Page search(String search, int page, int size) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        if (search != null) {
            //商品类型/商品规格/ 名称
            queryWrapper.like("goodsType", search).or().like("goodsSpecification", search).or().like("goodsName", search);
        }
        return goodsMapper.selectPage(new Page<>(page, size), queryWrapper);
    }

    @Override
    public void updateMarketable(Integer id) {
        Goods goods = goodsMapper.selectById(id);
        goods.setGoodsMarketable(!goods.getGoodsMarketable());
        goodsMapper.updateById(goods);

    }
}

控制器

package jkw.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jkw.pojo.Goods;
import jkw.result.BaseResult;
import jkw.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/goods/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    /**
     * 新增
     *
     * @param goods 对象
     * @return 执行结果
     */
    @PostMapping("/add")
    public BaseResult add(Goods goods) {
        goodsService.add(goods);
        return BaseResult.ok();
    }


    /**
     * 修改
     *
     * @param goods 对象
     * @return 执行结果
     */
    @PostMapping("/update")
    public BaseResult update(Goods goods) {
        goodsService.update(goods);
        return BaseResult.ok();
    }


    /**
     * 删除
     *
     * @param goodsId
     * @return 执行结果
     */
    @DeleteMapping("/delete")
    public BaseResult delete(Integer goodsId) {
        goodsService.delete(goodsId);
        return BaseResult.ok();
    }


    /**
     * 根据id查询
     *
     * @param goodsId
     * @return 查询结果
     */
    @GetMapping("/findById")
    public BaseResult findById(Integer goodsId) {
        Goods goods = goodsService.findById(goodsId);
        return BaseResult.ok(goods);
    }


    /**
     * 分页查询权限
     *
     * @param page 页面
     * @param size 每页条数
     * @return 查询结果
     */
    @PreAuthorize("hasAnyAuthority('/goods/goods')")
    @GetMapping("/search")
    public BaseResult> search(String search, int page, int size) {
        Page goodsPage = goodsService.search(search, page, size);
        return BaseResult.ok(goodsPage);
    }

}

你可能感兴趣的:(商城项目实战,java,开发语言)