依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
使用
和JPA用法一样
BookRepository
public interface BookRepository extends ElasticsearchRepository<Book,String> {
}
BookService
public interface BookService {
Book findById(String id);
boolean saveBook(Book book);
boolean updateBook(Book book);
boolean deleteBookById(String id);
}
BookServiceImpl
@Service("bookService")
public class BookServiceImpl implements BookService {
@Autowired
BookRepository bookRepository;
@Override
public Book findById(String id) {
Optional<Book> result = bookRepository.findById(id);
return result.get();
}
@Override
public boolean saveBook(Book book) {
bookRepository.save(book);
return true;
}
@Override
public boolean updateBook(Book book) {
bookRepository.save(book);
return true;
}
@Override
public boolean deleteBookById(String id) {
bookRepository.deleteById(id);
return true;
}