使用自定義的注解,將從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());
        }
    }

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com