Java网络商城项目 SpringBoot+SpringCloud+Vue 网络商城

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)]

  • 15
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 我可以提供一些有关使用JavaSpring Cloud开发电商项目的建议:1.使用Spring Boot搭建你的应用程序,这将极大地简化你的开发过程;2.使用Spring Cloud Netflix来构建你的微服务架构;3.将Spring Data JPA与你的数据库集成,来实现持久化操作;4.使用Spring Cloud Config来实现配置管理;5.使用Spring Security来实现安全认证;6.使用Spring Cloud Netflix Eureka来实现服务注册与发现;7.使用Spring Cloud Netflix Zuul来实现API网关;8.使用Spring Cloud Bus来实现消息总线;9.使用Spring Cloud Stream来实现消息驱动的微服务。 ### 回答2: 使用JavaSpring Cloud开发电商项目可以带来许多好处。Java是一种强大的编程语言,具有良好的跨平台性和丰富的开发工具和框架。而Spring Cloud是一个基于Spring框架的开发工具,它提供了一套方便的微服务组件,可以快速构建分布式系统。 使用JavaSpring Cloud开发电商项目,首先可以利用Java的面向对象特性来设计和开发项目的各个模块,使得代码结构清晰、可维护性强。同时,Java的丰富的库和框架可以提供很多功能模块的实现,例如数据库操作、网络通信、数据加密等,大大缩短了开发周期。 而Spring Cloud作为一个微服务框架,可以提供服务注册与发现、负载均衡、断路器、配置中心等解决方案,可以帮助开发者更轻松地实现分布式系统的各个模块。例如,使用Spring Cloud Eureka进行服务注册与发现,可以方便地管理服务之间的依赖关系,并提供自动负载均衡;使用Spring Cloud Config可以集中管理各个服务的配置信息,方便维护和修改。 在电商项目中,JavaSpring Cloud可以帮助我们实现用户管理、商品管理、订单管理等核心功能。我们可以使用Spring Cloud提供的服务注册与发现功能,将用户服务、商品服务、订单服务等拆分为独立的Spring Boot项目,并使用Feign或RestTemplate实现服务间的调用。这样,我们可以方便地扩展和修改各个模块,提高系统的可扩展性和可维护性。 总之,使用JavaSpring Cloud来开发电商项目是一个可行的选择。Java的强大和Spring Cloud的丰富功能可以帮助我们快速构建分布式系统,并能够方便地扩展和修改各个功能模块,提高开发效率和项目质量。 ### 回答3: 使用JavaSpring Cloud开发一个电商项目可以提供一个高效、可靠的电子商务平台。Java是一种范式化的、面向对象的、编译和解释执行的高级编程语言,它的特点是安全、稳定、可移植性强。而Spring Cloud是一个基于Spring Boot的开发工具箱,它提供了一整套微服务架构的解决方案,包括服务注册与发现、负载均衡、服务容错保护等。 在电商项目中,JavaSpring Cloud的结合可以实现以下功能: 1. 用户认证与授权:可以使用Java中的安全框架和Spring Cloud的微服务架构,实现用户登录、注册、密码加密、授权等功能。 2. 商品管理:可以使用Java的面向对象特性和Spring Cloud的分布式架构,实现商品的增删改查、库存管理、图片上传等功能。 3. 订单管理:可以使用Java的多线程处理和Spring Cloud的分布式事务管理,实现订单的创建、支付、取消等功能。 4. 评论与评分:可以使用Java中的数据库访问技术和Spring Cloud的消息队列,实现用户对商品的评论、评分等功能。 5. 支付与物流:可以使用Java的支付接口和Spring Cloud的服务调用功能,实现用户的在线支付和物流信息查询等功能。 6. 数据统计与分析:可以使用Java的数据处理和Spring Cloud的日志监控功能,实现销售数据的实时统计和分析报表的生成。 总之,使用JavaSpring Cloud开发电商项目可以充分发挥Java的稳定性和可移植性,以及Spring Cloud的微服务架构和分布式系统的优势,实现一个高效、可靠的电子商务平台。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值