使用自定义的注解,将从solr索引库查询得到的文档对象封装成bean

版权声明:转载请标明出处~~ https://blog.csdn.net/zzzgd_666/article/details/80692576

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());
        }
    }
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页