SpringBoot 2.7教程:SpringBoot + Web 项目搭建及实践应用-2022年最新图文版本
SpringBoot 2.7教程:SpringBoot + Web 项目搭建,异常捕获处理-2022年最新图文版本
SpringBoot 2.7教程:SpringBoot + Mysql 项目应用-2022年最新图文版本
SpringBoot 2.7教程:SpringBoot 集成 jsp 页面开发 -2022年最新图文版本
SpringBoot 2.7教程:SpringBoot 实现文件上传,图片上传并显示功能-2022年最新图文版本
SpringBoot 2.7教程:springboot 设置全局字符编码,解决乱码问题-2022年最新图文版
SpringBoot 2.7教程:SpringBoot mybatis 多数据源的整合方法-2022年最新图文版本
SpringBoot 2.7教程:SpringBoot 整合 RocketMQ 项目搭建-2022年最新图文版本
SpringBoot 2.7教程:SpringBoot 整合 RabbitMQ 项目搭建-2022年最新图文版本
SpringBoot 2.7教程:SpringBoot 整合 MongoDB 项目搭建-2022年最新图文版本
SpringBoot 2.7教程:SpringBoot 整合 Redisson 项目搭建-2022年最新图文版本
SpringBoot 2.7教程:SpringBoot 整合 Redis 项目搭建-2022年最新图文版本
目录
一、创建项目elasticsearch-sample,项目结构如下
二、配置pom文件
三、配置application文件
四、代码展示
org.springframework.boot
spring-boot-starter-data-elasticsearch
org.springframework.boot
spring-boot-starter-web
org.projectlombok
lombok
1.18.24
spring:
data:
elasticsearch:
repositories:
enabled: true
elasticsearch:
rest:
uris: [elasticsearch_host:9200]
http.cors.enabled: true
http.cors.allow-origin: "*"
public interface BookService {
Optional findById(String id);
BookBean save(BookBean book);
void delete(BookBean book);
Optional findOne(String id);
List findAll();
Page findByAuthor(String author, PageRequest pageRequest);
Page findByTitle(String title, PageRequest pageRequest);
}
@Service("bookService")
public class BookServiceImpl implements BookService {
@Autowired
@Qualifier("bookRepository")
private BookRepository bookRepository;
@Override
public Optional findById(String id) {
//CrudRepository中的方法
return bookRepository.findById(id);
}
@Override
public BookBean save(BookBean book) {
return bookRepository.save(book);
}
@Override
public void delete(BookBean book) {
bookRepository.delete(book);
}
@Override
public Optional findOne(String id) {
return bookRepository.findById(id);
}
@Override
public List findAll() {
return (List) bookRepository.findAll();
}
@Override
public Page findByAuthor(String author, PageRequest pageRequest) {
return bookRepository.findByAuthor(author, pageRequest);
}
@Override
public Page findByTitle(String title, PageRequest pageRequest) {
return bookRepository.findByTitle(title, pageRequest);
}
}
/**
* 接口关系:
* ElasticsearchRepository --> ElasticsearchCrudRepository --> PagingAndSortingRepository --> CrudRepository
*/
public interface BookRepository extends ElasticsearchRepository {
Optional findById(String id);
Page findByAuthor(String author, Pageable pageable);
Page findByTitle(String title, Pageable pageable);
List findByTitleOrAuthor(String title, String author);
@Highlight(fields = {
@HighlightField(name = "title"),
@HighlightField(name = "author")
})
@Query("{\"match\":{\"title\":\"?0\"}}")
SearchHits find(String keyword);
}
@Document(indexName = "book")
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;
}
@Override
public String toString() {
return "BookBean{" +
"id='" + id + '\'' +
", title='" + title + '\'' +
", author='" + author + '\'' +
", postDate='" + postDate + '\'' +
'}';
}
}
/**
* @author wx:深入技术架构
*/
@Slf4j
@RestController
public class BookController {
@Autowired
private BookService bookService;
@RequestMapping("/book/get/{id}")
@ResponseBody
public BookBean getBookById(@PathVariable String id){
Optional opt =bookService.findById(id);
log.info(opt.toString());
BookBean book=opt.get();
return book;
}
@RequestMapping("/book/save")
@ResponseBody
public void bookSave(){
BookBean book=new BookBean("1","ES 入门教程","张三","2022-08-05");
System.out.println(book);
bookService.save(book);
}
@RequestMapping("/book/edit")
@ResponseBody
public void bookEdit(){
BookBean book=new BookBean("1","ES 高级教程","李四","2022-08-05");
System.out.println(book);
bookService.save(book);
}
}
http://localhost:8080/book/edit
http://localhost:8080/book/get/1