开发工具:idea
elasticsearch版本:6.7.0
项目整体架构:
依赖pom.xml:
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.2.4.RELEASE
com.cxh
springboot_elasticsearch
0.0.1-SNAPSHOT
springboot_elasticsearch
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-devtools
true
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-data-elasticsearch
com.alibaba
fastjson
1.2.47
org.apache.commons
commons-lang3
${commons-lang3.version}
org.projectlombok
lombok
provided
org.springframework.boot
spring-boot-maven-plugin
注意: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 {
}
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 findById(String id);
Book save(Book book);
void delete(Book book);
List 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 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 findAll() {
Iterable books = bookRepository.findAll();
List 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 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 findAll(){
return bookService.findAll();
}
}
浏览器打开地址:
http://localhost:8080/insert(新增数据)
http://localhost:8080/findAll(查询数据)