xl_echo编辑整理,欢迎转载,转载请声明文章来源。更多IT、编程案例、资料请联系QQ:1280023003
百战不败,依不自称常胜,百败不颓,依能奋力前行。——这才是真正的堪称强大!!
虽然支持任何编程语言的能力具有很大的市场价值,你可能感兴趣的问题是:我如何将Solr 的应用集成到 Spring 中?可以,Spring Data Solr 就是为了方便 Solr 的开发所研制的一个框架,其底层是对 SolrJ(官方 API)的封装。
第一步:搭建一个maven工程,使用jar
第二步:在pom.xml文件中引入依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.echo.solrgroupId>
<artifactId>springdatasolrDemoartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>springdatasolrDemoname>
<dependencies>
<dependency>
<groupId>org.springframework.datagroupId>
<artifactId>spring-data-solrartifactId>
<version>1.5.5.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>4.2.4.RELEASEversion>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.9version>
dependency>
dependencies>
project>
第三步:在classpath路径下编一个applicationContext-solr.xml文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:solr="http://www.springframework.org/schema/data/solr"
xsi:schemaLocation="http://www.springframework.org/schema/data/solr
http://www.springframework.org/schema/data/solr/spring-solr-1.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<solr:solr-server id="solrServer" url="http://127.0.0.1:8080/solr" />
<bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
<constructor-arg ref="solrServer" />
bean>
beans>
准备工作做好啦,解析来就可以编写开发程序了,注意:开发需要用到一个特殊的注解@Field。变量上面标注该注解表示这个属性对应域名对应属性名,如果字段和域名不一致,后面添加一个名字即可,如:@Field("item_title") private String title;
第四步:编写一个实体类,并写上相应的注解
package com.echo.pojo;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import org.apache.solr.client.solrj.beans.Field;
public class TbItem implements Serializable{
@Field
private Long id;
@Field("item_title")
private String title;
private String sellPoint;
@Field("item_price")
private BigDecimal price;
private Integer stockCount;
private Integer num;
private String barcode;
@Field("item_image")
private String image;
private Long categoryid;
private String status;
private Date createTime;
private Date updateTime;
private String itemSn;
private BigDecimal costPirce;
private BigDecimal marketPrice;
private String isDefault;
@Field("item_goodsId")
private Long goodsId;
private String sellerId;
private String cartThumbnail;
@Field("item_category")
private String category;
@Field("item_brand")
private String brand;
private String spec;
@Field("item_seller")
private String seller;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title == null ? null : title.trim();
}
public String getSellPoint() {
return sellPoint;
}
public void setSellPoint(String sellPoint) {
this.sellPoint = sellPoint == null ? null : sellPoint.trim();
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public Integer getStockCount() {
return stockCount;
}
public void setStockCount(Integer stockCount) {
this.stockCount = stockCount;
}
public Integer getNum() {
return num;
}
public void setNum(Integer num) {
this.num = num;
}
public String getBarcode() {
return barcode;
}
public void setBarcode(String barcode) {
this.barcode = barcode == null ? null : barcode.trim();
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image == null ? null : image.trim();
}
public Long getCategoryid() {
return categoryid;
}
public void setCategoryid(Long categoryid) {
this.categoryid = categoryid;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status == null ? null : status.trim();
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public String getItemSn() {
return itemSn;
}
public void setItemSn(String itemSn) {
this.itemSn = itemSn == null ? null : itemSn.trim();
}
public BigDecimal getCostPirce() {
return costPirce;
}
public void setCostPirce(BigDecimal costPirce) {
this.costPirce = costPirce;
}
public BigDecimal getMarketPrice() {
return marketPrice;
}
public void setMarketPrice(BigDecimal marketPrice) {
this.marketPrice = marketPrice;
}
public String getIsDefault() {
return isDefault;
}
public void setIsDefault(String isDefault) {
this.isDefault = isDefault == null ? null : isDefault.trim();
}
public Long getGoodsId() {
return goodsId;
}
public void setGoodsId(Long goodsId) {
this.goodsId = goodsId;
}
public String getSellerId() {
return sellerId;
}
public void setSellerId(String sellerId) {
this.sellerId = sellerId == null ? null : sellerId.trim();
}
public String getCartThumbnail() {
return cartThumbnail;
}
public void setCartThumbnail(String cartThumbnail) {
this.cartThumbnail = cartThumbnail == null ? null : cartThumbnail.trim();
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category == null ? null : category.trim();
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand == null ? null : brand.trim();
}
public String getSpec() {
return spec;
}
public void setSpec(String spec) {
this.spec = spec == null ? null : spec.trim();
}
public String getSeller() {
return seller;
}
public void setSeller(String seller) {
this.seller = seller == null ? null : seller.trim();
}
}
第五步:创建一个测试类往solr操作数据
package test;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.core.query.Criteria;
import org.springframework.data.solr.core.query.Query;
import org.springframework.data.solr.core.query.SimpleQuery;
import org.springframework.data.solr.core.query.result.ScoredPage;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.echo.pojo.TbItem;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext-solr.xml")
public class TestAdd {
@Autowired
private SolrTemplate solrTemplate;
/**
* 添加一个对象
*/
@Test
public void testAdd() {
TbItem item = new TbItem();
item.setId(1L);
item.setBrand("超级手机");
item.setCategory("手机");
item.setGoodsId(1L);
item.setSeller("超级手机国际专卖店");
item.setTitle("超级手机第1代");
item.setPrice(new BigDecimal(2000000));
solrTemplate.saveBean(item);
solrTemplate.commit();
}
/**
* 根据ID查询一个对象
*/
@Test
public void testFindById() {
TbItem item = solrTemplate.getById(1L, TbItem.class);
System.out.println(item.getTitle());
}
/**
* 根据ID删除一个对象
*/
@Test
public void testDelById() {
solrTemplate.deleteById("1");
solrTemplate.commit();
}
/**
* 批量插入数据,插入一个List集合
*/
@Test
public void testAddList() {
List list = new ArrayList();
for (int i = 0; i < 10; i++) {
TbItem item = new TbItem();
item.setId(i + 1L);
item.setBrand("超级手机" + i);
item.setCategory("手机");
item.setGoodsId(1L);
item.setSeller("超级手机国际专卖店" + i);
item.setTitle("超级手机第1代" + i);
item.setPrice(new BigDecimal(20000 + i));
list.add(item);
}
solrTemplate.saveBeans(list);
solrTemplate.commit();
}
/**
* 分页查询
*/
@Test
public void testPageQuery(){
Query query = new SimpleQuery("*:*");//查询所有数据
query.setOffset(0);//开始索引(默认0)
query.setRows(3);//每页记录数(默认10)
ScoredPage page = solrTemplate.queryForPage(query, TbItem.class);
System.out.println("总记录数:" + page.getTotalElements());
List list = page.getContent();
for(TbItem item : list){
System.out.println(item.getTitle());
}
}
/**
* 根据条件查询
*/
@Test
public void testCriteriaPageQuery(){
Query query = new SimpleQuery("*:*");
//Criteria criteria = new Criteria("item_title").contains("2");//标题中包含2的记录
Criteria criteria = new Criteria("item_category").contains("手机");//分类中包含手机的
query.addCriteria(criteria);
ScoredPage page = solrTemplate.queryForPage(query, TbItem.class);
System.out.println("总记录数:" + page.getTotalElements());
List list = page.getContent();
for(TbItem item : list){
System.out.println(item.getTitle() + item.getPrice());
}
}
/**
* 删除全部记录(条件删除和条件查询一样,主要在于构建条件)
*/
@Test
public void testDelAll(){
Query query = new SimpleQuery("*:*");
solrTemplate.delete(query);
solrTemplate.commit();
}
}
执行程序并访问地址http://localhost:8080/solr/,如果出现以上存入的数据代表成功
总结:其实比较简单,主要是导入配置文件,注入Bean,然后使用solr