Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The configuration method of spring+springdata+elasticsearch

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "spring+springdata+elasticsearch configuration method", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "spring+springdata+elasticsearch configuration method" bar!

Pom configuration:

Org.springframework.data spring-data-elasticsearch 3.1.0.RELEASE

Es profile:

Entity Book.java, es index is automatically created by default when the project starts

Import lombok.Data;import org.springframework.data.annotation.Id;import org.springframework.data.annotation.Version;import org.springframework.data.elasticsearch.annotations.Document;import org.springframework.data.elasticsearch.annotations.Field;import org.springframework.data.elasticsearch.annotations.FieldType;@Data@Document (indexName= "book", type= "book", shards = 3, replicas = 1) public class Book {@ Id private String id; @ Field (type= FieldType.Keyword) private String name; @ Field private Long price @ Version private Long version;}

ElasticsearchCrudRepository.java

Import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;import org.springframework.data.repository.NoRepositoryBean;import org.springframework.data.repository.PagingAndSortingRepository;import java.io.Serializable;@NoRepositoryBeanpublic interface ElasticsearchCrudRepository extends ElasticsearchRepository, PagingAndSortingRepository {}

BookRepository.java

@ Repositorypublic interface BookRepository extends ElasticsearchCrudRepository {List findBookByName (String name);}

Test class

@ Controller@RequestMapping ("es") public class EsController {@ Autowired private BookService bookService; @ RequestMapping ("book/ {name}") public CommonResponse bookSearch (@ PathVariable String name) {CommonResponse response = new CommonResponse (); List list = bookService.findBookByName (name); response.setData (list); return response;}}

Pay attention to the compatibility of spring and es versions when configuring. You can see the official document https://github.com/spring-projects/spring-data-elasticsearch.

Thank you for your reading, the above is the content of "the configuration method of spring+springdata+elasticsearch", after the study of this article, I believe you have a deeper understanding of the configuration method of spring+springdata+elasticsearch, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report