项目搭建
在父工程中添加依赖
<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();
}
运行测试类