完整教程:spring-boot-starter-data-elasticsearch 整合。。。
1、前⾔
⽹上很多⾔论:
spring data elasticsearch
elasticsearch 3.2.x
6.5.03.1.x
6.2.23.0.x
5.5.02.1.x
2.4.02.0.x
2.2.01.
3.x    1.5.2
⼀开始我也信了。 今天使⽤SpringBoot 2的spring-boot-starter-data-elasticsearch整合elasticsearch 6.x,测试了⼀下。实践证明是可以的。
1.1 Elasticsearch 6.x
已有5个节点的Elasticsearch 集,版本是6.2.3。
2、创建SpringBoot 2.1 项⽬
2.l 新版本的SpringBoot 2的spring-boot-starter-data-elasticsearch 中⽀持的Elasticsearch 版本是2.X ,但Elasticsearch 实际上已经发展到6.5.X 版本了,为了更好的使⽤Elasticsearch 的新特性,
所以弃⽤了spring-boot-starter-data-elasticsearch 依赖,⽽改为直接使⽤Spring-data-elasticsearch
1
2
3
2.2 application.properties <?xml version="1.0" encoding="UTF-8"?><project xmlns="/POM/4.0.0" xmlns:xsi="www.w
4.0.0 /xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <pa
rent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>2.1.1.RELEASE</version>        <relativePath/> <!-- lookup parent from repository -->    </parent>    <groupId>cn.hadron</groupId>    <artifactId>demo</artifactId>    <version>0.0.1-SNAPSHOT</version>    <name>demo</name>    <description>Demo project for Spring Boot</description>    <properties>        <java.version>1.8</java.version>    </properties>    <repositories>        <repository>            <id>nexus-aliyun</id>            <name>Nexus aliyun</name>            <url>maven.aliyun/nexus/content/groups/public</url>        </repository>    </repositories>    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-thymeleaf</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test</scope>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifac
tId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build></project>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
springboot中文46
47
48
49
50
51
52
53
54
55
56
57
58
59
2.3 实体类# ELASTICSEARCH (ElasticsearchProperties)# Elasticsearch cluster name.spring.data.elasticsearch.cluster-name=elasticsearch # Comma-separated list of cluster node addresses.spring.data.elasticsearch.cluster-nodes=192.168.12.160:9300# Whether to enable Elasticsearch repositories.spring.abled=true
1
2
3
4
5
6
7package cn.hadron.demo.bean;import org.springframework.data.annotation.Id;import org.springframework.data.elasticsearch.annotations.Document;@Document(indexName = "book", type = "_doc")public class BookBean {    @Id    private String id;    private String title;    private String author;    private String postDate;    public BookBean(){}    public BookBean(String id, String title, String author, String postDate){        this.id=id;        this.title=title;        this.author=author;        this.postDate=postDate;    }    public String getId() {        return id;    }    public void setId(String id) {        this.id = id;    }    public String getTitle() {        return title;    }    public void setTitle(String title) {        this.title = title;    }    public String getAuthor() {        return author;    }    public void setAuthor(String author) {        this.author = author;    }    public String getPostDate() {        return postDate;    }    public void setPostDate(String postDate) {        this.postDate = postDate;    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
2.4 Repository 类
2.5 service 层
(1)接⼝    @Override    public String toString() {        return "BookBean{" +                "id='" + id + '\''
+                ", title='" + title + '\'' +                ", author='" + author + '\'' +                ", postDate='" + postDate + '\'' +                '}';    }}
55
56
57
585960
61
62
63
64
65package cn.hadron.demo.dao;import cn.hadron.demo.bean.BookBean;import org.springframework.data.domain.Page;import org.springframework.data.domain.Pageable;import or
g.springframework.pository.ElasticsearchRepository;/** * 接⼝关系: * ElasticsearchRepository --> ElasticsearchCrudRepository --> PagingAndSortingRepository --> CrudRepository  */public interface BookRepository extends ElasticsearchRepository<BookBean, String> {    //Optional<BookBean> findById(String id);    Page<BookBean> findByAuthor(String author, Pageable pageable);    Page<BookBean> findByTitle(String title, Pageable pageable);}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
(2)实现类package cn.hadron.demo.service;import cn.hadron.demo.bean.BookBean;import org.springframework.data.domain.Page;import org.springframework.data.domain.PageRequest;import java.util.List;import java.util.Optional;public interface BookService {    Optional<BookBean> findById(String id);    BookBean save(BookBean blog);    void delete(BookBean blog);    Optional<BookBean> findOne(String id);    List<BookBean> findAll();    Page<BookBean> findByAuthor(String author, PageRequest pageRequest);    Page<BookBean> findByTitle(String title, PageRequest pageRequest);}
12345678910111213141516171819202122232425

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。