SpringData - Solr

一. 导入依赖


    org.springframework.data
    spring-data-solr
    1.5.5.RELEASE

二. 配置文件

resources下配置applicationContext-solr.xml文件:






三. 创建实体类

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;

    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;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public Long getGoodsId() {
        return goodsId;
    }

    public void setGoodsId(Long goodsId) {
        this.goodsId = goodsId;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getSeller() {
        return seller;
    }

    public void setSeller(String seller) {
        this.seller = seller;
    }

    @Override
    public String toString() {
        return "item{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", price=" + price +
                ", image='" + image + '\'' +
                ", goodsId=" + goodsId +
                ", category='" + category + '\'' +
                ", brand='" + brand + '\'' +
                ", seller='" + seller + '\'' +
                '}';
    }

}

四. 执行索引库操作

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext-solr.xml")
public class testItem {

    @Autowired
    private SolrTemplate solrTemplate;

    /**
     * 添加集合
     */
    @Test
    public void testAddList() {
        List list = new ArrayList();
        for (int i = 0; i < 100; i++) {
            Item item = new Item();
            item.setId(i + 1L);
            item.setBrand("华为");
            item.setCategory("手机");
            item.setGoodsId(i + 1L);
            item.setSeller("华为1号专卖店");
            item.setTitle("华为C8813Q" + i);
            item.setPrice(new BigDecimal(2000 + i));
            list.add(item);
        }
        solrTemplate.saveBeans(list);
        solrTemplate.commit();
    }

    /**
     * 添加单个
     */
    @Test
    public void testAddOne() {
        Item item = new Item();
        item.setId(9999L);
        item.setBrand("华为");
        item.setCategory("手机");
        item.setGoodsId(9999L);
        item.setSeller("华为2号专卖店");
        item.setTitle("华为Mate9");
        item.setPrice(new BigDecimal(9999));
        solrTemplate.saveBean(item);
        solrTemplate.commit();
    }

    /**
     * 根据 id 查询
     */
    @Test
    public void testFindById() {
        Item item = solrTemplate.getById(1, Item.class);
        System.out.println(item);
    }

    /**
     * 根据 id 删除
     */
    @Test
    public void testDelete() {
        solrTemplate.deleteById("1");
        solrTemplate.commit();
    }

    /**
     * 清空索引库
     */
    @Test
    public void testDeleteAll() {
        Query query = new SimpleQuery("*:*");
        solrTemplate.delete(query);
        solrTemplate.commit();
    }

    /**
     * 分页查询
     */
    @Test
    public void testPageQuery() {
        Query query = new SimpleQuery("*:*");
        query.setOffset(0);   //开始索引(默认0)
        query.setRows(5);     //每页记录数(默认10)
        ScoredPage page = solrTemplate.queryForPage(query, Item.class);
        System.out.println("总记录数:" + page.getTotalElements());
        List list = page.getContent();
        showList(list);
    }

    /**
     * 条件分页查询
     */
    @Test
    public void testPageQueryMutil() {
        Query query = new SimpleQuery("*:*");
        Criteria criteria = new Criteria("item_title").contains("2");
        criteria = criteria.and("item_title").contains("5");
        query.addCriteria(criteria);
        //query.setOffset(20);//开始索引(默认0)
        //query.setRows(20);//每页记录数(默认10)
        ScoredPage page = solrTemplate.queryForPage(query, Item.class);
        System.out.println("总记录数:" + page.getTotalElements());
        List list = page.getContent();
        showList(list);
    }

    //显示记录数据
    private void showList(List list) {
        for (Item item : list) {
            System.out.println(item);
        }
    }

}

 

你可能感兴趣的:(demo,SpringData)