1. 程式人生 > >使用自定義的註解,將從solr索引庫查詢得到的文件物件封裝成bean

使用自定義的註解,將從solr索引庫查詢得到的文件物件封裝成bean

1. 註解

先寫一個註解, 加一個引數value ,用來指定這個欄位對應的solr的索引名
在bean的欄位上使用該註解

package com.zgd.anno;

import java.lang.annotation.*;

/**
 * 在欄位上使用該註解,並標註對應的索引域,
 *  配合Doc2BeanUtils即可將查詢到的Document物件轉換成bean
 */
@Documented
@Target(ElementType.FIELD) //作用範圍: 欄位
@Retention(RetentionPolicy.RUNTIME)  // 作用時間: 執行時
public @interface
SolrMapping {
String value() default ""; //solr索引名 }

2. util工具類

package com.zgd.utils;

import com.zgd.anno.SolrMapping;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.solr.common.SolrDocument;

import java.lang.reflect.Field;

public class Doc2BeanUtil {

      public static
<T> T getBean(Class<T> beanClass, SolrDocument document) { try { //獲取例項物件 Object bean = beanClass.newInstance(); // 反射獲得所有欄位 Field[] fields = beanClass.getDeclaredFields(); // 遍歷bean中的欄位 for (Field field : fields) { SolrMapping anno = field.getAnnotation(SolrMapping.class); if
(anno != null) { String filedName = field.getName(); //獲得註解中標記的對應的索引域 String indexName = anno.value(); Object val = document.get(indexName); if("".equals(val)){ //如果註解不指定solr的索引,預設用欄位名來作為索引名 var = fildeName; } // 判斷欄位的型別 BeanUtils.setProperty(bean,filedName,val); } } return (T) bean; } catch (Exception e) { e.printStackTrace(); } return null; } }

3. 使用方法

  • 將自定義的註解@SolrMapping在bean物件的欄位上標明,使欄位和索引庫中的域產生對映關係
  • 使用Doc2BeanUtils對結果集進行封裝

Product的一個Bean物件

package com.zgd.bean;
import com.zgd.anno.SolrMapping;

import javax.naming.Name;
import java.io.Serializable;

public class Product {
    @SolrMapping(value = "id")
    private Integer pid;
    @SolrMapping(value = "product_name")
    private String name;
    @SolrMapping(value = "product_price")
    private Double price;
    @SolrMapping(value = "product_picture")
    private String picture;
    @SolrMapping(value = "product_catalog")
    private Integer catalog;
    @SolrMapping(value = "product_catalog_name")
    private String catalog_name;

    public Integer getPid() {
        return pid;
    }

    public void setPid(Integer pid) {
        this.pid = pid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public String getPicture() {
        return picture;
    }

    public void setPicture(String picture) {
        this.picture = picture;
    }

    public Integer getCatalog() {
        return catalog;
    }

    public void setCatalog(Integer catalog) {
        this.catalog = catalog;
    }

    public String getCatalog_name() {
        return catalog_name;
    }

    public void setCatalog_name(String catalog_name) {
        this.catalog_name = catalog_name;
    }

    @Override
    public String toString() {
        return "Product{" +
                "pid=" + pid +
                ", name='" + name + '\'' +
                ", price=" + price +
                ", picture='" + picture + '\'' +
                ", catalog=" + catalog +
                ", catalog_name='" + catalog_name + '\'' +
                '}';
    }
}

使用工具方法:

public class SolrTest {

    /*需要httpSolrClient物件來對索引庫進行操作*/
    private HttpSolrClient httpSolrClient;

    @Before
    public void init(){
        /*指定solr的地址*/
        String baseURL = "http://localhost:18080/solr/collection1";
        httpSolrClient = new HttpSolrClient(baseURL);
    }

    @Test
    public void query() throws IOException, SolrServerException {
        //建立Query查詢
        SolrQuery solrQuery = new SolrQuery("product_name:果凍");
        //執行查詢
        QueryResponse response = httpSolrClient.query(solrQuery);
        //獲取SolrDocumentList物件
        SolrDocumentList results = response.getResults();

        //總記錄數
        long total = results.getNumFound();
        System.out.println("total = " + total);

        //結果集
        for (SolrDocument result : results) {
            Product bean = Doc2BeanUtil.getBean(Product.class, result);
            System.out.println(bean.getName());
        }
    }