Java网络商城项目 SpringBoot+SpringCloud+Vue 网络商城-程序员宅基地

技术标签: spring boot  java  spring cloud  2024年程序员  

public interface GoodsRepository extends ElasticsearchRepository<Goods,Long> {

}

(1)创建GoodsRepository对应的测试类

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

package com.leyou.search.repostory;

import com.leyou.search.pojo.Goods;

import com.leyou.search.repository.GoodsRepository;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;

import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)

@SpringBootTest

public class GoodsRepositoryTest {

@Autowired

private GoodsRepository goodsRepository;

@Autowired

private ElasticsearchTemplate template;

@Test

public void testCreateIndex(){

template.createIndex(Goods.class);

template.putMapping(Goods.class);

}

}

运行测试

在这里插入图片描述

二、导入数据


1、创建SearchService,构建Goods对象

将数据库当中的SPU和SKU的信息封装为Goods对象,并导入Elasticsearch

在这里插入图片描述

在这里插入图片描述

package com.leyou.search.service;

import com.fasterxml.jackson.core.type.TypeReference;

import com.leyou.common.enums.ExceptionEnum;

import com.leyou.common.exception.LyException;

import com.leyou.common.utils.JsonUtils;

import com.leyou.item.pojo.*;

import com.leyou.search.client.BrandClient;

import com.leyou.search.client.CategoryClient;

import com.leyou.search.client.GoodsClient;

import com.leyou.search.client.SpecificationClient;

import com.leyou.search.pojo.Goods;

import org.apache.commons.lang.StringUtils;

import org.apache.commons.lang.math.NumberUtils;

import org.apache.lucene.util.CollectionUtil;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.util.CollectionUtils;

import java.util.*;

import java.util.stream.Collectors;

@Service

public class SearchService {

@Autowired

private CategoryClient categoryClient;

@Autowired

private BrandClient brandClient;

@Autowired

private GoodsClient goodsClient;

@Autowired

private SpecificationClient specClient;

public Goods buildGoods(Spu spu){

//查询分类

List categories = categoryClient.queryCategoryByIds(

Arrays.asList(spu.getCid1(), spu.getCid2(), spu.getCid3()));

if(CollectionUtils.isEmpty(categories)){

throw new LyException(ExceptionEnum.CATEGORY_NOT_FOND);

}

//将categories集合当中所有的name取出来封装为一个字符串集合

List names = categories.stream().map(Category::getName).collect(Collectors.toList());

//查询品牌

Brand brand = brandClient.queryBrandById(spu.getBrandId());

if(brand == null){

throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);

}

//搜索字段 将字符串集合变成一个字符串以空格为分隔拼接到后面

String all = spu.getTitle() + StringUtils.join(names," ") + brand.getName();

//查询sku

List skuList = goodsClient.querySkuBySpuId(spu.getId());

if(CollectionUtils.isEmpty(skuList)){

throw new LyException(ExceptionEnum.GOODS_SKU_NOT_FOND);

}

//对Sku进行处理

List<Map<String,Object>> skus = new ArrayList<>();

//价格集合

ArrayList priceList = new ArrayList();

for (Sku sku : skuList) {

Map<String,Object> map = new HashMap<>();

map.put(“id”,sku.getId());

map.put(“title”,sku.getTitle());

map.put(“price”,sku.getPrice());

//截取sku当中图片逗号之前的第一个

map.put(“images”,StringUtils.substringBefore(sku.getImages(),“,”));

skus.add(map);

//处理价格

priceList.add(sku.getPrice());

}

//查询规格参数

List params = specClient.queryParamList(null, spu.getCid3(), true);

if(CollectionUtils.isEmpty(params)){

throw new LyException(ExceptionEnum.SPEC_GROUP_NOT_FOND);

}

//查询商品详情

SpuDetail spuDetail = goodsClient.queryDetailById(spu.getId());

//获取通用规格参数,获取到通用规格参数的JSON字符串,将其转换为Map集合

Map<Long, String> genericSpec = JsonUtils.toMap(spuDetail.getGenericSpec(), Long.class, String.class);

//获取特有规格参数,获取到特有规格参数的JSON字符串,将其转换为Map集合,而Map集合当中的值是String,键为List集合

Map<Long, List> specailSpec =

JsonUtils.nativeRead(spuDetail.getSpecialSpec(),

new TypeReference<Map<Long, List>>(){});

//处理规格参数,key是规格参数的名称,值是规格参数的值

Map<String,Object> specs = new HashMap<>();

for (SpecParam param : params) {

//规格名称

String key = param.getName();

Object value = “”;

//判断是否是通过规格参数

if(param.getGeneric()){

value = genericSpec.get(param.getId());

//判断是否是数值类型

if(param.getNumeric()){

//处理成段

value = chooseSegment(value.toString(),param);

}

}else {

value = specailSpec.get(param.getId());

}

//存入map

specs.put(key,value);

}

//构建good对象

Goods goods = new Goods();

goods.setBrandId(spu.getBrandId());

goods.setCid1(spu.getCid1());

goods.setCid2(spu.getCid2());

goods.setCid3(spu.getCid3());

goods.setCreateTime(spu.getCreateTime());

goods.setId(spu.getId());

goods.setAll(all);//搜索字段,包含标题,分类,品牌,规格等信息

goods.setPrice(priceList);// 所有sku价格的集合

goods.setSkus(JsonUtils.toString(skus));// 所有sku的集合的JSON格式

goods.setSpecs(specs);// 所有可以搜索的规格参数

goods.setSubTitle(spu.getSubTitle());

return goods;

}

private String chooseSegment(String value, SpecParam p) {

double val = NumberUtils.toDouble(value);

String result = “其它”;

// 保存数值段

for (String segment : p.getSegments().split(“,”)) {

String[] segs = segment.split(“-”);

// 获取数值范围

double begin = NumberUtils.toDouble(segs[0]);

double end = Double.MAX_VALUE;

if(segs.length == 2){

end = NumberUtils.toDouble(segs[1]);

}

// 判断是否在范围内

if(val >= begin && val < end){

if(segs.length == 1){

result = segs[0] + p.getUnit() + “以上”;

}else if(begin == 0){

result = segs[1] + p.getUnit() + “以下”;

}else{

result = segment + p.getUnit();

}

break;

}

}

return result;

}

}

2、然后编写一个测试类,循环查询Spu,然后调用IndexService中的方法,把SPU变为Goods,然后写入索引库:

在这里插入图片描述

package com.leyou.search.repostory;

import com.leyou.common.vo.PageResult;

import com.leyou.item.pojo.Spu;

import com.leyou.search.client.GoodsClient;

import com.leyou.search.pojo.Goods;

import com.leyou.search.repository.GoodsRepository;

import com.leyou.search.service.SearchService;

import org.apache.lucene.util.CollectionUtil;

import org.aspectj.weaver.ast.Var;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;

import org.springframework.test.context.junit4.SpringRunner;

import org.springframework.util.CollectionUtils;

import java.util.List;

import java.util.stream.Collectors;

@RunWith(SpringRunner.class)

@SpringBootTest

public class GoodsRepositoryTest {

@Autowired

private GoodsRepository goodsRepository;

@Autowired

private ElasticsearchTemplate template;

@Autowired

private GoodsClient goodsClient;

@Autowired

private SearchService searchService;

@Test

public void testCreateIndex(){

template.createIndex(Goods.class);

template.putMapping(Goods.class);

}

@Test

public void loadData(){

int page = 1;

int rows = 100;

int size = 0;

do {

//查询spu的信息

PageResult result = goodsClient.querySpuByPage(page, rows, true, null);

List spuList = result.getItems();//得到当前页

if(CollectionUtils.isEmpty(spuList)){

break;

}

//构建成Goods

List goodsList = spuList.stream().map(searchService::buildGoods).collect(Collectors.toList());

//存入索引库

goodsRepository.saveAll(goodsList);

//翻页

page++;

size = spuList.size();

}while (size == 100);

}

}

运行测试类

在这里插入图片描述

查询虚拟机发送请求:

http://134.135.131.36:9200/goods/_search

返回结果

{

“took”: 97,

“timed_out”: false,

“_shards”: {

“total”: 1,

“successful”: 1,

“skipped”: 0,

“failed”: 0

},

“hits”: {

“total”: 181,

“max_score”: 1,

“hits”: [

{

“_index”: “goods”,

“_type”: “docs”,

“_id”: “129”,

“_score”: 1,

“_source”: {

“id”: 129,

“all”: “小米(MI) 红米5 plus 手机 (更新)手机 手机通讯 手机小米(MI)”,

“subTitle”: “18:9全面屏,4000mAh大电池,骁龙八核处理器!<a href=“https://item.jd.com/21685362089.html” target=”_blank">32G金色限时8XX秒!",

“brandId”: 18374,

“cid1”: 74,

“cid2”: 75,

“cid3”: 76,

“createTime”: 1524297578000,

“price”: [

105900,

109900,

109900,

109900

],

“skus”: “[{“images”:“http://image.leyou.com/images/13/5/1524297576554.jpg”,“price”:105900,“id”:27359021725,“title”:“小米(MI) 红米5 plus 手机 (更新) 黑色 3GB 32GB”},{“images”:“http://image.leyou.com/images/7/15/1524297577054.jpg”,“price”:109900,“id”:27359021726,“title”:“小米(MI) 红米5 plus 手机 (更新) 金色 3GB 32GB”},{“images”:“http://image.leyou.com/images/0/10/1524297577503.jpg”,“price”:109900,“id”:27359021727,“title”:“小米(MI) 红米5 plus 手机 (更新) 玫瑰金 3GB 32GB”},{“images”:“http://image.leyou.com/images/2/2/1524297577945.jpg”,“price”:109900,“id”:27359021728,“title”:“小米(MI) 红米5 plus 手机 (更新) 浅蓝色 3GB 32GB”}]”,

“specs”: {

“CPU核数”: “八核”,

“后置摄像头”: “1000-1500万”,

“CPU品牌”: “骁龙(Snapdragon)”,

“CPU频率”: “2.0-2.5GHz”,

“操作系统”: “Android”,

“内存”: [

“3GB”

],

“主屏幕尺寸(英寸)”: “5.5-6.0英寸”,

“前置摄像头”: “500-1000万”,

“电池容量(mAh)”: “4000mAh以上”,

“机身存储”: [

“32GB”

]

}

}

},

{

“_index”: “goods”,

“_type”: “docs”,

“_id”: “168”,

“_score”: 1,

“_source”: {

“id”: 168,

“all”: “小米(MI) 小米5X 手机 (更新3)手机 手机通讯 手机小米(MI)”,

“subTitle”: “【爆款低价 移动/公开全网通不做混发,请放心购买!】5.5”屏幕,变焦双摄!<a href=“https://item.jd.com/12068579160.html” target=”_blank">戳戳小米6~",

“brandId”: 18374,

“cid1”: 74,

“cid2”: 75,

小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级前端工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Web前端开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img
img
img
img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频

如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注:前端)
img

MI)",

“subTitle”: “【爆款低价 移动/公开全网通不做混发,请放心购买!】5.5”屏幕,变焦双摄!<a href=“https://item.jd.com/12068579160.html” target=”_blank">戳戳小米6~",

“brandId”: 18374,

“cid1”: 74,

“cid2”: 75,

小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级前端工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Web前端开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

[外链图片转存中…(img-iDd4JfWF-1710881412401)]
[外链图片转存中…(img-69NmvflJ-1710881412402)]
[外链图片转存中…(img-AYm9w858-1710881412403)]
[外链图片转存中…(img-UcHjKygF-1710881412403)]

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频

如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注:前端)
[外链图片转存中…(img-hyM8URwr-1710881412404)]

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/m0_61040489/article/details/136860997

智能推荐

FTP命令字和返回码_ftp 登录返回230-程序员宅基地

文章浏览阅读3.5k次,点赞2次,收藏13次。为了从FTP服务器下载文件,需要要实现一个简单的FTP客户端。FTP(文件传输协议) 是 TCP/IP 协议组中的应用层协议。FTP协议使用字符串格式命令字,每条命令都是一行字符串,以“\r\n”结尾。客户端发送格式是:命令+空格+参数+"\r\n"的格式服务器返回格式是以:状态码+空格+提示字符串+"\r\n"的格式,代码只要解析状态码就可以了。读写文件需要登陆服务器,特殊用..._ftp 登录返回230

centos7安装rabbitmq3.6.5_centos7 安装rabbitmq3.6.5-程序员宅基地

文章浏览阅读648次。前提:systemctl stop firewalld 关闭防火墙关闭selinux查看getenforce临时关闭setenforce 0永久关闭sed-i'/SELINUX/s/enforcing/disabled/'/etc/selinux/configselinux的三种模式enforcing:强制模式,SELinux 运作中,且已经正确的开始限制..._centos7 安装rabbitmq3.6.5

idea导入android工程,idea怎样导入Android studio 项目?-程序员宅基地

文章浏览阅读5.8k次。满意答案s55f2avsx2017.09.05采纳率:46%等级:12已帮助:5646人新版Android Studio/IntelliJ IDEA可以直接导入eclipse项目,不再推荐使用eclipse导出gradle的方式2启动Android Studio/IntelliJ IDEA,选择 import project3选择eclipse 项目4选择 create project f..._android studio 项目导入idea 看不懂安卓项目

浅谈AI大模型技术:概念、发展和应用_ai大模型应用开发-程序员宅基地

文章浏览阅读860次,点赞2次,收藏6次。AI大模型技术已经在自然语言处理、计算机视觉、多模态交互等领域取得了显著的进展和成果,同时也引发了一系列新的挑战和问题,如数据质量、计算效率、知识可解释性、安全可靠性等。城市运维涉及到多个方面,如交通管理、环境监测、公共安全、社会治理等,它们需要处理和分析大量的多模态数据,如图像、视频、语音、文本等,并根据不同的场景和需求,提供合适的决策和响应。知识搜索有多种形式,如语义搜索、对话搜索、图像搜索、视频搜索等,它们可以根据用户的输入和意图,从海量的数据源中检索出最相关的信息,并以友好的方式呈现给用户。_ai大模型应用开发

非常详细的阻抗测试基础知识_阻抗实部和虚部-程序员宅基地

文章浏览阅读8.2k次,点赞12次,收藏121次。为什么要测量阻抗呢?阻抗能代表什么?阻抗测量的注意事项... ...很多人可能会带着一系列的问题来阅读本文。不管是数字电路工程师还是射频工程师,都在关注各类器件的阻抗,本文非常值得一读。全文13000多字,认真读完大概需要2小时。一、阻抗测试基本概念阻抗定义:阻抗是元器件或电路对周期的交流信号的总的反作用。AC 交流测试信号 (幅度和频率)。包括实部和虚部。​图1 阻抗的定义阻抗是评测电路、元件以及制作元件材料的重要参数。那么什么是阻抗呢?让我们先来看一下阻抗的定义。首先阻抗是一个矢量。通常,阻抗是_阻抗实部和虚部

小学生python游戏编程arcade----基本知识1_arcade语言 like-程序员宅基地

文章浏览阅读955次。前面章节分享试用了pyzero,pygame但随着想增加更丰富的游戏内容,好多还要进行自己编写类,从今天开始解绍一个新的python游戏库arcade模块。通过此次的《连连看》游戏实现,让我对swing的相关知识有了进一步的了解,对java这门语言也有了比以前更深刻的认识。java的一些基本语法,比如数据类型、运算符、程序流程控制和数组等,理解更加透彻。java最核心的核心就是面向对象思想,对于这一个概念,终于悟到了一些。_arcade语言 like

随便推点

【增强版短视频去水印源码】去水印微信小程序+去水印软件源码_去水印机要增强版-程序员宅基地

文章浏览阅读1.1k次。源码简介与安装说明:2021增强版短视频去水印源码 去水印微信小程序源码网站 去水印软件源码安装环境(需要材料):备案域名–服务器安装宝塔-安装 Nginx 或者 Apachephp5.6 以上-安装 sg11 插件小程序已自带解析接口,支持全网主流短视频平台,搭建好了就能用注:接口是公益的,那么多人用解析慢是肯定的,前段和后端源码已经打包,上传服务器之后在配置文件修改数据库密码。然后输入自己的域名,进入后台,创建小程序,输入自己的小程序配置即可安装说明:上传源码,修改data/_去水印机要增强版

verilog进阶语法-触发器原语_fdre #(.init(1'b0) // initial value of register (1-程序员宅基地

文章浏览阅读557次。1. 触发器是FPGA存储数据的基本单元2. 触发器作为时序逻辑的基本元件,官方提供了丰富的配置方式,以适应各种可能的应用场景。_fdre #(.init(1'b0) // initial value of register (1'b0 or 1'b1) ) fdce_osc (

嵌入式面试/笔试C相关总结_嵌入式面试笔试c语言知识点-程序员宅基地

文章浏览阅读560次。本该是不同编译器结果不同,但是尝试了g++ msvc都是先计算c,再计算b,最后得到a+b+c是经过赋值以后的b和c参与计算而不是6。由上表可知,将q复制到p数组可以表示为:*p++=*q++,*优先级高,先取到对应q数组的值,然后两个++都是在后面,该行运算完后执行++。在电脑端编译完后会分为text data bss三种,其中text为可执行程序,data为初始化过的ro+rw变量,bss为未初始化或初始化为0变量。_嵌入式面试笔试c语言知识点

57 Things I've Learned Founding 3 Tech Companies_mature-程序员宅基地

文章浏览阅读2.3k次。57 Things I've Learned Founding 3 Tech CompaniesJason Goldberg, Betashop | Oct. 29, 2010, 1:29 PMI’ve been founding andhelping run techn_mature

一个脚本搞定文件合并去重,大数据处理,可以合并几个G以上的文件_python 超大文本合并-程序员宅基地

文章浏览阅读1.9k次。问题:先讲下需求,有若干个文本文件(txt或者csv文件等),每行代表一条数据,现在希望能合并成 1 个文本文件,且需要去除重复行。分析:一向奉行简单原则,如无必要,绝不复杂。如果数据量不大,那么如下两条命令就可以搞定合并:cat a.txt >> new.txtcat b.txt >> new.txt……去重:cat new...._python 超大文本合并

支付宝小程序iOS端过渡页DFLoadingPageRootController分析_类似支付宝页面过度加载页-程序员宅基地

文章浏览阅读489次。这个过渡页是第一次打开小程序展示的,点击某个小程序前把手机的开发者->network link conditioner->enable & very bad network 就会在停在此页。比如《支付宝运动》这个小程序先看这个类的.h可以看到它继承于DTViewController点击左上角返回的方法- (void)back;#import "DTViewController.h"#import "APBaseLoadingV..._类似支付宝页面过度加载页

推荐文章

热门文章

相关标签