如何将Solr的应用集成到Spring中?
SpringDataSolr就是为了方便Solr的开发所研制的一个框架,其底层是对SolrJ的封装.
SpringDataSolr入门小Demo
首先目录结构为:
搭建工程,需要引入的依赖为:
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-solr</artifactId>
<version>1.5.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- java编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</build>
如果jar包不完全引入,就会出现报错的情况的.
然后在Resource下创建的配置文件是:
<?xml version="1.0" encoding="UTF-8"?>
<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:solr-server id="solrServer" url="http://192.168.200.128:8080/solr" />
<!-- solr模板,使用solr模板可对索引库进行CRUD的操作 -->
<bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
<constructor-arg ref="solrServer" />
</bean>
</beans>
http://192.168.200.128:8080 这是我在虚拟机中配置的tomcat,然后在虚拟机中配置的solr 如果属性和配置文件定义的域名不一致的时候,需要在注解中指定域名称.
这是我配置的pojo类
import org.apache.solr.client.solrj.beans.Field; import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date; public class Item implements Serializable {
@Field
private Long id; @Field("item_title")
private String title; @Field("item_price")
private BigDecimal price; @Field("item_image")
private String image; @Field("item_goodsid")
private Long goodsId; @Field("item_category")
private String category; @Field("item_brand")
private String brand; @Field("item_seller")
private String seller; /**
* 商品id,同时也是商品编号
*/ /**
* 商品标题
*/ /**
* 商品卖点
*/
private String sellPoint; /**
* 商品价格,单位为:元
*/ private Integer stockCount; /**
* 库存数量
*/
private Integer num; /**
* 商品条形码
*/
private String barcode; /**
* 商品图片
*/ /**
* 所属类目,叶子类目
*/
private Long categoryid; /**
* 商品状态,1-正常,2-下架,3-删除
*/
private String status; /**
* 创建时间
*/
private Date createTime; /**
* 更新时间
*/
@Field("item_updatetime")
private Date updateTime; private String itemSn; private BigDecimal costPirce; private BigDecimal marketPrice; private String isDefault; private String sellerId; private String cartThumbnail; private String spec; private static final long serialVersionUID = 1L; 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();
} @Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", id=").append(id);
sb.append(", title=").append(title);
sb.append(", sellPoint=").append(sellPoint);
sb.append(", price=").append(price);
sb.append(", stockCount=").append(stockCount);
sb.append(", num=").append(num);
sb.append(", barcode=").append(barcode);
sb.append(", image=").append(image);
sb.append(", categoryid=").append(categoryid);
sb.append(", status=").append(status);
sb.append(", createTime=").append(createTime);
sb.append(", updateTime=").append(updateTime);
sb.append(", itemSn=").append(itemSn);
sb.append(", costPirce=").append(costPirce);
sb.append(", marketPrice=").append(marketPrice);
sb.append(", isDefault=").append(isDefault);
sb.append(", goodsId=").append(goodsId);
sb.append(", sellerId=").append(sellerId);
sb.append(", cartThumbnail=").append(cartThumbnail);
sb.append(", category=").append(category);
sb.append(", brand=").append(brand);
sb.append(", spec=").append(spec);
sb.append(", seller=").append(seller);
sb.append(", serialVersionUID=").append(serialVersionUID);
sb.append("]");
return sb.toString();
} @Override
public boolean equals(Object that) {
if (this == that) {
return true;
}
if (that == null) {
return false;
}
if (getClass() != that.getClass()) {
return false;
}
Item other = (Item) that;
return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
&& (this.getTitle() == null ? other.getTitle() == null : this.getTitle().equals(other.getTitle()))
&& (this.getSellPoint() == null ? other.getSellPoint() == null : this.getSellPoint().equals(other.getSellPoint()))
&& (this.getPrice() == null ? other.getPrice() == null : this.getPrice().equals(other.getPrice()))
&& (this.getStockCount() == null ? other.getStockCount() == null : this.getStockCount().equals(other.getStockCount()))
&& (this.getNum() == null ? other.getNum() == null : this.getNum().equals(other.getNum()))
&& (this.getBarcode() == null ? other.getBarcode() == null : this.getBarcode().equals(other.getBarcode()))
&& (this.getImage() == null ? other.getImage() == null : this.getImage().equals(other.getImage()))
&& (this.getCategoryid() == null ? other.getCategoryid() == null : this.getCategoryid().equals(other.getCategoryid()))
&& (this.getStatus() == null ? other.getStatus() == null : this.getStatus().equals(other.getStatus()))
&& (this.getCreateTime() == null ? other.getCreateTime() == null : this.getCreateTime().equals(other.getCreateTime()))
&& (this.getUpdateTime() == null ? other.getUpdateTime() == null : this.getUpdateTime().equals(other.getUpdateTime()))
&& (this.getItemSn() == null ? other.getItemSn() == null : this.getItemSn().equals(other.getItemSn()))
&& (this.getCostPirce() == null ? other.getCostPirce() == null : this.getCostPirce().equals(other.getCostPirce()))
&& (this.getMarketPrice() == null ? other.getMarketPrice() == null : this.getMarketPrice().equals(other.getMarketPrice()))
&& (this.getIsDefault() == null ? other.getIsDefault() == null : this.getIsDefault().equals(other.getIsDefault()))
&& (this.getGoodsId() == null ? other.getGoodsId() == null : this.getGoodsId().equals(other.getGoodsId()))
&& (this.getSellerId() == null ? other.getSellerId() == null : this.getSellerId().equals(other.getSellerId()))
&& (this.getCartThumbnail() == null ? other.getCartThumbnail() == null : this.getCartThumbnail().equals(other.getCartThumbnail()))
&& (this.getCategory() == null ? other.getCategory() == null : this.getCategory().equals(other.getCategory()))
&& (this.getBrand() == null ? other.getBrand() == null : this.getBrand().equals(other.getBrand()))
&& (this.getSpec() == null ? other.getSpec() == null : this.getSpec().equals(other.getSpec()))
&& (this.getSeller() == null ? other.getSeller() == null : this.getSeller().equals(other.getSeller()));
} @Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
result = prime * result + ((getTitle() == null) ? 0 : getTitle().hashCode());
result = prime * result + ((getSellPoint() == null) ? 0 : getSellPoint().hashCode());
result = prime * result + ((getPrice() == null) ? 0 : getPrice().hashCode());
result = prime * result + ((getStockCount() == null) ? 0 : getStockCount().hashCode());
result = prime * result + ((getNum() == null) ? 0 : getNum().hashCode());
result = prime * result + ((getBarcode() == null) ? 0 : getBarcode().hashCode());
result = prime * result + ((getImage() == null) ? 0 : getImage().hashCode());
result = prime * result + ((getCategoryid() == null) ? 0 : getCategoryid().hashCode());
result = prime * result + ((getStatus() == null) ? 0 : getStatus().hashCode());
result = prime * result + ((getCreateTime() == null) ? 0 : getCreateTime().hashCode());
result = prime * result + ((getUpdateTime() == null) ? 0 : getUpdateTime().hashCode());
result = prime * result + ((getItemSn() == null) ? 0 : getItemSn().hashCode());
result = prime * result + ((getCostPirce() == null) ? 0 : getCostPirce().hashCode());
result = prime * result + ((getMarketPrice() == null) ? 0 : getMarketPrice().hashCode());
result = prime * result + ((getIsDefault() == null) ? 0 : getIsDefault().hashCode());
result = prime * result + ((getGoodsId() == null) ? 0 : getGoodsId().hashCode());
result = prime * result + ((getSellerId() == null) ? 0 : getSellerId().hashCode());
result = prime * result + ((getCartThumbnail() == null) ? 0 : getCartThumbnail().hashCode());
result = prime * result + ((getCategory() == null) ? 0 : getCategory().hashCode());
result = prime * result + ((getBrand() == null) ? 0 : getBrand().hashCode());
result = prime * result + ((getSpec() == null) ? 0 : getSpec().hashCode());
result = prime * result + ((getSeller() == null) ? 0 : getSeller().hashCode());
return result;
}
}
对solr进行增删改查的步骤为:
import org.junit.Test;
import org.junit.experimental.theories.suppliers.TestedOn;
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.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List; /**
* @Auther:qingmu
* @Description:脚踏实地,只为出人头地
* @Date:Created in 16:44 2019/4/19
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext-solr.xml")
public class TestTemplate { @Autowired
private SolrTemplate solrTemplate; /**
* 添加
*/
@Test
public void testAdd() {
Item item = new Item();
item.setId(1L);
item.setBrand("华为");
item.setCategory("手机");
item.setGoodsId(1L);
item.setSeller("华为2号专卖店");
item.setTitle("华为Mate9");
item.setPrice(new BigDecimal(2000));
solrTemplate.saveBean(item);
solrTemplate.commit();
}
/**
* 按主键进行查询
*/
@Test
public void testQueryById(){
Item item = solrTemplate.getById(1, Item.class);
System.out.println(item.getTitle());
} /**
*按主键删除
*/
@Test
public void testDelete(){
solrTemplate.deleteById("1");
solrTemplate.commit();
}
//创建100条数据
@Test
public void test100(){
ArrayList<Item> itemArrayList = new ArrayList<Item>();
for (long i = 0; i < 100; i++) {
Item item = new Item();
item.setId(i);
item.setBrand("华为");
item.setCategory("手机");
item.setGoodsId(i);
item.setSeller("华为2号专卖店");
item.setTitle("华为Mate9");
item.setPrice(new BigDecimal(2000+i));
itemArrayList.add(item);
}
solrTemplate.saveBeans(itemArrayList);
solrTemplate.commit();
} /**
* 编写分页查询的代码
*/
@Test
public void queryAll(){
// 创建查询对象
Query query = new SimpleQuery("*:*");
//开始
query.setOffset(20);
query.setRows(28);
ScoredPage<Item> items = solrTemplate.queryForPage(query, Item.class);
System.out.println( "总记录:"+items.getTotalElements());
List<Item> content = items.getContent(); for (Item item : content) {
System.out.println(item.getTitle()+"..."+item.getPrice());
}
} /**
* 条件查询
*/
@Test
public void queryByEx(){
Query query = new SimpleQuery("*:*");
Criteria criteria = new Criteria();
criteria = criteria.or("item_title").contains("9");
criteria = criteria.or("item_title").contains("9");
query.addCriteria(criteria);
System.out.println(criteria.toString());
ScoredPage<Item> items = solrTemplate.queryForPage(query, Item.class);
System.out.println(items.getTotalElements());
for (Item item : items) {
System.out.println(item.getTitle()+"...."+item.getPrice());
}
} @Test
public void deleteTest(){
SimpleQuery simpleQuery = new SimpleQuery("*:*");
solrTemplate.delete(simpleQuery);
solrTemplate.commit();
} }