Author:Gorit
Date:2020年10月21日
project:SpringBoot 2.3.4 + ElasticSearch 7.6.2
target:实现了一个最简单 CRUD 操作
Other:Idea 2020,Postman

一、搭建 ES 环境

没有学过 ES 的可以先看这篇文章

  1. 下载,解压即可
  2. 运行启动:localhost:9200

二、SpringBoot 整合 ES 快速入门

2.1 导入 ES 相关坐标依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
  8. </dependency>

2.2 配置文件

application.yml 中可以不用任何配置,网上现有的内容大多数是基于 6.x 版本的,到了 7.x 版本就不适用了

2.3 编写JAVA Bean

  1. entity ```java package com.example.entity;

import org.springframework.data.annotation.Id; import org.springframework.data.elasticsearch.annotations.Document;

/**

  • indexName 指定所在索引,type 指定其所在的类型,type 属性将在 ES 8.x 版本移除,默认是 _doc */ @Document(indexName = “blog”) public class Blog { @Id private Integer id; private String title; private String content; private String authName;

    public Integer getId() {

    1. return id;

    }

    public void setId(Integer id) {

     this.id = id;
    

    }

    public String getTitle() {

     return title;
    

    }

    public void setTitle(String title) {

     this.title = title;
    

    }

    public String getContent() {

     return content;
    

    }

    public void setContent(String content) {

     this.content = content;
    

    }

    public String getAuthName() {

     return authName;
    

    }

    public void setAuthName(String authName) {

     this.authName = authName;
    

    }

    @Override public String toString() {

     return "Blog{" +
             "id=" + id +
             ", title='" + title + '\'' +
             ", content='" + content + '\'' +
             ", authName='" + authName + '\'' +
             '}';
    

    } } ```

    2.4 编写 dao 层接口

    ```java package com.example.dao;

import com.example.entity.Blog; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

import java.util.List;

public interface ArticleRepository extends ElasticsearchRepository {

List<Blog> findBlogByAuthName(String authName);

List<Blog> findByTitle(String title);

}

<a name="x5vCJ"></a>
### 2.5  编写 Controller 层
```java
package com.example.controller;

import com.example.dao.ArticleRepository;
import com.example.entity.Blog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class TestController {

    @Autowired
    ArticleRepository repository;

    // 添加(也可以添加同名的,但是如果缺少字段,
    @RequestMapping(value = "/blog",method = RequestMethod.POST)
    public Object setMsg(@RequestBody Blog blog) {
        repository.save(blog);
        return "add OK";
    }

    // 查询单个
    @RequestMapping(value = "/blog/{key}",method = RequestMethod.GET)
    public Object getValueBykey(@PathVariable("key") Integer key) {
        return repository.findById(key);
    }
    // 查询所有
    @RequestMapping(value = "/blog",method = RequestMethod.GET)
    public Object getAll() {
        return repository.findAll();
    }

    // 修改
    @RequestMapping(value = "/blog",method = RequestMethod.PUT)
    public Object update(@RequestBody Blog blog) {
       return repository.save(blog);
    }

    // 删除
    @RequestMapping(value = "/blog/{key}",method = RequestMethod.DELETE)
    public Object deleteById(@PathVariable("key") Integer key) {
        repository.deleteById(key);
        return "delete success";
    }

    // 根据作者名称查询
    @RequestMapping(value = "/blog/u/{name}",method = RequestMethod.GET)
    public List<Blog> findByAuthName(@PathVariable("name") String name) {
        return repository.findBlogByAuthName(name);
    }

    // 根据文章标题查询
    @RequestMapping(value = "/blog/t/{title}",method = RequestMethod.GET)
    public List<Blog> findByTitle (@PathVariable("title") String title) {
        return repository.findByTitle(title);
    }
}

2.5 项目截图

image.png
image.png