欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringData整合ElasticSearch實(shí)現(xiàn)CRUD的示例代碼(超詳細(xì))

 更新時間:2023年07月21日 11:54:47   作者:秋日的晚霞  
本文主要介紹了SpringData整合ElasticSearch實(shí)現(xiàn)CRUD的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

1.導(dǎo)入依賴

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
            <version>2.6.4</version>
        </dependency>

2.配置 yml

spring:
  elasticsearch:
    rest:
      uris:
        - http://xxxxx:9200

3.創(chuàng)建Bean

  • @Document( indexName= xxx) ES 的索引名
  • @Id ES 的文檔ID
  • @Field ES的字段映射
  • type = FieldType.Keyword 關(guān)鍵字 不分詞 ( ES中沒有String 只有 text (分詞) 和 keyword ( 不分詞 )
  • index 是否能索引
  • analyzer 使用的分詞器
  • format 格式轉(zhuǎn)換 pattern 日期格式
  • FieldType.Nested 集合屬性 避免查出業(yè)務(wù)錯誤
@Document(indexName = "goods" , shards = 3,replicas = 2)
public class Goods {
    // 商品Id skuId
    @Id
    private Long id;
    @Field(type = FieldType.Keyword, index = false)
    private String defaultImg;
    //  es 中能分詞的字段,這個字段數(shù)據(jù)類型必須是 text!keyword 不分詞!
    @Field(type = FieldType.Text, analyzer = "ik_max_word")
    private String title;
    @Field(type = FieldType.Double)
    private Double price;
    //  @Field(type = FieldType.Date)   6.8.1
    @Field(type = FieldType.Date,format = DateFormat.custom,pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime; // 新品
    @Field(type = FieldType.Long)
    private Long tmId;
    @Field(type = FieldType.Keyword)
    private String tmName;
    @Field(type = FieldType.Keyword)
    private String tmLogoUrl;
    @Field(type = FieldType.Long)
    private Long category1Id;
    @Field(type = FieldType.Keyword)
    private String category1Name;
    @Field(type = FieldType.Long)
    private Long category2Id;
    @Field(type = FieldType.Keyword)
    private String category2Name;
    @Field(type = FieldType.Long)
    private Long category3Id;
    @Field(type = FieldType.Keyword)
    private String category3Name;
    //  商品的熱度! 我們將商品被用戶點(diǎn)查看的次數(shù)越多,則說明熱度就越高!
    @Field(type = FieldType.Long)
    private Long hotScore = 0L;
    // 平臺屬性集合對象
    // Nested 支持嵌套查詢
    @Field(type = FieldType.Nested)
    private List<SearchAttr> attrs;
}

4.創(chuàng)建接口繼承 CrudRepository 接口

泛型1 : ES對應(yīng)的javaBean

泛型2 : 文檔唯一ID的類型

@Repository
public interface GoodsDao extends CrudRepository<Goods,Long> {
}

注意 如果想實(shí)現(xiàn)分頁 請實(shí)現(xiàn) PagingAndSortingRepository 接口

@Repository
public interface GoodsDao extends PagingAndSortingRepository<Goods,Long> {
}

接口上添加 @Repository 注解

image-20220314200112925

5. 創(chuàng)建service 注入 接口代理類對象

@Service
public class GoodsServiceImpl implements GoodService {
    @Autowired
    private GoodsDao goodsDao;
    @Override
    public boolean onSale(Goods goods) {
        Goods save = goodsDao.save(goods);
       return !StringUtils.isEmpty(save);
    }
}

6.主啟動類上添加 @EnableElasticsearchRepositories

@EnableElasticsearchRepositories
@SpringCloudApplication
public class EsListApp {
    public static void main(String[] args) {
        SpringApplication.run(EsListApp.class);
    }
}

7.編寫方法名

小提示 先寫返回值類型 這樣有提示

idea64_vJZvPkhh48

命名規(guī)則 Spring Data Commons - 參考文檔

到此這篇關(guān)于SpringData整合ElasticSearch實(shí)現(xiàn)CRUD的示例代碼(超詳細(xì))的文章就介紹到這了,更多相關(guān)SpringData ElasticSearch實(shí)現(xiàn)CRUD內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring AOP在web應(yīng)用中的使用方法實(shí)例

    Spring AOP在web應(yīng)用中的使用方法實(shí)例

    這篇文章主要給大家介紹了關(guān)于Spring AOP在web應(yīng)用中的使用方法,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Spring AOP具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • java在集合遍歷過程中刪除元素5種方法對比、案例、常見的錯誤及其后果

    java在集合遍歷過程中刪除元素5種方法對比、案例、常見的錯誤及其后果

    這篇文章主要介紹了java在集合遍歷過程中刪除元素5種方法對比、案例、常見的錯誤及其后果的相關(guān)資料,介紹了五種不同的解決方案,包括使用Iterator.remove()、for-each+手動刪除、for循環(huán)反向遍歷、List.removeIf()和使用Stream.filter(),需要的朋友可以參考下
    2024-12-12
  • Java五子棋簡單實(shí)現(xiàn)代碼舉例

    Java五子棋簡單實(shí)現(xiàn)代碼舉例

    Java五子棋游戲是一種經(jīng)典的兩人對戰(zhàn)棋類游戲,它基于簡單的規(guī)則,即任何一方的棋子在棋盤上形成連續(xù)的五個,無論是橫、豎還是斜線,都將獲勝,這篇文章主要介紹了Java五子棋實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下
    2024-10-10
  • Java中日期與時間的處理及工具類封裝詳解

    Java中日期與時間的處理及工具類封裝詳解

    在項(xiàng)目開發(fā)中免不了有對日期時間的處理,但Java中關(guān)于日期時間的類太多了,本文就來介紹一下各種類的使用及我們項(xiàng)目中應(yīng)該怎么選擇吧
    2023-07-07
  • 解決Maven中關(guān)于依賴導(dǎo)入不進(jìn)的問題

    解決Maven中關(guān)于依賴導(dǎo)入不進(jìn)的問題

    這篇文章主要介紹了解決Maven中關(guān)于依賴導(dǎo)入不進(jìn)的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • Spring整合消息隊(duì)列RabbitMQ流程

    Spring整合消息隊(duì)列RabbitMQ流程

    Spring整合RabbitMQ很容易,但是整合的目的是為了使用,那要使用RabbitMQ就要對其有一定的了解,不然容易整成一團(tuán)漿糊。因?yàn)檎f到底,Spring只是在封裝RabbitMQ的API,讓其更容易使用而已,廢話不多說,讓我們一起整它
    2023-03-03
  • java可變參數(shù)(不定向參數(shù))的作用與實(shí)例

    java可變參數(shù)(不定向參數(shù))的作用與實(shí)例

    這篇文章主要給大家介紹了關(guān)于java可變參數(shù)(不定向參數(shù))的作用與實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • Struts2框架初學(xué)接觸

    Struts2框架初學(xué)接觸

    本文主要給大家從初學(xué)者的角度介紹了Struts2框架結(jié)構(gòu)和基本頁面代碼等內(nèi)容,一起來學(xué)習(xí)一下。
    2017-11-11
  • spring boot入門開始你的第一個應(yīng)用

    spring boot入門開始你的第一個應(yīng)用

    這篇文章主要介紹了spring boot入門開始你的第一個應(yīng)用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,,需要的朋友可以參考下
    2019-06-06
  • 關(guān)于Java日期工具類的編寫

    關(guān)于Java日期工具類的編寫

    這篇文章主要介紹了關(guān)于Java日期工具類的編寫,在Java開發(fā)中,經(jīng)常會遇到處理日期相關(guān)的數(shù)據(jù),那么今天我們來自己寫一個工具類,文中有詳細(xì)的實(shí)例代碼以及實(shí)現(xiàn)思路,需要的朋友可以參考下
    2023-05-05

最新評論