spring-data-solr的配置及使用(二)
阿新 • • 發佈:2018-12-09
接下來我們就帶大家認識一下spring-data-solr在專案中的使用
首先大家需要solr伺服器安裝好, 直接訪問solr的地址,這樣下面操作的步驟更直觀一些.
第一步:導包
需要匯入測試的包,另外需要匯入下面的這個包,哈哈,很明顯這也是spring框架幫我封裝好的,跟spring-data-redis一樣簡單配置就可以使用.
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-solr</artifactId> <version>1.5.5.RELEASE</version> </dependency>
第二步:獲取solrTemplate 在resouorce目錄下直接建立solr相關的配置,這樣才能使用solrTemplate
<?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:solr="http://www.springframework.org/schema/data/solr" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/data/solr http://www.springframework.org/schema/data/solr/spring-solr.xsd"> <!--solr伺服器地址--> <solr:solr-server id="solr" url="http://127.0.0.1:8080/solr"></solr:solr-server> <!--solrtemplate獲取--> <bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate"> <constructor-arg ref="solr"/> </bean> </beans>
第三步:建立pojo,並在pojo的成員變數屬性上加相應的標籤
此標籤裡面的內容一定是對應solrhome裡面的schema-xml裡面配置好的相應欄位
普通欄位只用加@Filed("schema.xml中相應的欄位")
動態欄位要加兩個標籤 @Dynamic @Field("item_spec_*")
以下是l\solr-4.10.2-apach-tomcat2\solrhome\collection1\conf 路徑下的shema.xml中的配置,所以pojo裡面的欄位需要跟這些欄位對相應著
pojo如下:
package com.pyg.pojo; import org.apache.solr.client.solrj.beans.Field; import java.io.Serializable; import java.math.BigDecimal; import java.util.Date; public class TbItem implements Serializable{ @Field private Long id; @Field("item_title") private String title; @Field("item_price") private BigDecimal price; @Field("item_imag") 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; @Dynamic @Field("item_spec_*") private String spec;
省略getter和setter方法....
第四步: 注入solrTemplate後直接使用
package com.pyg.solr.test; import com.pyg.pojo.TbItem; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; 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 java.math.BigDecimal; import java.util.ArrayList; import java.util.List; import java.util.Queue; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext.xml") public class TbItemCURD { @Autowired private SolrTemplate solrTemplate; //------------------------增加單個物件操作----------------------------------------------- @Test public void add(){ TbItem item = new TbItem(); item.setId(1L); item.setPrice(new BigDecimal(1999)); item.setBrand("小米"); solrTemplate.saveBean(item); solrTemplate.commit(); } //------------------------增加多個物件----------------------------------------------- @Test public void multiAdd(){ List<TbItem> list= new ArrayList<TbItem>(); for(int i=1;i<=100;i++){ TbItem item = new TbItem(); item.setId(i+0L); item.setBrand("華為"+i); item.setPrice(new BigDecimal(2000+i)); item.setNum(99999); item.setTitle("華為榮耀系列產品"); item.setStatus("1"); list.add(item); } solrTemplate.saveBeans(list); solrTemplate.commit(); } //------------------------根據主鍵查詢一個物件----------------------------------------------- @Test public void findOne(){ TbItem item = solrTemplate.getById(1L, TbItem.class); System.out.println("title:"+item.getTitle()); } //------------------------分頁查詢----------------------------------------------- @Test public void findByOther(){ Query query=new SimpleQuery("*:*"); query.setOffset(0); query.setRows(10); ScoredPage<TbItem> tbItems = solrTemplate.queryForPage(query, TbItem.class); List<TbItem> content = tbItems.getContent();//獲取內容 for (TbItem tbItem : content) { System.out.println("brand:"+tbItem.getBrand()); } System.out.println("總條數"+tbItems.getTotalElements());//獲取總條數 System.out.println("本頁展示的條數"+tbItems.getTotalPages());//獲取本頁展示的條數 } //-----------條件查詢(注意條件查詢只能對支援分詞的欄位查詢,其餘均無法查詢)------------- @Test public void multifind(){ Query query = new SimpleQuery(); Criteria contains = new Criteria("item_title").contains("華為"); query.addCriteria(contains); ScoredPage<TbItem> tbItems = solrTemplate.queryForPage(query, TbItem.class); List<TbItem> content = tbItems.getContent(); for (TbItem tbItem : content) { System.out.println(tbItem.getTitle()); } } //---------------------------按主鍵刪除------------------------------------ @Test public void deleteById(){ solrTemplate.deleteById("1"); solrTemplate.commit(); } //刪除全部資料 @Test public void deleteAll(){ SimpleQuery query = new SimpleQuery("*:*"); solrTemplate.delete(query); solrTemplate.commit(); } //修改操作不用寫,主鍵只要一樣就會覆蓋 }