zoukankan      html  css  js  c++  java
  • 商城后台上架商品列表查询的书写全过程

    //1、实体类po/vo-------》2、mapper-------》service--------》controller

    1、实体类po/vo
    package com.leyou.item.pojo
    ;

    import com.fasterxml.jackson.annotation.JsonIgnore;
    import lombok.Data;
    import tk.mybatis.mapper.annotation.KeySql;

    import javax.persistence.Id;
    import javax.persistence.Table;
    import javax.persistence.Transient;
    import java.util.Date;

    /**
    * @author newcityman
    * @date 2019/12/30 - 22:21
    */
    @Data
    @Table(name = "tb_spu")
    public class Spu {
    @Id
    @KeySql(useGeneratedKeys = true)
    private Long id;
    private Long BrandId;
    private Long cid1; //1级类目
    private Long cid2; //2级类目
    private Long cid3; //3级类目
    private String title; //标题
    private String subTitle; //子标题
    private Boolean saleable; //是否上架

    @JsonIgnore
    private Boolean valid; //是否有效,逻辑删除用
    private Date createTime; //创建时间

    @JsonIgnore
    private Date lastUpdateTime; //最后修改时间

    @Transient
    private String bname;
    @Transient
    private String cname;
    }

    package com.leyou.item.pojo;

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

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

    /**
    * @author newcityman
    * @date 2019/12/30 - 22:26
    */
    @Data
    @Table(name = "tb_spu_detail")
    public class SpuDetail {

    @Id
    private Long spuId; //对应的SPU的id
    private String description ; //商品描述
    private String specialSpec; //商品特殊规格的名称及可选值模版
    private String genericSpec; //商品的全局规格属性
    private String packingList; //包装清单
    private String afterService; //售后服务
    }

    //2、mapper
    package com.leyou.item.mapper;

    import com.leyou.item.pojo.Spu;
    import tk.mybatis.mapper.common.Mapper;

    /**
    * @author newcityman
    * @date 2019/12/30 - 22:35
    */
    public interface SpuMapper extends Mapper<Spu> {
    }

    //3、service

    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.SpuDetailMapper;
    import com.leyou.item.mapper.SpuMapper;
    import com.leyou.item.pojo.Category;
    import com.leyou.item.pojo.Spu;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.util.CollectionUtils;
    import tk.mybatis.mapper.entity.Example;

    import java.util.Arrays;
    import java.util.List;
    import java.util.stream.Collectors;

    /**
    * @author newcityman
    * @date 2019/12/30 - 22:37
    */
    @Service
    public class GoodsService {

    @Autowired
    private SpuMapper spuMapper;

    @Autowired
    private SpuDetailMapper spuDetailMapper;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private BrandService brandService;


    public PageResult<Spu> querySpuByPage(Integer page, Integer rows, Boolean saleable, String key) {
    //分页
    PageHelper.startPage(page,rows);
    //过滤
    Example example = new Example(Spu.class);
    Example.Criteria criteria = example.createCriteria();
    //搜索字段过滤
    if(StringUtils.isNotBlank(key)){
    criteria.andLike("title","%"+key +"%");
    }
    if(saleable!=null){
    criteria.andEqualTo("saleable",saleable);
    }

    //上下架过滤
    //默认排序(商品更新时间)
    example.setOrderByClause("last_update_time DESC");

    //查询
    List<Spu> spus = spuMapper.selectByExample(example);

    //判断
    if(CollectionUtils.isEmpty(spus)){
    throw new LyException(ExceptionEnum.GOODS_NOT_FOUND);
    }

    //解析分类和品牌的名称
    loadCategoryAndBrandName(spus);

    PageInfo<Spu> info = new PageInfo<>();
    return new PageResult<>(info.getTotal(),spus);
    }

    //解析分类和品牌名称的方法
    private void loadCategoryAndBrandName(List<Spu> spus) {
    for (Spu spu : spus) {
    //处理分类名称
    List<String> names = categoryService.queryByIds(Arrays.asList(spu.getCid1(), spu.getCid2(), spu.getCid3()))
    .stream().map(Category::getName).collect(Collectors.toList());
    spu.setCname(StringUtils.join(names,"/"));
    //处理品牌名称
    spu.setBname(brandService.queryById(spu.getBrandId()).getName());
    }
    }
    }
     
     

  • 相关阅读:
    ADO.NET操作PostgreSQL:数据库操作类(未封装)
    ADO.NET操作SQL Server:数据库操作类(已封装)
    ADO.NET操作SQL Server:数据库操作类(未封装)
    h5微信支付
    css3的transform:tanslateZ没有效果
    判断页面是否被嵌入iframe里面
    vue项目的环境变量
    iOS下调用元素的focus方法,input元素不聚焦,键盘不弹起的问题
    关于common.js里面的module.exports与es6的export default的思考总结
    Vue2.4.0 新增的inheritAttrs,attrs
  • 原文地址:https://www.cnblogs.com/newcityboy/p/12122431.html
Copyright © 2011-2022 走看看