class="hide-preCode-box">

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
2.3 服务层实现
在服务层中实现增删改查的业务逻辑:
package com.coderjia.boot318es.service;
import com.coderjia.boot318es.bean.Product;
import com.coderjia.boot318es.dao.ProductRepository;
import jakarta.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
/**
* @author CoderJia
* @create 2024/11/4 下午 09:29
* @Description
**/
@Service
public class ProductService {
@Resource
private ProductRepository productRepository;
// 创建或更新产品
public Product saveProduct(Product product) {
return productRepository.save(product);
}
// 根据 ID 查询产品
public Optional findById(String id) {
return productRepository.findById(id);
}
// 根据名称查询产品
public List findByName(String name) {
return productRepository.findByName(name);
}
// 获取所有产品
public Page findAll(Pageable pageable) {
return productRepository.findAll(pageable);
}
// 删除产品
public void deleteProduct(String id) {
productRepository.deleteById(id);
}
}
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
class="hide-preCode-box">
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
2.4 控制器层
在控制器层实现 REST API 接口,处理增删改查请求:
package com.coderjia.boot318es.controller;
import com.coderjia.boot318es.bean.Product;
import com.coderjia.boot318es.service.ProductService;
import jakarta.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Optional;
/**
* @author CoderJia
* @create 2024/11/4 下午 09:30
* @Description
**/
@RestController
@RequestMapping("/products")
public class ProductController {
@Resource
private ProductService productService;
// 创建或更新产品
@PostMapping
public Product createOrUpdateProduct(@RequestBody Product product) {
return productService.saveProduct(product);
}
// 根据 ID 查询产品
@GetMapping("/{id}")
public Optional getProductById(@PathVariable String id) {
return productService.findById(id);
}
// 根据名称查询产品
@GetMapping("/search")
public List searchByName(@RequestParam String name) {
return productService.findByName(name);
}
// 获取所有产品
@GetMapping
public List getAllProducts(
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size) {
Pageable pageable = PageRequest.of(page, size);
Page products = productService.findAll(pageable);
return products.getContent();
}
// 删除产品
@DeleteMapping("/{id}")
public String deleteProduct(@PathVariable String id) {
productService.deleteProduct(id);
return "Product deleted successfully!";
}
}
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
class="hide-preCode-box">
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
3. 测试应用
3.1 启动 Elasticsearch
确保 Elasticsearch 8.x 正在运行,并且可以通过 http://localhost:9200
访问。
3.2 启动 Spring Boot 应用
运行 Spring Boot 应用,确保没有错误。
3.3 测试 API
创建产品:
POST http://localhost:8080/products
Content-Type: application/json
{
"id": "1",
"name": "coderjia",
"description": "desc v1",
"price": 1.0
}
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">

更新产品:
POST http://localhost:8080/products
Content-Type: application/json
{
"id": "1",
"name": "coderjia",
"description": "desc v2",
"price": 2.0
}
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">

根据 ID 查询产品:
GET http://localhost:8080/products/1
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">

根据名称查询产品:
GET http://localhost:8080/products/search?name=coderjia
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">

获取所有产品(分页):
GET http://localhost:8080/products?page=0&size=3
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">

删除产品:
DELETE http://localhost:8080/products/1
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">

4. 总结
通过以上步骤,我们构建了一个完整的 Spring Boot 3 和 Elasticsearch 8.x 的增删改查示例应用。使用 Spring Data Elasticsearch Repository,我们能够快速实现对 Elasticsearch 的基本 CRUD 操作,简化了开发流程。希望这个示例能够帮助你理解如何在项目中有效使用 Elasticsearch!
data-report-view="{"mod":"1585297308_001","spm":"1001.2101.3001.6548","dest":"https://blog.csdn.net/m0_74825003/article/details/145512208","extend1":"pc","ab":"new"}">>
评论记录:
回复评论: