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

智能推荐

获取大于等于一个整数的最小2次幂算法(HashMap#tableSizeFor)_整数 最小的2的几次方-程序员宅基地

文章浏览阅读2w次,点赞51次,收藏33次。一、需求给定一个整数,返回大于等于该整数的最小2次幂(2的乘方)。例: 输入 输出 -1 1 1 1 3 4 9 16 15 16二、分析当遇到这个需求的时候,我们可能会很容易想到一个"笨"办法:..._整数 最小的2的几次方

Linux 中 ss 命令的使用实例_ss@,,x,, 0-程序员宅基地

文章浏览阅读865次。选项,以防止命令将 IP 地址解析为主机名。如果只想在命令的输出中显示 unix套接字 连接,可以使用。不带任何选项,用来显示已建立连接的所有套接字的列表。如果只想在命令的输出中显示 tcp 连接,可以使用。如果只想在命令的输出中显示 udp 连接,可以使用。如果不想将ip地址解析为主机名称,可以使用。如果要取消命令输出中的标题行,可以使用。如果只想显示被侦听的套接字,可以使用。如果只想显示ipv4侦听的,可以使用。如果只想显示ipv6侦听的,可以使用。_ss@,,x,, 0

conda activate qiuqiu出现不存在activate_commandnotfounderror: 'activate-程序员宅基地

文章浏览阅读568次。CommandNotFoundError: 'activate'_commandnotfounderror: 'activate

Kafka 实战 - Windows10安装Kafka_win10安装部署kafka-程序员宅基地

文章浏览阅读426次,点赞10次,收藏19次。完成以上步骤后,您已在 Windows 10 上成功安装并验证了 Apache Kafka。在生产环境中,通常会将 Kafka 与外部 ZooKeeper 集群配合使用,并考虑配置安全、监控、持久化存储等高级特性。在生产者窗口中输入一些文本消息,然后按 Enter 发送。ZooKeeper 会在新窗口中运行。在另一个命令提示符窗口中,同样切换到 Kafka 的。Kafka 服务器将在新窗口中运行。在新的命令提示符窗口中,切换到 Kafka 的。,应显示已安装的 Java 版本信息。_win10安装部署kafka

【愚公系列】2023年12月 WEBGL专题-缓冲区对象_js 缓冲数据 new float32array-程序员宅基地

文章浏览阅读1.4w次。缓冲区对象(Buffer Object)是在OpenGL中用于存储和管理数据的一种机制。缓冲区对象可以存储各种类型的数据,例如顶点、纹理坐标、颜色等。在渲染过程中,缓冲区对象中存储的数据可以被复制到渲染管线的不同阶段中,例如顶点着色器、几何着色器和片段着色器等,以完成渲染操作。相比传统的CPU访问内存,缓冲区对象的数据存储和管理更加高效,能够提高OpenGL应用的性能表现。_js 缓冲数据 new float32array

四、数学建模之图与网络模型_图论与网络优化数学建模-程序员宅基地

文章浏览阅读912次。(1)图(Graph):图是数学和计算机科学中的一个抽象概念,它由一组节点(顶点)和连接这些节点的边组成。图可以是有向的(有方向的,边有箭头表示方向)或无向的(没有方向的,边没有箭头表示方向)。图用于表示各种关系,如社交网络、电路、地图、组织结构等。(2)网络(Network):网络是一个更广泛的概念,可以包括各种不同类型的连接元素,不仅仅是图中的节点和边。网络可以包括节点、边、连接线、路由器、服务器、通信协议等多种组成部分。网络的概念在各个领域都有应用,包括计算机网络、社交网络、电力网络、交通网络等。_图论与网络优化数学建模

随便推点

android 加载布局状态封装_adnroid加载数据转圈封装全屏转圈封装-程序员宅基地

文章浏览阅读1.5k次。我们经常会碰见 正在加载中,加载出错, “暂无商品”等一系列的相似的布局,因为我们有很多请求网络数据的页面,我们不可能每一个页面都写几个“正在加载中”等布局吧,这时候将这些状态的布局封装在一起就很有必要了。我们可以将这些封装为一个自定布局,然后每次操作该自定义类的方法就行了。 首先一般来说,从服务器拉去数据之前都是“正在加载”页面, 加载成功之后“正在加载”页面消失,展示数据;如果加载失败,就展示_adnroid加载数据转圈封装全屏转圈封装

阿里云服务器(Alibaba Cloud Linux 3)安装部署Mysql8-程序员宅基地

文章浏览阅读1.6k次,点赞23次,收藏29次。PS: 如果执行sudo grep 'temporary password' /var/log/mysqld.log 后没有报错,也没有任何结果显示,说明默认密码为空,可以直接进行下一步(后面设置密码时直接填写新密码就行)。3.(可选)当操作系统为Alibaba Cloud Linux 3时,执行如下命令,安装MySQL所需的库文件。下面示例中,将创建新的MySQL账号,用于远程访问MySQL。2.依次运行以下命令,创建远程登录MySQL的账号,并允许远程主机使用该账号访问MySQL。_alibaba cloud linux 3

excel离散度图表怎么算_excel离散数据表格-Excel 离散程度分析图表如何做-程序员宅基地

文章浏览阅读7.8k次。EXCEL中数据如何做离散性分析纠错。离散不是均值抄AVEDEV……=AVEDEV(A1:A100)算出来的是A1:A100的平均数。离散是指各项目间指标袭的离散均值(各数值的波动情况),数值较低表明项目间各指标波动幅百度小,数值高表明波动幅度较大。可以用excel中的离散公式为STDEV.P(即各指标平均离散)算出最终度离散度。excel表格函数求一组离散型数据,例如,几组C25的...用exc..._excel数据分析离散

学生时期学习资源同步-JavaSE理论知识-程序员宅基地

文章浏览阅读406次,点赞7次,收藏8次。i < 5){ //第3行。int count;System.out.println ("危险!System.out.println(”真”);System.out.println(”假”);System.out.print(“姓名:”);System.out.println("无匹配");System.out.println ("安全");

linux 性能测试磁盘状态监测:iostat监控学习,包含/proc/diskstats、/proc/stat简单了解-程序员宅基地

文章浏览阅读3.6k次。背景测试到性能、压力时,经常需要查看磁盘、网络、内存、cpu的性能值这里简单介绍下各个指标的含义一般磁盘比较关注的就是磁盘的iops,读写速度以及%util(看磁盘是否忙碌)CPU一般比较关注,idle 空闲,有时候也查看wait (如果wait特别大往往是io这边已经达到了瓶颈)iostatiostat uses the files below to create ..._/proc/diskstat

glReadPixels读取保存图片全黑_glreadpixels 全黑-程序员宅基地

文章浏览阅读2.4k次。问题:在Android上使用 glReadPixel 读取当前渲染数据,在若干机型(华为P9以及魅族某魅蓝手机)上读取数据失败,glGetError()没有抓到错误,但是获取到的数据有误,如果将获取到的数据保存成为图片,得到的图片为黑色。解决方法:glReadPixels实际上是从缓冲区中读取数据,如果使用了双缓冲区,则默认是从正在显示的缓冲(即前缓冲)中读取,而绘制工作是默认绘制到后缓..._glreadpixels 全黑