当前位置: 首页 > article >正文

Springboot接入Elastic

1. 添加依赖

首先,需要在pom.xml文件中添加Spring Data Elasticsearch的依赖:

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

2. 配置Elasticsearch连接

application.propertiesapplication.yml文件中配置Elasticsearch连接信息:

spring:
  elasticsearch:
    uris: http://localhost:9200

如果Elasticsearch需要身份验证,则添加用户名和密码:

spring:
  elasticsearch:
    username: your-username
    password: your-password

3. 创建实体类

创建一个实体类,并使用Elasticsearch的注解来标记其映射:

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

@Document(indexName = "your_index_name")
public class YourEntity {

    @Id
    private String id;
    private String field1;
    private String field2;

    // Getters and setters
}

4. 创建Repository接口

import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

public interface YourEntityRepository extends ElasticsearchRepository<YourEntity, String> {
    // 你可以在这里定义自定义查询方法
}

5. 实现增删改查操作

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Optional;

@Service
public class YourEntityService {

    @Autowired
    private YourEntityRepository repository;

    public YourEntity save(YourEntity entity) {
        return repository.save(entity);
    }

    public Optional<YourEntity> findById(String id) {
        return repository.findById(id);
    }

    public Iterable<YourEntity> findAll() {
        return repository.findAll();
    }

    public void deleteById(String id) {
        repository.deleteById(id);
    }

    public YourEntity update(String id, YourEntity updatedEntity) {
        if (repository.existsById(id)) {
            updatedEntity.setId(id);
            return repository.save(updatedEntity);
        }
        return null;
    }
}

6. 使用

import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/entities")
public class YourEntityController {

    @Autowired
    private YourEntityService service;

    @PostMapping
    public YourEntity createEntity(@RequestBody YourEntity entity) {
        return service.save(entity);
    }

    @GetMapping("/{id}")
    public YourEntity getEntity(@PathVariable String id) {
        return service.findById(id).orElse(null);
    }

    @GetMapping
    public Iterable<YourEntity> getAllEntities() {
        return service.findAll();
    }

    @PutMapping("/{id}")
    public YourEntity updateEntity(@PathVariable String id, @RequestBody YourEntity updatedEntity) {
        return service.update(id, updatedEntity);
    }

    @DeleteMapping("/{id}")
    public void deleteEntity(@PathVariable String id) {
        service.deleteById(id);
    }
}

http://www.kler.cn/news/355715.html

相关文章:

  • AWTK fscript 中的 widget 扩展函数
  • C++ 学习笔记 十二 结构体
  • 深度学习框架-Keras的常用内置数据集总结
  • nacos组件介绍
  • 智能指针(3)
  • 构建 effet.js 人脸识别交互系统的实战之路
  • Linux 和Windows创建共享文件夹实现文件共享
  • 产品推介——高压晶体管光耦KL851
  • Mybatis多对一查询的配置及两种方法的使用示例对比以及Mybatis一对多查询两种方法使用示例及对比
  • Android中的Room数据库框架
  • 如何使用代理来保护你的电子邮件?
  • 了解一些常用的Javascript对象方法
  • Python网络爬虫入门指南
  • 使用JavaScript开发扑克牌游戏:从零开始的前端之旅
  • Vscode+Pycharm+Vue.js+WEUI+django火锅(7) 傍着Copliot战WEUI Picker
  • React高级Hook
  • 遇到 msvcp120.dll 文件缺失的情况如何处理?全面分析msvcp120.dll
  • kubernets(二)
  • 【OpenGL】创建窗口/绘制图形
  • mailx邮件服务器的配置