实现轮播图接口

1.在开始工作之前请先准备好以下要用到的软件以及素材:

MarkdownPad;

POSTMAN;

dm-common(install打包操作-保证本地构建),

dm-base-provider,

dm-eureka-server,

dm-item-consumer

导入数据库脚本(创建库,创建表)

2.打开 Idea ,导入我们所需要的项目

3.构建本地maven库,将 dm-common 包配置到本地maven库中。

  3.1、在执行请步骤之前,请先在 Idea 设置中修改本机对应的 maven 环境地址(File→Settings→Build, Execution, Deployment→Build Tools→Maven)大觅网03Day-LMLPHP

大觅网03Day-LMLPHP

  3.2、maven配置文件修改本地仓库配置文件:将下列代码粘贴到 配置文件中的mirrors标签下

<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
   <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
<mirror>
<id>CN</id>
<name>OSChina Central</name>
<url>http://maven.oschina.net/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>

  3.3、配置完成后,在 idea 中打开 maven 窗口,依次点击每个模块下的install功能,直到本地maven苦衷多出如图二所示的7个文件夹即可。

大觅网03Day-LMLPHP

4.上一步骤执行结束后,启动注册中心,然后打开 dm-base-provider  模块。

4.1、修改该模块下的yml文件,将数据库链接地址修改成你虚拟机的ip,或者在hosts文件中修改虚拟主机地址。

大觅网03Day-LMLPHP

大觅网03Day-LMLPHP

5.创建查询图片接口。

  5.1、设置接口路径("/queryDmImageList"),通过 targetId :关联表ID (dm_item中item_id),type :图片类型(0:无类型 1:轮播图 2:海报图),category :图片分类(0:用户头像 1:商品图片)三个单数查询图片参数。

  5.2、 RestDmImageService.java 文件中添加一个新接口 queryDmImageList 通过该接口调用 setDefaultImageList  方法查询图片。

@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);
}

  5.3、setDefaultImageList  方法,设置默认图片,如果获取到的热点图片信息是空值,则将图片更改为默认图片

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;
}

  5.4、测试接口连通性:在postman中现将请求模式更改为 Post 测试 http://localhost:7002/queryDmImageList?targetId=1&type=1&category=1 ,当出现图中结果时,则说明接口没有问题。

大觅网03Day-LMLPHP

6.创建查询首页轮播图接口。

  6.1、在 dm-item-consumer 中创建 HomeService.java 、HomeServiceImpl.java  HomeController.java 文件

  HomeService.java 文件

public interface HomeService {
/**
* 查询首页轮播图
* @return
* @throws Exception
*/
public List<HotItemVo> queryBanner() throws Exception;
}

  HomeServiceImpl.java 文件

@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;
}
}

  HomeController.java 文件

@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);
}
}

7.启动服务列表:

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

8.测试连通性

  8.1、在postman中现将请求模式更改为 Post 测试 http://localhost:7201/api/p/index/queryBanner ,当出现图中结果时,则说明接口没有问题。

大觅网03Day-LMLPHP

05-17 04:50