JAVA全系列 教程
3762个小节阅读:7089.9k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
创建Repository接口继承ElasticsearchRepository,该接口提供了文档的增删改查方法
xxxxxxxxxx
public interface ProductRepository extends ElasticsearchRepository<Product,Integer> {
}
编写测试类,注入Repository接口并测试Repository接口的增删改查方法
xxxxxxxxxx
@SpringBootTest
public class ProductRepositoryTest {
@Autowired
private ProductRepository repository;
@Test
public void addDocument(){
Product product = new Product(1, "iphone30", "iphone30是苹果最新手机");
repository.save(product);
}
@Test
public void updateDocument(){
Product product = new Product(1, "iphone31", "iphone31是苹果最新手机");
repository.save(product);
}
@Test
public void findAllDocument(){
Iterable<Product> all = repository.findAll();
for (Product product : all) {
System.out.println(product);
}
}
@Test
public void findDocumentById(){
Optional<Product> product = repository.findById(1);
System.out.println(product.get());
}
@Test
public void deleteDocument(){
repository.deleteById(1);
}
}
1. 在Spring Data ElasticSearch中,ElasticsearchRepository提供的保存方法为
A save()
B findById()
C findAll()
D insert()
2. 在Spring Data ElasticSearch中,ElasticsearchRepository提供的查询所有方法为
A save()
B findById()
C findAll()
D insert()
1=>A 2=>C