Spring Boot 集成 Solr 的詳細示例
環(huán)境準備
- 安裝 Solr :從 Solr 官網(wǎng)(Welcome to Apache Solr - Apache Solr)下載并安裝最新版本,然后通過命令
bin/solr start
啟動 Solr 服務(wù),使用bin/solr create -c mycore
創(chuàng)建一個新的 Solr 核心。 - 安裝 JDK :確保安裝了 JDK 8 及以上版本。
- 配置 Maven :以 Maven 作為項目構(gòu)建工具,創(chuàng)建 Spring Boot 項目。
添加依賴
在 Spring Boot 項目的 pom.xml
文件中添加以下依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-solr</artifactId> </dependency>
此依賴會自動配置 Spring Data Solr 的相關(guān)組件,包括 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
定義實體類
創(chuàng)建一個實體類用于映射 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 接口
創(chuàng)建一個繼承自 SolrCrudRepository
的接口來操作 Solr 中的數(shù)據(jù),示例如下:
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
方法可用于按名稱模糊查詢。
創(chuàng)建 Service 與 Controller
創(chuàng)建 Service :封裝業(yè)務(wù)邏輯,示例如下:
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); } }
創(chuàng)建 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); } }
示例運行與測試
添加產(chǎn)品 :啟動 Spring Boot 應(yīng)用后,發(fā)送 POST 請求添加產(chǎn)品,如使用 curl 命令:
curl -X POST -H "Content-Type: application/json" -d '{"id":"1","name":"Laptop","description":"High performance laptop","price":1000}' http://localhost:8080/products
搜索產(chǎn)品 :發(fā)送 GET 請求搜索產(chǎn)品:
curl http://localhost:8080/products/search?name=Laptop
刪除產(chǎn)品 :發(fā)送 DELETE 請求刪除產(chǎn)品:
curl -X DELETE http://localhost:8080/products/1
到此這篇關(guān)于Spring Boot 集成 Solr 的詳細示例的文章就介紹到這了,更多相關(guān)Spring Boot 集成 Solr內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
EasyExcel工具讀取Excel空數(shù)據(jù)行問題的解決辦法
EasyExcel是阿里巴巴開源的一個excel處理框架,以使用簡單,節(jié)省內(nèi)存著稱,下面這篇文章主要給大家介紹了關(guān)于EasyExcel工具讀取Excel空數(shù)據(jù)行問題的解決辦法,需要的朋友可以參考下2022-08-08一文詳解SpringBoot使用Kafka如何保證消息不丟失
這篇文章主要為大家詳細介紹了SpringBoot使用Kafka如何保證消息不丟失的相關(guān)知識,文中的示例代碼講解詳細,有需要的小伙伴可以參考一下2025-01-01JAVA時間戳-Calendar類使用(包括set,get,add方法)
這篇文章主要介紹了JAVA時間戳-Calendar類使用(包括set,get,add方法),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-04-04java編程SpringSecurity入門原理及應(yīng)用簡介
Spring 是非常流行和成功的 Java 應(yīng)用開發(fā)框架,Spring Security 正是 Spring 家族中的成員。Spring Security 基于 Spring 框架,提供了一套 Web 應(yīng)用安全性的完整解決方案2021-09-09Java多線程并發(fā)JUC包ReentrantLock顯示鎖的用法
這篇文章主要介紹了Java多線程并發(fā)JUC包ReentrantLock顯示鎖的用法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-05-05Spring實現(xiàn)動態(tài)數(shù)據(jù)源切換的方法總結(jié)
這篇文章主要為大家詳細介紹了一種Spring實現(xiàn)動態(tài)數(shù)據(jù)源切換的方法,文中的示例代碼講解詳細,具有一定的學(xué)習(xí)價值,感興趣的小伙伴可以跟隨小編一起了解一下2023-06-06