1 Spring Data Solr简介
虽然支持任何编程语言的能力具有很大的市场价值,你可能感兴趣的问题是:我如何将Solr的应用集成到Spring中?可以,Spring Data Solr就是为了方便Solr的开发所研制的一个框架,其底层是对SolrJ(官方API)的封装。
2 Spring Data Solr 入门小Demo
2.1 搭建工程
(1)创建maven工程,pom.xml中引入依赖
<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>
(2)在src/main/resources下创建 applicationContext-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: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/solrhttp://www.springframework.org/schema/data/solr/spring-solr-1.0.xsdhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!-- solr服务器地址 --><solr:solr-server id="solrServer" url="http://127.0.0.1:8080/solr" /><!-- solr模板,使用solr模板可对索引库进行CRUD的操作 --><bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate"><constructor-arg ref="solrServer" /></bean></beans>
2.2 @Field注解
创建 cn.carven.pojo 包,将品优购的TbItem实体类拷入本工程 ,属性使用@Field注解标识 。 如果属性与配置文件定义的域名称不一致,需要在注解中指定域名称。
public class TbItem implements Serializable {@Fieldprivate 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;.............}
2.3 增加(修改)
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContext-solr.xml")public class TestTemplate {@Autowiredprivate SolrTemplate solrTemplate;// 增加@Testpublic void testAdd() {TbItem item = new TbItem();item.setId(1L);item.setTitle("苹果8 PLUS");item.setCategory("手机10");item.setBrand("苹果");item.setSeller("苹果旗舰店");item.setGoodsId(2L);item.setPrice(new BigDecimal(3000.01));solrTemplate.saveBean(item);solrTemplate.commit();}}
2.4 按主键查询
@Testpublic void FindById() {TbItem item = solrTemplate.getById(1L, TbItem.class);System.out.println(item.getTitle());}
2.5 按主键删除
@Testpublic void deleteById() {solrTemplate.deleteById("1");solrTemplate.commit();}
2.6 批量插入
// 先插入一些数据准备@Testpublic void addList() {List beans = new ArrayList();for (int i = 0; i < 100; i++) {TbItem item = new TbItem();item.setId(i + 1L);item.setTitle("苹果" + i + 1 + " PLUS");item.setCategory("手机10");item.setBrand("苹果");item.setSeller("苹果旗舰店" + i);item.setGoodsId(2L);item.setPrice(new BigDecimal(3000.01 + i));beans.add(item);}solrTemplate.saveBeans(beans);solrTemplate.commit();}
2.7 按条件查询
// 分页查询@Testpublic void pageQuery() {// 构建查询对象Query query = new SimpleQuery("*:*");query.setOffset(20); // 开始索引query.setRows(20); // 每页记录大小ScoredPage<TbItem> page = solrTemplate.queryForPage(query, TbItem.class);for (TbItem item : page.getContent()) {System.out.println(item.getId() + " " + item.getTitle());}System.out.println("总记录数: " + page.getTotalElements());System.out.println("总页数: " + page.getTotalPages());}
2.8 条件查询
// 条件查询@Testpublic void pageQueryMutil() {// 构建查询对象Query query = new SimpleQuery();Criteria criteria = new Criteria("item_category").is("手机10");criteria = criteria.and("item_brand").contains("苹果");query.addCriteria(criteria);query.setOffset(10); // 开始索引query.setRows(20); // 每页记录大小ScoredPage<TbItem> page = solrTemplate.queryForPage(query, TbItem.class);for (TbItem item : page.getContent()) {System.out.println(item.getId() + " " + item.getTitle());}System.out.println("总记录数: " + page.getTotalElements());System.out.println("总页数: " + page.getTotalPages());}
2.9 全部删除
// 全部删除@Testpublic void deleteAll(){Query query = new SimpleQuery("*:*");solrTemplate.delete(query);solrTemplate.commit();}
3. 导入商品数据
3.1 @Dynamic注解
复制到品优购项目后,并修改TbItem.java,添加属性
@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;}
3.2 修改导入工具
原来为 table,需要自己转换一下{"cells":[{"value":"/**\n * 导入商品数据\n */\npublic void importItemData(){\n TbItemExample example=new TbItemExample();\n Criteria criteria = example.createCriteria();\n criteria.andStatusEqualTo(\"1\");//已审核\n List itemList = itemMapper.selectByExample(example);\n System.out.println(\"===商品列表===\");\n for(TbItem item:itemList){\n Map specMap= JSON.parseObject(item.getSpec());//将spec字段中的json字符串转换为map\n item.setSpecMap(specMap);//给带注解的字段赋值 \n System.out.println(item.getTitle()); \n }\n solrTemplate.saveBeans(itemList);\n solrTemplate.commit();\n System.out.println(\"===结束===\"); \n}\n","inlineStyles":{"color":[{"from":326,"to":397,"value":"#df402a"},{"from":398,"to":455,"value":"#df402a"}]}}],"heights":[40],"widths":[620]}
