分布式电商项目三十一:商品服务-获取分类属性分组

商品服务-获取分类属性分组

本章编写根据三级分类之后的分组属性,API文档如下:

03、获取分类属性分组
GET
/product/attrgroup/list/{catelogId}
请求参数
{
   page: 1,//当前页码
   limit: 10,//每页记录数
   sidx: 'id',//排序字段
   order: 'asc/desc',//排序方式
   key: '华为'//检索关键字
}
分页数据

响应数据
{
	"msg": "success",
	"code": 0,
	"page": {
		"totalCount": 0,
		"pageSize": 10,
		"totalPage": 0,
		"currPage": 1,
		"list": [{
			"attrGroupId": 0, //分组id
			"attrGroupName": "string", //分组名
			"catelogId": 0, //所属分类
			"descript": "string", //描述
			"icon": "string", //图标
			"sort": 0 //排序
			"catelogPath": [2,45,225] //分类完整路径
		}]
	}
}

编写响应

来到对应的AttrGroupController,这里面现在使用的是公共模型,而这个请求多了一个路径变量{catelogId},添加进公共模型的分页函数中:

package com.lastingwar.mall.product.controller;

import java.util.Arrays;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lastingwar.mall.product.entity.AttrGroupEntity;
import com.lastingwar.mall.product.service.AttrGroupService;
import com.lastingwar.common.utils.PageUtils;
import com.lastingwar.common.utils.R;



/**
 * 属性分组
 *
 * @author yhm
 * @email 403627000@qq.com
 * @date 2020-05-29 16:37:08
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") long catelogId){
        //PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params, catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}

之后ALT+ENTL重载queryPage方法:

package com.lastingwar.mall.product.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.lastingwar.common.utils.PageUtils;
import com.lastingwar.mall.product.entity.AttrGroupEntity;

import java.util.Map;

/**
 * 属性分组
 *
 * @author yhm
 * @email 403627000@qq.com
 * @date 2020-05-29 16:37:08
 */
public interface AttrGroupService extends IService<AttrGroupEntity> {

    PageUtils queryPage(Map<String, Object> params);

    PageUtils queryPage(Map<String, Object> params, long catelogId);
}

之后ALT+ENTL添加方法的实现:
方法实现分为两步,如果catelogId=0,默认使用之前的方法,返回全部的信息,如果不为1 ,返回对应分组的信息。

package com.lastingwar.mall.product.service.impl;

import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lastingwar.common.utils.PageUtils;
import com.lastingwar.common.utils.Query;

import com.lastingwar.mall.product.dao.AttrGroupDao;
import com.lastingwar.mall.product.entity.AttrGroupEntity;
import com.lastingwar.mall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, long catelogId) {
    //catelogId == 0 返回所有的信息,使用renren-fast给的分页封装方法,底层使用mybatis-plus类的方法。
        if (catelogId == 0){
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    new QueryWrapper<AttrGroupEntity>());
            return new PageUtils(page);
        }else {
		//待编写
        }
        return null;
    }

}

针对前端页面的搜索功能,提供后台支持;
在这里插入图片描述
代码为:

package com.lastingwar.mall.product.service.impl;

import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lastingwar.common.utils.PageUtils;
import com.lastingwar.common.utils.Query;

import com.lastingwar.mall.product.dao.AttrGroupDao;
import com.lastingwar.mall.product.entity.AttrGroupEntity;
import com.lastingwar.mall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 找到catelogId的完整路径
     * @param params
     * @param catelogId
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params, long catelogId) {
        String key = (String) params.get("key");
        //select * from pms_attr_group where catelog_id=? and (attr_group_id=key or attr_group_name like %key%)
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        //如果key的值不为空
        if(!StringUtils.isEmpty(key)){
            //函数式编程,与attr_group_id相等,或者attr_group_name包含key
            //like指双百分号,单百分号对应的有likeLeft和likeRight
            wrapper.and((obj)->{
                obj.like("attr_group_name",key);
            });
        }
        //catelogId == 0 返回所有的信息,使用renren-fast给的分页封装方法,底层使用mybatis-plus类的方法。
        if (catelogId == 0){
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    wrapper);
            return new PageUtils(page);
        }else {
            //针对前段页面的搜索框:关键字在params的key键中,
            wrapper.eq("catelog_id",catelogId);
            //调用相同的封装方法,添加上自己写的wrapper
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    wrapper);
            return new PageUtils(page);
        }

    }

}

这里针对全都搜索的查询,也先提供了模糊查询的name字段。
之后可以使用postman进行测试:

在这里插入图片描述
能够正常返回,查询中没有数据,这里可以修改数据层的日志等级,看一下搜索使用的sql语句。
在product模块的yml文件中添加:

logging:
  level:
    com.lastingwar.mall: debug

之后再使用postman请求,能在控制台看到对应的sql语句:

2020-06-05 10:23:25.148 DEBUG 15968 --- [io-10001-exec-1] c.l.m.p.dao.AttrGroupDao.selectPage      : ==>  Preparing: SELECT attr_group_id,attr_group_name,sort,descript,icon,catelog_id FROM pms_attr_group WHERE ((attr_group_id = ? OR attr_group_name LIKE ?)) 
2020-06-05 10:23:25.171 DEBUG 15968 --- [io-10001-exec-1] c.l.m.p.dao.AttrGroupDao.selectPage      : ==> Parameters: a(String), %a%(String)

在这里插入图片描述
检查完sql语句说明没有问题。

前端联调
可以在数据库写入两条测试数据进行前后端的联调:
在这里插入图片描述
之后来到前端页面,点击手机(catelogId=225)
在这里插入图片描述
同时还可以测试一下查找功能:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值