項目搭建
在父工程中添加依賴
<spring-data-solr.version>1.5.5.RELEASE</spring-data-solr.version>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-solr</artifactId>
<version>${spring-data-solr.version}</version>
</dependency>
在實體類jar工程的pom文件上加入spring-data-solr的依賴和tb_item類上加入註解,修改完後安裝pojo工程
public class TbItem implements Serializable {
@Field
private Long id;//與solr中name一致,不用在註解中指定名稱
@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;
@Field("item_updatetime")
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;
省略了get和set方法
}
在父工程下創建一個moudle,jar工程youlexuan-solr-util
pom依賴
添加自己的項目依賴youlexuan_dao
因爲dao工程依賴了pojo工程,pojo工程導入了spring-data-solr依賴,所以youlexuan-solr-util也依賴了spring-data-solr
<artifactId>youlexuan-solr-util</artifactId>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>com.youxin</groupId>
<artifactId>youlexuan_dao</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
配置spring-solr-util.xml,並在創建的類上加上@Compent註解
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="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">
<context:component-scan base-package="com.youxin.solrutil" />
</beans>
配置spring-solr.xml
<?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/data/solr
http://www.springframework.org/schema/data/solr/spring-solr.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- solr服務器地址和solr庫 -->
<solr:solr-server id="solrServer" url="http://192.168.2.123:8983/solr/core1" />
<!-- solr模板,使用solr模板可對索引庫進行CRUD操作 -->
<bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
<constructor-arg ref="solrServer" />
</bean>
</beans>
搭建的最後一步創建solrUtil類
注入TbItemMapper和SolrTemplate
@Component
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:spring-*.xml")
public class SolrUtil {
@Autowired
private TbItemMapper itemMapper;
@Autowired
private SolrTemplate solrTemplate;
/*
導入商品數據
*/
@Test
public void importItemData(){
}
}
導入數據到solr
基本域
@Test
public void importItemData(){
//先條件查詢出審覈通過的商品
TbItemExample example = new TbItemExample();
TbItemExample.Criteria criteria = example.createCriteria();
criteria.andStatusEqualTo("1");
List<TbItem> items = itemMapper.selectByExample(example);
//導入solr
solrTemplate.saveBeans(items);
solrTemplate.commit();
}
運行測試方法,查看效果
動態域
數據庫中規格的形式
代碼中字段的類型
規格的名稱要以動態的形式命名,而代碼中的規格選項是字符串類型,無法操作其中的鍵和值,所以我們在實體類中新創建一個Map類型的屬性specMap,並加上註解以及get、set方法
@Dynamic
@Field("item_spec_*")
private Map<String,String> specMap;
public Map<String, String> getSpecMap() {
return specMap;
}
public void setSpecMap(Map<String, String> specMap) {
this.specMap = specMap;
}
public static long getSerialVersionUID() {
return serialVersionUID;
}
注意:solr 5.x後,安裝方式從tomcat中獨立出來,且鍵不支持中文了。因此,itme_spec_網絡 的動態屬性,會保存爲itme_spec___ 形式的下劃線。
解決方案:將中文轉爲拼音。
<dependency>
<groupId>com.github.promeg</groupId>
<artifactId>tinypinyin</artifactId>
<version>2.0.3</version>
</dependency>
代碼實現
@Test
public void importItemData(){
//先條件查詢出審覈通過的商品
TbItemExample example = new TbItemExample();
TbItemExample.Criteria criteria = example.createCriteria();
criteria.andStatusEqualTo("1");
List<TbItem> items = itemMapper.selectByExample(example);
for (TbItem item : items) {
Map<String,Object> specMap = JSON.parseObject(item.getSpec(), Map.class);//轉成map格式
System.out.println(specMap);
System.out.println("分割線");
Map map = new HashMap();//臨時map
for (String key : specMap.keySet()) {
// 第一個參數:要轉換的中文
// 第二參數:分隔符,比如逗號之類的,比如從哪轉換到哪,沒有的話,就寫空串
map.put(Pinyin.toPinyin(key,"").toLowerCase(),specMap.get(key));
}
System.out.println(map);
//給動態域名賦值
item.setSpecMap(map);
}
//導入solr
solrTemplate.saveBeans(items);
solrTemplate.commit();
}
運行測試類