Spring Boot 集成 Solr 的详细示例
作者:搬砖牛马人
这篇文章主要介绍了Spring Boot 集成 Solr 的详细示例,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧
环境准备
- 安装 Solr :从 Solr 官网(Welcome to Apache Solr - Apache Solr)下载并安装最新版本,然后通过命令
bin/solr start启动 Solr 服务,使用bin/solr create -c mycore创建一个新的 Solr 核心。 - 安装 JDK :确保安装了 JDK 8 及以上版本。
- 配置 Maven :以 Maven 作为项目构建工具,创建 Spring Boot 项目。
添加依赖
在 Spring Boot 项目的 pom.xml 文件中添加以下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-solr</artifactId>
</dependency>此依赖会自动配置 Spring Data Solr 的相关组件,包括 Solr 客户端和 Spring Solr 支持。
配置 Solr 连接
在 application.properties 或 application.yml 文件中添加 Solr 的连接配置,示例如下:
application.properties :
spring.data.solr.host=http://localhost:8983/solr spring.data.solr.core=mycore
application.yml :
spring:
data:
solr:
host: http://localhost:8983/solr
core: mycore定义实体类
创建一个实体类用于映射 Solr 中的文档,示例如下:
package cn.juwatech.model;
import org.apache.solr.client.solrj.beans.Field;
import org.springframework.data.annotation.Id;
import org.springframework.data.solr.core.mapping.SolrDocument;
@SolrDocument(collection = "mycore")
public class Product {
@Id
@Field
private String id;
@Field
private String name;
@Field
private String description;
@Field
private double price;
// Getters and Setters
}其中,@SolrDocument(collection = "mycore") 指定了 Solr 的核心名称,@Field 注解用于将实体类的字段映射到 Solr 文档的字段。
编写 Repository 接口
创建一个继承自 SolrCrudRepository 的接口来操作 Solr 中的数据,示例如下:
package cn.juwatech.repository;
import cn.juwatech.model.Product;
import org.springframework.data.solr.repository.SolrCrudRepository;
import java.util.List;
public interface ProductRepository extends SolrCrudRepository<Product, String> {
List<Product> findByNameContaining(String name);
}通过继承 SolrCrudRepository 接口,可方便地进行文档的增删改查操作,findByNameContaining 方法可用于按名称模糊查询。
创建 Service 与 Controller
创建 Service :封装业务逻辑,示例如下:
package cn.juwatech.service;
import cn.juwatech.model.Product;
import cn.juwatech.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ProductService {
@Autowired
private ProductRepository productRepository;
public void saveProduct(Product product) {
productRepository.save(product);
}
public List<Product> searchByName(String name) {
return productRepository.findByNameContaining(name);
}
public void deleteProduct(String id) {
productRepository.deleteById(id);
}
}创建 Controller :处理 HTTP 请求,示例如下:
package cn.juwatech.controller;
import cn.juwatech.model.Product;
import cn.juwatech.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/products")
public class ProductController {
@Autowired
private ProductService productService;
@PostMapping
public void addProduct(@RequestBody Product product) {
productService.saveProduct(product);
}
@GetMapping("/search")
public List<Product> searchProducts(@RequestParam String name) {
return productService.searchByName(name);
}
@DeleteMapping("/{id}")
public void deleteProduct(@PathVariable String id) {
productService.deleteProduct(id);
}
}示例运行与测试
添加产品 :启动 Spring Boot 应用后,发送 POST 请求添加产品,如使用 curl 命令:
curl -X POST -H "Content-Type: application/json" -d '{"id":"1","name":"Laptop","description":"High performance laptop","price":1000}' http://localhost:8080/products搜索产品 :发送 GET 请求搜索产品:
curl http://localhost:8080/products/search?name=Laptop
删除产品 :发送 DELETE 请求删除产品:
curl -X DELETE http://localhost:8080/products/1
到此这篇关于Spring Boot 集成 Solr 的详细示例的文章就介绍到这了,更多相关Spring Boot 集成 Solr内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
