day20200203

《课程笔记》

作业详细:

《大觅网项目-day03》

上课笔记:
1.准备物料(MarkdownPad),POSTMAN,素材(dm-common(install打包操作-保证本地构建),dm-base-provider,dm-eureka-server),dm-item-consumer
导入数据库脚本(创建库,创建表)

2.业务需求:
1.学习如何看接口文档(技术!)
2.查询图片接口(基础微服务)
3.轮播接口(商品微服务)
4.商品详情接口(商品微服务)

3.1 maven的本地构建
素材:common工具工程
步骤1: idea直接导入dm-common
步骤2:Lifecycle install

3.2 创建注册中心eureka
步骤:创建工程dm-eureka-server

3.3 创建基础微服务
步骤:创建工程dm-base-provider

3.4 建库,导入脚本

3.5 创建hosts映射
C:WindowsSystem32driversetchosts
修改:
192.168.115.156 item2.local.com
192.168.115.156 scheduler2.local.com
192.168.115.156 user2.local.com
192.168.115.156 base2.local.com
192.168.115.156 order2.local.com

3.6修改基础微服务中yml文件中的url
url: jdbc:mysql://base2.local.com:3306/xxxxx

4.查询图片接口
步骤:创建基础服务dm-base-provider
===RestDmImageService.java
思路1:
接口路径:/queryDmImageList
==入口
targetId:关联表ID (dm_item中item_id)
type :图片类型(0:无类型 1:轮播图 2:海报图)
category:图片分类(0:用户头像 1:商品图片)
==出口
List<DmImage>集合

思路2:
技术点:组装list(场景:list中泛型中的对象的属性在数据库中缺失数据,需设置默认值)

案例:
@RequestMapping(value = "/queryDmImageList", method = RequestMethod.POST)
public List<DmImage> queryDmImageList(@RequestParam("targetId") Long targetId,
@RequestParam("type") Integer type,
@RequestParam("category") Integer category) throws Exception {
Map<String, Object> imageParam = new HashMap<String, Object>();
imageParam.put("targetId", targetId);
imageParam.put("type", type);
imageParam.put("category", category);
List<DmImage> dmImageList = dmImageMapper.getDmImageListByMap(imageParam);
return setDefaultImageList(dmImageList, category, type);
}

//设置图片为空时默认显示图片
public List<DmImage> setDefaultImageList(List<DmImage> dmImageList, Integer category, Integer type) {
if (EmptyUtils.isEmpty(dmImageList)) {
dmImageList = new ArrayList<DmImage>();
DmImage dmImage = new DmImage();
dmImage.setType(type);
dmImage.setCategory(category);
dmImageList.add(dmImage);
}
for(DmImage dmImage:dmImageList){
if (EmptyUtils.isEmpty(dmImage.getImgUrl())) {
dmImage.setImgUrl(Constants.DEFAULT_CAROUSEL);
}
}
return dmImageList;
}

4.测试查询图片接口的URL:
http://localhost:7002/queryDmImageList
targetId=1
type=1
category=1

5.查询首页轮播图接口
思路1:查询 "首页.md"文档,查询入口和出口
思路2:dm-item-consumer消费者创建接口,实现类
思路3:轮播涉及到的商品表(dm-item中isBanner字段(是否推荐(0:默认 1:热推)))
思路4:同一个消费者调用不同的生产者(通过Feign调用公共服务模块中的不同生产者接口)
思路5:组装VO(重点学习!)

====dm-item-consumer
①service/HomeService
案例:
public interface HomeService {
/**
* 查询首页轮播图
* @return
* @throws Exception
*/
public List<HotItemVo> queryBanner() throws Exception;
}

②Service/impl/HomeServiceImpl
@Component
public class HomeServiceImpl implements HomeService {
@Autowired
private RestDmItemClient restDmItemClient;
@Autowired
private RestDmImageClient restDmImageClient;

@Override
public List<HotItemVo> queryBanner() throws Exception {
//查询轮播图前5个
Map<String, Object> param = new HashMap<String, Object>();
param.put("isBanner", 1);
param.put("beginPos", 0);
param.put("pageSize", 5);
List<DmItem> dmItemList = restDmItemClient.getDmItemListByMap(param);
//组装接口返回数据
List<HotItemVo> hotItemVoList = new ArrayList<HotItemVo>();
if (EmptyUtils.isEmpty(dmItemList)) {
return null;
}
for (DmItem dmItem : dmItemList) {
HotItemVo hotItemVo = new HotItemVo();
BeanUtils.copyProperties(dmItem, hotItemVo);
//查询图片信息
List<DmImage> dmImageList = restDmImageClient.queryDmImageList(dmItem.getId(),
Constants.Image.ImageType.carousel,
Constants.Image.ImageCategory.item);
//组装图片信息
hotItemVo.setImgUrl(EmptyUtils.isEmpty(dmImageList) ? null : dmImageList.get(0).getImgUrl());
hotItemVoList.add(hotItemVo);
}
return hotItemVoList;
}
}

③controller/HomeController
案例:
@RestController
@RequestMapping("api/p/index")
public class HomeController {

@Resource
private HomeService homeService;

@RequestMapping(value = "/queryBanner", method = RequestMethod.POST)
public Dto<HotItemVo> queryBanner() throws Exception {
List<HotItemVo> hotItemVoList = homeService.queryBanner();
return DtoUtil.returnDataSuccess(hotItemVoList);
}
}

④dm-item-provider/application.yml
修改url: jdbc:mysql://item2.local.com:3306/xxxx

⑤启动服务列表:
(1)dm-common本地构建
(2)启动注册中心dm-eureka-server
(3)启动基础微服务dm-base-provider
(4)启动商品生产者dm-item-provider
(5)启动商品消费者dm-item-consumer

⑥测试URL
http://localhost:7201/api/p/index/queryBanner


2.课后作业:
(1)上机-作业本-从零开始独立实现查询图片接口,查询首页轮播图接口,并结合接口文档整理编写接口思路
(2)作业本-描述什么是springboot?你们公司是用的哪个版本?
(3)作业本-什么是redis?常用的命令有哪些?你是怎么理解redis的?
(4)上机-作业本-JDK8中BeanUtils.copyProperties(User,UserVO)代表的含义

原文地址:https://www.cnblogs.com/effortandluck/p/12258956.html