查询与修改属性详情
本章编写查询商品属性详情功能,包括有查询与修改的功能:
查询属性详情
对应的API文档
对应前端的页面:
生产代码返回的/product/attr/info/{attrId}响应的内容,不包括以下内容;
"catelogPath": [2, 34, 225] //分类完整路径
需要在响应中添加以上内容。首先把字段添加进AttrRespVo中:
package com.lastingwar.mall.product.vo;
import lombok.Data;
@Data
public class AttrRespVo extends AttrVo {
/**
* "catelogName": "手机/数码/手机", //所属分类名字
* "groupName": "主体", //所属分组名字
* catelogPath://分类完整路径
*/
private String catelogName;
private String groupName;
private Long[] catelogPath;
}
之后来到controller修改原先的方法:
/**
* 信息
*/
@RequestMapping("/info/{attrId}")
//@RequiresPermissions("product:attr:info")
public R info(@PathVariable("attrId") Long attrId){
//AttrEntity attr = attrService.getById(attrId);
AttrRespVo attrRespVo=attrService.getAttrInfo(attrId);
return R.ok().put("attr", attrRespVo);
}
把原先返回的AttrEntity替换成AttrRespVo,这样就能包含需求的信息,之后创建并实现getInfo方法:
package com.lastingwar.mall.product.service.impl;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.lastingwar.common.utils.R;
import com.lastingwar.mall.product.dao.AttrAttrgroupRelationDao;
import com.lastingwar.mall.product.dao.AttrGroupDao;
import com.lastingwar.mall.product.dao.CategoryDao;
import com.lastingwar.mall.product.entity.AttrAttrgroupRelationEntity;
import com.lastingwar.mall.product.entity.AttrGroupEntity;
import com.lastingwar.mall.product.entity.CategoryEntity;
import com.lastingwar.mall.product.service.AttrAttrgroupRelationService;
import com.lastingwar.mall.product.service.CategoryService;
import com.lastingwar.mall.product.vo.AttrRespVo;
import com.lastingwar.mall.product.vo.AttrVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
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.AttrDao;
import com.lastingwar.mall.product.entity.AttrEntity;
import com.lastingwar.mall.product.service.AttrService;
import org.springframework.transaction.annotation.Transactional;
@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {
@Autowired
AttrAttrgroupRelationDao relationDao;
@Autowired
AttrGroupDao attrGroupDao;
@Autowired
CategoryDao categoryDao;
@Autowired
CategoryService categoryService;
@Override
public PageUtils queryPage(Map<String, Object> params) {
IPage<AttrEntity> page = this.page(
new Query<AttrEntity>().getPage(params),
new QueryWrapper<AttrEntity>()
);
return new PageUtils(page);
}
/**
* 将属性分组新增信息保存进属性的表中,同时保存AttrAttrgroupRelation标准对应的ID
* @param attr 属性分组新增信息
*/
@Transactional
@Override
public void saveAttr(AttrVo attr) {
//拷贝传入的vo数据,保存进实体类
AttrEntity attrEntity = new AttrEntity();
BeanUtils.copyProperties(attr,attrEntity);
//完成保存基础数据的功能
this.save(attrEntity);
//保存关联信息
AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
relationEntity.setAttrGroupId(attr.getAttrGroupId());
//由于ID是自动生成的,这里使用传入的vo是没有值的
relationEntity.setAttrId(attrEntity.getAttrId());
relationDao.insert(relationEntity);
}
/**
* 通过分类属性ID查找分类名称和组名称添加到返回中,
* @param params 基础的返回信息
* @param catelogId 分类属性ID
* @return AttrRespVo
*/
@Override
public PageUtils queryBaseAttrPage(Map<String, Object> params, Long catelogId) {
//重写QueryWrapper,进行不同的检索
QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<>();
//如果请求的有catelogId,搜索catelogId相同的行
if (catelogId!=0){
queryWrapper.eq("catelog_id",catelogId);
}
//如果有关键字的模糊查询,添加进queryWrapper
String key = (String) params.get("key");
if (StringUtils.isNotEmpty(key)){
//attr_id attr_name
queryWrapper.and((wrapper)->{
wrapper.eq("attr_id",key).or().like("attr_name",key);
}
);
}
//基本的分页显示
IPage<AttrEntity> page = this.page(
new Query<AttrEntity>().getPage(params),
//使用条件判断后的queryWrapper
queryWrapper
);
//需要进行内容的添加
PageUtils pageUtils = new PageUtils(page);
//拆开成行信息的形式
List<AttrEntity> records = page.getRecords();
//收集流处理后信息
List<AttrRespVo> respVos = records.stream().map((attrEntity) -> {
//使用流,对每一个对象进行内容的添加,添加成为attrRespVo
AttrRespVo attrRespVo = new AttrRespVo();
//将原来的信息拷贝过来
BeanUtils.copyProperties(attrEntity, attrRespVo);
//查询需要添加的两条信息,并添加进AttrRespVo中
//使用attrId查询关联表获得AttrGroupId,之后根据分组ID获取分组名称
AttrAttrgroupRelationEntity attrId = relationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>()
.eq("attr_id", attrEntity.getAttrId()));
if (attrId != null) {
AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrId.getAttrGroupId());
attrRespVo.setGroupName(attrGroupEntity.getAttrGroupName());
}
CategoryEntity categoryEntity = categoryDao.selectById(attrEntity.getCatelogId());
if (categoryEntity != null) {
attrRespVo.setCatelogName(categoryEntity.getName());
}
return attrRespVo;
}
).collect(Collectors.toList());
//添加处理完的结果集
pageUtils.setList(respVos);
return pageUtils;
}
/**
* 请求属性ID,返回包含有属性基础信息,附加分类,附加分组信息的属性详细信息
* @param attrId 属性ID
* @return AttrRespVo
*/
@Override
public AttrRespVo getAttrInfo(Long attrId) {
AttrRespVo attrRespVo = new AttrRespVo();
//拷贝原有的内容
AttrEntity attrEntity = this.getById(attrId);
BeanUtils.copyProperties(attrEntity,attrRespVo);
//查询到的attrAttrgroupRelation表的全部信息
AttrAttrgroupRelationEntity attrAttrgroupRelation= relationDao.
selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrId));
if (attrAttrgroupRelation!=null){
//保存分组ID信息
attrRespVo.setAttrGroupId(attrAttrgroupRelation.getAttrGroupId());
//根据分组ID查询分组名称
AttrGroupEntity attrGroupEntity = attrGroupDao.
selectById(attrAttrgroupRelation.getAttrGroupId());
if (attrGroupEntity!=null){
//保存分组名称
attrRespVo.setGroupName(attrGroupEntity.getAttrGroupName());
}
}
Long[] catelogPath = categoryService.findCatelogPath(attrEntity.getCatelogId());
//添加分组路径
attrRespVo.setCatelogPath(catelogPath);
CategoryEntity categoryEntity = categoryDao.selectById(attrEntity.getCatelogId());
//添加分类名称
if (categoryEntity!=null){
attrRespVo.setCatelogName(categoryEntity.getName());
}
return attrRespVo;
}
}
至此来到前端可以看到附加的内容已经能够正确回显:
注意此时如果修改附加的属性是没办法保存到数据库中的,因为还未对属性修改进行编写。
修改属性详情
对应API文档:
同样来到controller层:
/**
* 修改
*/
@RequestMapping("/update")
//@RequiresPermissions("product:attr:update")
public R update(@RequestBody AttrVo attrVo){
attrService.updateAttr(attrVo);
return R.ok();
}
之后创建和实现updateAttr方法:
package com.lastingwar.mall.product.service.impl;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.lastingwar.common.utils.R;
import com.lastingwar.mall.product.dao.AttrAttrgroupRelationDao;
import com.lastingwar.mall.product.dao.AttrGroupDao;
import com.lastingwar.mall.product.dao.CategoryDao;
import com.lastingwar.mall.product.entity.AttrAttrgroupRelationEntity;
import com.lastingwar.mall.product.entity.AttrGroupEntity;
import com.lastingwar.mall.product.entity.CategoryEntity;
import com.lastingwar.mall.product.service.AttrAttrgroupRelationService;
import com.lastingwar.mall.product.service.CategoryService;
import com.lastingwar.mall.product.vo.AttrRespVo;
import com.lastingwar.mall.product.vo.AttrVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
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.AttrDao;
import com.lastingwar.mall.product.entity.AttrEntity;
import com.lastingwar.mall.product.service.AttrService;
import org.springframework.transaction.annotation.Transactional;
@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {
@Autowired
AttrAttrgroupRelationDao relationDao;
@Autowired
AttrGroupDao attrGroupDao;
@Autowired
CategoryDao categoryDao;
@Autowired
CategoryService categoryService;
@Override
public PageUtils queryPage(Map<String, Object> params) {
IPage<AttrEntity> page = this.page(
new Query<AttrEntity>().getPage(params),
new QueryWrapper<AttrEntity>()
);
return new PageUtils(page);
}
/**
* 将属性分组新增信息保存进属性的表中,同时保存AttrAttrgroupRelation标准对应的ID
* @param attr 属性分组新增信息
*/
@Transactional
@Override
public void saveAttr(AttrVo attr) {
//拷贝传入的vo数据,保存进实体类
AttrEntity attrEntity = new AttrEntity();
BeanUtils.copyProperties(attr,attrEntity);
//完成保存基础数据的功能
this.save(attrEntity);
//保存关联信息
AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
relationEntity.setAttrGroupId(attr.getAttrGroupId());
//由于ID是自动生成的,这里使用传入的vo是没有值的
relationEntity.setAttrId(attrEntity.getAttrId());
relationDao.insert(relationEntity);
}
/**
* 通过分类属性ID查找分类名称和组名称添加到返回中,
* @param params 基础的返回信息
* @param catelogId 分类属性ID
* @return AttrRespVo
*/
@Override
public PageUtils queryBaseAttrPage(Map<String, Object> params, Long catelogId) {
//重写QueryWrapper,进行不同的检索
QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<>();
//如果请求的有catelogId,搜索catelogId相同的行
if (catelogId!=0){
queryWrapper.eq("catelog_id",catelogId);
}
//如果有关键字的模糊查询,添加进queryWrapper
String key = (String) params.get("key");
if (StringUtils.isNotEmpty(key)){
//attr_id attr_name
queryWrapper.and((wrapper)-> wrapper.eq("attr_id",key).or().like("attr_name",key)
);
}
//基本的分页显示
IPage<AttrEntity> page = this.page(
new Query<AttrEntity>().getPage(params),
//使用条件判断后的queryWrapper
queryWrapper
);
//需要进行内容的添加
PageUtils pageUtils = new PageUtils(page);
//拆开成行信息的形式
List<AttrEntity> records = page.getRecords();
//收集流处理后信息
List<AttrRespVo> respVos = records.stream().map((attrEntity) -> {
//使用流,对每一个对象进行内容的添加,添加成为attrRespVo
AttrRespVo attrRespVo = new AttrRespVo();
//将原来的信息拷贝过来
BeanUtils.copyProperties(attrEntity, attrRespVo);
//查询需要添加的两条信息,并添加进AttrRespVo中
//使用attrId查询关联表获得AttrGroupId,之后根据分组ID获取分组名称
AttrAttrgroupRelationEntity attrId = relationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>()
.eq("attr_id", attrEntity.getAttrId()));
if (attrId != null) {
AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrId.getAttrGroupId());
attrRespVo.setGroupName(attrGroupEntity.getAttrGroupName());
}
CategoryEntity categoryEntity = categoryDao.selectById(attrEntity.getCatelogId());
if (categoryEntity != null) {
attrRespVo.setCatelogName(categoryEntity.getName());
}
return attrRespVo;
}
).collect(Collectors.toList());
//添加处理完的结果集
pageUtils.setList(respVos);
return pageUtils;
}
/**
* 请求属性ID,返回包含有属性基础信息,附加分类,附加分组信息的属性详细信息
* @param attrId 属性ID
* @return AttrRespVo
*/
@Override
public AttrRespVo getAttrInfo(Long attrId) {
AttrRespVo attrRespVo = new AttrRespVo();
//拷贝原有的内容
AttrEntity attrEntity = this.getById(attrId);
BeanUtils.copyProperties(attrEntity,attrRespVo);
//查询到的attrAttrgroupRelation表的全部信息
AttrAttrgroupRelationEntity attrAttrgroupRelation= relationDao.
selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrId));
if (attrAttrgroupRelation!=null){
//保存分组ID信息
attrRespVo.setAttrGroupId(attrAttrgroupRelation.getAttrGroupId());
//根据分组ID查询分组名称
AttrGroupEntity attrGroupEntity = attrGroupDao.
selectById(attrAttrgroupRelation.getAttrGroupId());
if (attrGroupEntity!=null){
//保存分组名称
attrRespVo.setGroupName(attrGroupEntity.getAttrGroupName());
}
}
Long[] catelogPath = categoryService.findCatelogPath(attrEntity.getCatelogId());
//添加分组路径
attrRespVo.setCatelogPath(catelogPath);
CategoryEntity categoryEntity = categoryDao.selectById(attrEntity.getCatelogId());
//添加分类名称
if (categoryEntity!=null){
attrRespVo.setCatelogName(categoryEntity.getName());
}
return attrRespVo;
}
/**
* 根据请求中的属性详细信息,同时修改信息表和关联表的信息
* @param attrVo 属性详细信息
*/
@Transactional
@Override
public void updateAttr(AttrVo attrVo) {
//首先保存基本信息
AttrEntity attrEntity = new AttrEntity();
BeanUtils.copyProperties(attrVo,attrEntity);
this.updateById(attrEntity);
//修改关联信息
AttrAttrgroupRelationEntity attrgroupRelation = new AttrAttrgroupRelationEntity();
attrgroupRelation.setAttrGroupId(attrVo.getAttrGroupId());
attrgroupRelation.setAttrId(attrVo.getAttrId());
//判断数据库时候已经存在当前的值
Integer count = relationDao.selectCount(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrgroupRelation.getAttrId()));
if (count<1){
relationDao.insert(attrgroupRelation);
}
//使用UpdateWrapper,更新行的匹配条件是attr_id相同
else {
relationDao.update(attrgroupRelation,
new UpdateWrapper<AttrAttrgroupRelationEntity>().eq("attr_id",attrgroupRelation.getAttrId()));
}
}
}
重启服务,来到前端页面,可以看到所属分组能够正确保存。