springboot使用配置ElasticSearch完整保姆全教程

目录

    • 简介
    • 前置条件
    • springboot配置
      • pom配置
      • spring配置
      • maven配置
      • Gradle配置
    • 我的Dao层
    • 我的repository层
    • 我的service层
    • 测试使用
    • API接口文档

简介

因为搜了半天,找不到一个完整的从0到1的全过程,所以写这个记录一下,防止后续忘记

前置条件

springboot版本:2.6.X
ElasticSearch版本:7.15.2
spring官方支持的版本
官方下载链接ES7.15.2

springboot配置

pom配置


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.7.3version>
        <relativePath/> 
    parent>
    <groupId>com.examplegroupId>
    <artifactId>demoartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>demoname>
    <description>Demo project for Spring Boot JPAdescription>
    <properties>
        <java.version>1.8java.version>
    properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-jpaartifactId>
        dependency>

        <dependency>
            <groupId>com.h2databasegroupId>
            <artifactId>h2artifactId>
            <scope>runtimescope>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
        dependency>
        
        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-boot-starterartifactId>
            <version>3.5.2version>
        dependency>
        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plusartifactId>
            <version>3.5.2version>
        dependency>
        
        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-generatorartifactId>
            <version>3.5.3version>
        dependency>
        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-elasticsearchartifactId>
        dependency>

    dependencies>

    <build>
        <plugins>
            
            
            
            
        plugins>
    build>

project>

spring配置

springboot使用配置ElasticSearch完整保姆全教程_第1张图片

spring:
  elasticsearch:
    uris: http://127.0.0.1:9200

maven配置

你要配置maven的版本,以及本地库,镜像下载源,这些东西百度
整完以后,开始加载maven
springboot使用配置ElasticSearch完整保姆全教程_第2张图片
点击左上角这个刷新的小圈圈,然后等待maven下载,加载完成

Gradle配置

在build.gradle中加上一行

	//ES依赖
	implementation 'org.springframework.boot:spring-boot-starter-data-elasticsearch'

我的Dao层

package com.example.demo.dao;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import org.springframework.data.elasticsearch.annotations.Document;

import javax.persistence.Column;
import javax.persistence.Id;
import java.time.LocalDateTime;

/**
 * @descriptions: 文章实体
 * @author: zhangpeng
 * @date: 2022/10/20 14:35
 * @version: 1.0
 */
@Data
@Document(indexName = "article")
public class Article {
    @Id
    private String id;

    @Column(columnDefinition="varchar(32) COMMENT '连接主表的article_id'")
    private String articleId;

    @Column(columnDefinition="varchar(32) COMMENT '作者用户open id'")
    private String openId;


    @Column(columnDefinition="varchar(191) COMMENT '文章标题'")
    private String title;


    @Column(columnDefinition="varchar(33) COMMENT '文章分类id'")
    private String categoryId;


    @Column(columnDefinition="int(2) COMMENT '匿名性,匿名1,公开0'")
    private int anonymity;

    @Column(columnDefinition="int(2) COMMENT '帖子状态(删除0,草稿1,发布2,隐藏3)默认草稿'")
    private int state;

    @Column(columnDefinition="longtext COMMENT '帖子内容'")
    private String content;

    @CreationTimestamp
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createdTime;

    @UpdateTimestamp
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updatedTime;


    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime deletedTime;
}

我的repository层

package com.example.demo.repository;

import com.example.demo.dao.Article;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

/**
 * @descriptions: 文章reporsity
 * @author: zhangpeng
 * @date: 2022/10/20 14:38
 * @version: 1.0
 */
public interface ArticleRepository extends ElasticsearchRepository<Article, String> {
    Article findFirstByArticleId(String articleId);
}

我的service层

package com.example.demo.service;

import com.example.demo.dao.Article;
import com.example.demo.repository.ArticleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @descriptions: 文章service
 * @author: zhangpeng
 * @date: 2022/10/20 14:42
 * @version: 1.0
 */
@Service
public class ArticleService {
    @Autowired
    ArticleRepository articleRepo;
    public Article getArticle(String articleId){
        Article firstByArticleId = articleRepo.findFirstByArticleId(articleId);
        return firstByArticleId;
    }
    public Article addArticle(Article article){
       return articleRepo.save(article);
    }
}

测试使用

package com.example.demo.service;

import com.example.demo.DemoApplication;
import com.example.demo.dao.Article;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest(
        /*MyframeApplication对应项目的启动类。如果不写这个的话,下面的service就无法自动注入。
         测试类启动时会先启动项目,再执行测试方法。也就是说测试类的测试依赖项目启动环境。*/
        classes = DemoApplication.class,
        /*因为测试的是service,禁用web以加快项目启动速度*/
        webEnvironment = SpringBootTest.WebEnvironment.NONE
)
class ArticleServiceTest {
    @Autowired
    ArticleService articleService;
    @Test
    void getArticle() {
        Article article = articleService.getArticle("no");
        System.out.println(article);
    }

    @Test
    void addArticle() {
        Article article = new Article();
        article.setArticleId("no");
        article.setContent("Hello, my broher. ");
        article.setCategoryId("1");
        articleService.addArticle(article);
    }
}

API接口文档

我写了一些接口,可以看你的es数据库数据,便于更好的测试数据
es学习使用
http-api文档

你可能感兴趣的:(spring,boot,elasticsearch,java)