您现在的位置是:主页 > news > 重庆市卫生厅网站 查询前置审批/seo优化一般包括
重庆市卫生厅网站 查询前置审批/seo优化一般包括
admin2025/6/29 3:44:28【news】
简介重庆市卫生厅网站 查询前置审批,seo优化一般包括,wordpress auto save,天津营销网站建设公司排名1. 注解 先写一个注解, 加一个参数value ,用来指定这个字段对应的solr的索引名 在bean的字段上使用该注解 package com.zgd.anno;import java.lang.annotation.*;/*** 在字段上使用该注解,并标注对应的索引域,* 配合Doc2BeanUtils即可将查询到的Document对象转换成bean*/ D…
重庆市卫生厅网站 查询前置审批,seo优化一般包括,wordpress auto save,天津营销网站建设公司排名1. 注解
先写一个注解, 加一个参数value ,用来指定这个字段对应的solr的索引名 在bean的字段上使用该注解
package com.zgd.anno;import java.lang.annotation.*;/*** 在字段上使用该注解,并标注对应的索引域,* 配合Doc2BeanUtils即可将查询到的Document对象转换成bean*/
D…
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;}@Overridepublic 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;@Beforepublic void init(){/*指定solr的地址*/String baseURL = "http://localhost:18080/solr/collection1";httpSolrClient = new HttpSolrClient(baseURL);}@Testpublic 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());}}