一 .明确自己的任务
把商品数据导入到solr引擎的索引库里
导入依赖
solr
dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>4.10.3</version>
</dependency>
二.确定自己的目标搜索productpojo
private String product_id;
private String product_name;
private String product_img;
private long product_price;
private String product_discription;
private String product_type;
private String product_status;
private String mast;
三.写productmapper.xml
<mapper namespace="com.bigotry.dao.ProductSearchMapper">
<select id="findproduct" resultType="com.bigotry.pojo.ProductSearch">
select * from bk_product where product_status=1
</select>
</mapper>
四.写productdao数据交互层
public interface ProductSearchMapper {
List<ProductSearch> findproduct();
}
五.写productservice服务层,进行数据添加到索引库
SolrServer solrServer=new HttpSolrServer("http://192.168.0.128:8080/solr/collection1");
try {
//1、先查询所有商品数据
List<ProductSearch> searchItems1= this.productSearchMapper.findproduct();
//2、遍历商品数据添加到索引库
for (ProductSearch productSearch : searchItems1) {
//创建文档对象
SolrInputDocument document = new SolrInputDocument();
//向文档中添加域
document.addField("id", productSearch.getProduct_id());
document.addField("product_name", productSearch.getProduct_name());
document.addField("product_img", productSearch.getProduct_img());
document.addField("product_price", productSearch.getProduct_price());
document.addField("product_discription", productSearch.getProduct_discription());
document.addField("product_type",productSearch.getProduct_type());
document.addField("product_status", productSearch.getProduct_status());
document.addField("product_number",productSearch.getProduct_number());
document.addField("product_sale", productSearch.getProduct_sale());
//把文档写入索引库
solrServer.add(document);
}
//3、提交
solrServer.commit();
System.out.println("success");
a=1;
} catch (Exception e) {
e.printStackTrace();
}
五.数据导入控制层controller