From 9d477774a03088a80cfecedae33c9a9db34a6f07 Mon Sep 17 00:00:00 2001 From: wuwenbi Date: Thu, 6 Apr 2023 17:36:05 +0800 Subject: [PATCH] =?UTF-8?q?bug=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 9 ++ .../zhunong/controller/OrderController.java | 3 + .../java/com/wwb/zhunong/util/EsUtils.java | 126 ++++++++++++++++++ .../java/com/wwb/zhunong/util/MinIOUtil.java | 6 +- src/main/resources/application.yml | 6 +- 5 files changed, 146 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/wwb/zhunong/util/EsUtils.java diff --git a/pom.xml b/pom.xml index 528b3bb..0e1bea9 100644 --- a/pom.xml +++ b/pom.xml @@ -83,6 +83,15 @@ fastjson 1.2.40 + + + + org.elasticsearch.client + elasticsearch-rest-high-level-client + diff --git a/src/main/java/com/wwb/zhunong/controller/OrderController.java b/src/main/java/com/wwb/zhunong/controller/OrderController.java index 766f89d..4029c2e 100644 --- a/src/main/java/com/wwb/zhunong/controller/OrderController.java +++ b/src/main/java/com/wwb/zhunong/controller/OrderController.java @@ -55,6 +55,9 @@ public class OrderController { } order.setOrderNo("JAVA"+ DateUtil.getCurrentDateStr()); order.setCreateDate(new Date()); + // 此处模拟支付成功修改订单表数据 + order.setStatus(2); + order.setPayDate(new Date()); OrderDetail[] goods = order.getGoods(); orderService.save(order); diff --git a/src/main/java/com/wwb/zhunong/util/EsUtils.java b/src/main/java/com/wwb/zhunong/util/EsUtils.java new file mode 100644 index 0000000..544f87c --- /dev/null +++ b/src/main/java/com/wwb/zhunong/util/EsUtils.java @@ -0,0 +1,126 @@ +package com.wwb.zhunong.util; + +import com.alibaba.fastjson.JSON; +import lombok.extern.slf4j.Slf4j; +import org.elasticsearch.action.bulk.BulkRequest; +import org.elasticsearch.action.bulk.BulkResponse; +import org.elasticsearch.action.get.GetRequest; +import org.elasticsearch.action.get.GetResponse; +import org.elasticsearch.action.index.IndexRequest; +import org.elasticsearch.action.index.IndexResponse; +import org.elasticsearch.action.search.SearchRequest; +import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.action.update.UpdateRequest; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.RestHighLevelClient; +import org.elasticsearch.common.Strings; +import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.index.query.IdsQueryBuilder; +import org.elasticsearch.index.query.QueryBuilders; +import org.elasticsearch.search.builder.SearchSourceBuilder; +import org.elasticsearch.search.fetch.subphase.FetchSourceContext; +import org.springframework.stereotype.Component; +import org.springframework.util.CollectionUtils; + +import javax.annotation.Resource; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * es工具 + */ +@Slf4j +@Component +public class EsUtils { + + @Resource + private RestHighLevelClient restHighLevelClient; + + /** + * es 查询一个对象 + * + * @param indexName indexName + * @param indexType indexType + * @param id id + * @param clazz 需要转换的类型 + * @param 返回的类型 + * @return 可能返回null + */ + public T getById(String indexName, String indexType, String id, Class clazz) { + try { + GetRequest getRequest = new GetRequest(indexName, indexType, id); + GetResponse getResponse = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT); + return JSON.parseObject(JSON.toJSONString(getResponse.getSource()), clazz); + } catch (Exception e) { + log.error("es 查询错误 indexName :{},id :{}", indexName, id); + } + return null; + } + + + /** + * 创建或修改一个索引 + * + * @param indexName indexName + * @param indexType indexType + * @param id id + * @param jsonString json 字符串 + * @return true 成功 false 失败 + * @return + */ + public Boolean createOrderUpdate(String indexName, String indexType, String id, String jsonString) { + try { + IndexRequest indexRequest = new IndexRequest(indexName, indexType); + indexRequest.id(id); + indexRequest.source(jsonString, XContentType.JSON); + IndexResponse index = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT); + log.info("【添加 es数据请求】返回值 : {}", index.getId()); + return true; + } catch (Exception e) { + log.error("【添加 es数据请求】发生异常", e); + return false; + } + } + + + + /** + * 根据主键查询多个数据 + * + * @param indexName indexName + * @param clazz 响应对象class + * @param idList 主键id + * @param field 需要查询的属性 + * @param 返回数据 + * @return + */ + public List listById(String indexName, Class clazz, List idList, List field) { + try { + SearchRequest searchRequest = new SearchRequest(indexName); + SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); + IdsQueryBuilder idsQueryBuilder = QueryBuilders.idsQuery(); + idsQueryBuilder.addIds(idList.toArray(new String[idList.size()])); + searchSourceBuilder.query(idsQueryBuilder); + if (!CollectionUtils.isEmpty(field)) { + searchSourceBuilder + .fetchSource(new FetchSourceContext(true, field.toArray(new String[field.size()]), Strings.EMPTY_ARRAY)); + } + searchSourceBuilder.from(0).size(idList.size()); + searchRequest.source(searchSourceBuilder); + + SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT); + + return Arrays.stream(searchResponse.getHits().getHits()) + .map(item -> JSON.parseObject(item.getSourceAsString(), clazz)) + .collect(Collectors.toList()); + } catch (Exception e) { + log.error("es 查询异常", e); + } + return Collections.emptyList(); + } + + +} diff --git a/src/main/java/com/wwb/zhunong/util/MinIOUtil.java b/src/main/java/com/wwb/zhunong/util/MinIOUtil.java index 936726f..4130c7a 100644 --- a/src/main/java/com/wwb/zhunong/util/MinIOUtil.java +++ b/src/main/java/com/wwb/zhunong/util/MinIOUtil.java @@ -30,7 +30,7 @@ public class MinIOUtil { /** * Bucket所在地域对应的Endpoint */ - private static String endPoint="http://192.168.181.128:9000"; + private static String endPoint="http://123.249.127.68:9000"; /** * 存储桶名称 @@ -40,12 +40,12 @@ public class MinIOUtil { /** * 用户名 */ - private static String accessKey="NqhWmuYInBXNlVOM"; + private static String accessKey="MPV4JXXB3FO2NUCDRXHZ"; /** * 密码 */ - private static String secretKey="I60UzlnqEbkKAo9eyFYLY15SDj964ALM"; + private static String secretKey="nTfQTN9vi0GZL8bzMRexP3CFQVmfTzKv829vQp0V"; /** * 分隔符 diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 8b8b6b4..acdc032 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -7,7 +7,7 @@ spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver - url: jdbc:mysql://192.168.181.128:3306/db_zhunong?serverTimezone=Asia/Shanghai&characterEncoding=utf8 + url: jdbc:mysql://123.249.127.68:3306/db_zhunong?serverTimezone=Asia/Shanghai&characterEncoding=utf8 username: root password: 123456 redis: @@ -17,6 +17,10 @@ spring: multipart: max-file-size: 20MB max-request-size: 20MB + elasticsearch: + rest: + uris: http://192.168.181.128:9200 + mybatis-plus: global-config: -- Gitee