欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Boot 集成 Solr 的詳細示例

 更新時間:2025年05月20日 11:20:44   作者:搬磚牛馬人  
這篇文章主要介紹了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.propertiesapplication.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)文章

最新評論