开发工具:idea
elasticsearch版本:6.7.0
项目整体架构:
依赖pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cxh</groupId>
<artifactId>springboot_elasticsearch</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot_elasticsearch</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- DevTools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!-- Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Thymeleaf -->
<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>
<!-- Fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<!-- Commons-Lang3工具包 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
注意:springboot跟elasticsearch存在版本依赖关系。
配置application.properties:
# ELASTICSEARCH (ElasticsearchProperties)
# Elasticsearch cluster name.
spring.data.elasticsearch.cluster-name=my-application
# Comma-separated list of cluster node addresses.
spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300
# Whether to enable Elasticsearch repositories.
spring.data.elasticsearch.repositories.enabled=true
注意:cluster-name要跟elasticsearch安装包下的elasticsearch.yml中的配置一样:
entity层:
package com.cxh.springboot_elasticsearch.entity;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
@Document(indexName = "es", type = "book")
@Data
public class Book {
@Id
private String id;
private String title;
private String author;
private String postDate;
public Book(){}
public Book(String id, String title, String author, String postDate){
this.id=id;
this.title=title;
this.author=author;
this.postDate=postDate;
}
@Override
public String toString() {
return "Book{" +
"id='" + id + '\'' +
", title='" + title + '\'' +
", author='" + author + '\'' +
", postDate='" + postDate + '\'' +
'}';
}
}
dao层:
package com.cxh.springboot_elasticsearch.dao;
import com.cxh.springboot_elasticsearch.entity.Book;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.Optional;
/**
* 接口关系:
* ElasticsearchRepository --> ElasticsearchCrudRepository --> PagingAndSortingRepository --> CrudRepository
*/
public interface BookRepository extends ElasticsearchRepository<Book, String> {
}
service层:
package com.cxh.springboot_elasticsearch.service;
import com.cxh.springboot_elasticsearch.entity.Book;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import java.util.List;
import java.util.Optional;
public interface BookService {
Optional<Book> findById(String id);
Book save(Book book);
void delete(Book book);
List<Book> findAll();
void delete(String id);
}
接口实现类:
package com.cxh.springboot_elasticsearch.service;
import com.cxh.springboot_elasticsearch.dao.BookRepository;
import com.cxh.springboot_elasticsearch.entity.Book;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@Service("bookService")
public class BookServiceImpl implements BookService {
@Autowired
@Qualifier("bookRepository")
private BookRepository bookRepository;
@Override
public Optional<Book> findById(String id) {
return bookRepository.findById(id);
}
@Override
public Book save(Book blog) {
return bookRepository.save(blog);
}
@Override
public void delete(Book blog) {
bookRepository.delete(blog);
}
@Override
public List<Book> findAll() {
Iterable<Book> books = bookRepository.findAll();
List<Book> bookList = new ArrayList<>();
books.forEach(it -> bookList.add(it));
return bookList;
}
@Override
public void delete(String id) {
bookRepository.deleteById(id);
}
}
controller层:
package com.cxh.springboot_elasticsearch.controller;
import com.cxh.springboot_elasticsearch.entity.Book;
import com.cxh.springboot_elasticsearch.service.BookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Optional;
@RestController
public class ElasticController {
@Autowired
private BookService bookService;
@RequestMapping("/book/{id}")
public Book getBookById(@PathVariable String id){
Optional<Book> opt =bookService.findById(id);
Book book=opt.get();
System.out.println(book);
return book;
}
@RequestMapping("/insert")
public String insert(){
String [] books = {"java","实战java","java进阶","c","c++"};
String [] authors = {"小明","小明","小明","小华","小朱"};
for(int i = 0,len = books.length; i < len; i++){
Book book = new Book(i+1 + "",books[i],authors[i],"2020-02-16");
bookService.save(book);
System.out.println(book);
}
return "操作成功";
}
@RequestMapping("/update/{id}")
public String update(@PathVariable String id){
Book book=new Book(id,"ES入门教程","小华","2020-02-16");
bookService.save(book);
System.out.println(book);
return "操作成功";
}
@RequestMapping("/delete/{id}")
public String delete(@PathVariable String id){
bookService.delete(id);
return "操作成功";
}
@RequestMapping("/findAll")
public List<Book> findAll(){
return bookService.findAll();
}
}
浏览器打开地址:
http://localhost:8080/insert(新增数据)
http://localhost:8080/findAll(查询数据)