Spring Data Solr入门小Demo
package com.offcn.pojo;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date; import org.apache.solr.client.solrj.beans.Field; public class TbItem implements Serializable{
@Field
private 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; 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 == null ? null : title.trim();
} public String getSellPoint() {
return sellPoint;
} public void setSellPoint(String sellPoint) {
this.sellPoint = sellPoint == null ? null : sellPoint.trim();
} public BigDecimal getPrice() {
return price;
} public void setPrice(BigDecimal price) {
this.price = price;
} public Integer getStockCount() {
return stockCount;
} public void setStockCount(Integer stockCount) {
this.stockCount = stockCount;
} public Integer getNum() {
return num;
} public void setNum(Integer num) {
this.num = num;
} public String getBarcode() {
return barcode;
} public void setBarcode(String barcode) {
this.barcode = barcode == null ? null : barcode.trim();
} public String getImage() {
return image;
} public void setImage(String image) {
this.image = image == null ? null : image.trim();
} public Long getCategoryid() {
return categoryid;
} public void setCategoryid(Long categoryid) {
this.categoryid = categoryid;
} public String getStatus() {
return status;
} public void setStatus(String status) {
this.status = status == null ? null : status.trim();
} public Date getCreateTime() {
return createTime;
} public void setCreateTime(Date createTime) {
this.createTime = createTime;
} public Date getUpdateTime() {
return updateTime;
} public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
} public String getItemSn() {
return itemSn;
} public void setItemSn(String itemSn) {
this.itemSn = itemSn == null ? null : itemSn.trim();
} public BigDecimal getCostPirce() {
return costPirce;
} public void setCostPirce(BigDecimal costPirce) {
this.costPirce = costPirce;
} public BigDecimal getMarketPrice() {
return marketPrice;
} public void setMarketPrice(BigDecimal marketPrice) {
this.marketPrice = marketPrice;
} public String getIsDefault() {
return isDefault;
} public void setIsDefault(String isDefault) {
this.isDefault = isDefault == null ? null : isDefault.trim();
} public Long getGoodsId() {
return goodsId;
} public void setGoodsId(Long goodsId) {
this.goodsId = goodsId;
} public String getSellerId() {
return sellerId;
} public void setSellerId(String sellerId) {
this.sellerId = sellerId == null ? null : sellerId.trim();
} public String getCartThumbnail() {
return cartThumbnail;
} public void setCartThumbnail(String cartThumbnail) {
this.cartThumbnail = cartThumbnail == null ? null : cartThumbnail.trim();
} public String getCategory() {
return category;
} public void setCategory(String category) {
this.category = category == null ? null : category.trim();
} public String getBrand() {
return brand;
} public void setBrand(String brand) {
this.brand = brand == null ? null : brand.trim();
} public String getSpec() {
return spec;
} public void setSpec(String spec) {
this.spec = spec == null ? null : spec.trim();
} public String getSeller() {
return seller;
} public void setSeller(String seller) {
this.seller = seller == null ? null : seller.trim();
}
}
测试类
package com.offcn.test; import com.offcn.pojo.TbItem;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
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; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext-solr.xml")
public class TestTemplate { @Autowired
private SolrTemplate solrTemplate; @Test
public void test(){
TbItem item = new TbItem();
item.setId(1L);
item.setBrand("华为");
item.setCategory("手机");
item.setGoodsId(1L);
item.setSeller("华为2号专卖店");
item.setTitle("华为Mate9");
item.setPrice(new BigDecimal(2000));
solrTemplate.saveBean(item);
solrTemplate.commit(); } @Test
public void testFindOne(){
TbItem item = solrTemplate.getById(5, TbItem.class);
System.out.println(item.getTitle());
} @Test
public void testDelete(){
solrTemplate.deleteById("1");
solrTemplate.commit();
} @Test
public void testAddList(){
List<TbItem> list=new ArrayList(); for(int i=0;i<100;i++){
TbItem item=new TbItem();
item.setId(i+1L);
item.setBrand("华为");
item.setCategory("手机");
item.setGoodsId(1L);
item.setSeller("华为2号专卖店");
item.setTitle("华为Mate"+i);
item.setPrice(new BigDecimal(2000+i));
list.add(item);
} solrTemplate.saveBeans(list);
solrTemplate.commit();
} @Test
public void testPage(){
Query query = new SimpleQuery("*:*");
query.setOffset(0);//开始索引(默认0)
query.setRows(20);//每页记录数(默认10)
ScoredPage<TbItem> page = solrTemplate.queryForPage(query, TbItem.class);
System.out.println("总记录数:"+page.getTotalElements()); List<TbItem> list = page.getContent();
showList(list);
} private void showList(List<TbItem> list) {
for (TbItem item : list) {
System.out.println(item.getTitle() );
} } @Test
//分页查询
public void testQueryMutil(){
//创建查询对象
Query query = new SimpleQuery("*:*");
query.setOffset(0);//开始索引(默认0)
query.setRows(20);//每页记录数(默认10)
//声明一个查询条件
Criteria criteria = new Criteria("item_title").contains("5");
query.addCriteria(criteria);
ScoredPage<TbItem> page = solrTemplate.queryForPage(query, TbItem.class);
System.out.println("总记录数:"+page.getTotalElements()); List<TbItem> list = page.getContent();
showList(list);
} @Test
//删除所有
public void testDeleteAll(){
Query query = new SimpleQuery("*:*"); solrTemplate.delete(query);
solrTemplate.commit(); } }
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/solr
http://www.springframework.org/schema/data/solr/spring-solr-1.0.xsd
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">
<!-- solr服务器地址 -->
<solr:solr-server id="solrServer" url="http://192.168.200.128:8080/solr" />
<!-- solr模板,使用solr模板可对索引库进行CRUD的操作 -->
<bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
<constructor-arg ref="solrServer" />
</bean>
</beans>
pom.xml <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.offcn</groupId>
<artifactId>spring_data_solr</artifactId>
<version>1.0-SNAPSHOT</version> <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.12</version>
</dependency>
</dependencies> </project>
Spring Data Solr入门小Demo的更多相关文章
- Solr学习笔记(5)—— Spring Data Solr入门
一.Spring Data Solr简介 前面已经介绍了通过solrJ来操作solr,那么我们如何将Solr的应用集成到Spring中?Spring Data Solr就是为了方便Solr的开发所研制 ...
- Spring Data Solr入门
如何将Solr的应用集成到Spring中? SpringDataSolr就是为了方便Solr的开发所研制的一个框架,其底层是对SolrJ的封装. SpringDataSolr入门小Demo 首先目录结 ...
- Spring Data Solr教程(翻译) 开源的搜索服务器
Solr是一个使用开源的搜索服务器,它采用Lucene Core的索引和搜索功能构建,它可以用于几乎所有的编程语言实现可扩展的搜索引擎. Solr的虽然有很多优点,建立开发环境是不是其中之一.此博客条 ...
- Spring Data Solr教程(翻译)
大多数应用都必须具有某种搜索功能,问题是搜索功能往往是巨大的资源消耗并且它们由于沉重的数据库加载而拖垮你的应用的性能 这就是为什么转移负载到一个外部的搜索服务器是一个不错的主意,Apache Solr ...
- Spring Data Solr
1.什么是spring data solr? Solr是一个开源搜索平台,用于构建搜索应用程序.简单的来说就是作为一个搜索引擎使用. 2.solr的安装(本地安装,远程安装同) 1)解压一个tomca ...
- Spring Data Solr —— 快速入门
Solr是基于Lucene(全文检索引擎)开发,它是一个独立系统,运行在Tomcat或Jetty(solr6以上集成了jetty,无需再部署到servlet容器上),但其原生中文的分词词功能不行,需要 ...
- Spring Data Solr相关配置
1.增加Maven POM文件的存储库:pom配置如下: <repositories> <repository> <id>spring-milestone</ ...
- Spring Data Solr操作solr的简单案例
Spring Data Solr简介 虽然支持任何编程语言的能力具有很大的市场价值,你可能感兴趣的问题是:我如何将Solr的应用集成到Spring中?可以,Spring Data Solr就是为了方便 ...
- 记录一次Spring Data Solr相关的错误解决
记录一次Spring Data Solr相关的错误解决 生活本不易,流人遂自安 相信大家也使用过SpringDataSolr,但是在最新版的SpringDataSolr 4.0.5 RELEASE中有 ...
随机推荐
- adb server version (31) doesn't match this client (41); killing...
1.有时候用adb工具去连接安卓设备,或者模拟器的时候,会提示adb server version(31) doesn’t match this client(41)这样的提示.如图 提示的字面意思就 ...
- Spring Cloud-新一代Web框架微服务
序言 springcloud是微服务架构的集大成者,将一系列优秀的组件进行了整合.基于springboot构建,对我们熟悉spring的程序员来说,上手比较容易. 通过一些简单的注解,我们就可以快速的 ...
- annotation processor 为啥没有被调用?
Android Studio 3.5 使用@AutoService(Processor.class)注册annotation processor Android Plugin for Gradle: ...
- vs2015 项目调试出现未能加载文件或程序集“Antlr3.Runtime”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)
今天在调试项目不知道怎么了,突然就报未能加载文件或程序集“Antlr3.Runtime”或它的某一个依赖项.找到的程序集清单定义与程序集引用不匹配. (异常来自 HRESULT:0x80131040) ...
- 大数据常见端口汇总-hadoop、hbase、hive、spark、kafka、zookeeper等(持续更新)
常见端口汇总:Hadoop: 50070:HDFS WEB UI端口 8020 : 高可用的HDFS RPC端口 9000 : 非高可用的HDFS RPC端口 8088 ...
- supervisor配置进程
具体配置见此页面:https://www.jianshu.com/p/ac6c84a2f415 设置开机启动:https://blog.csdn.net/xyang81/article/details ...
- 解决mysql的in条件中参数是带引号的字符串的时候查询失效
原文链接:https://blog.csdn.net/u014520745/article/details/54091256
- setDefaultDllDirectories无法定位动态链接库kernel32.dll
参考链接 : https://blog.csdn.net/gdali/article/details/93084828 https://tieba.baidu.com/p/5795675519?red ...
- asp.net 的log4net的helper类
using log4net; using System; using System.Diagnostics; namespace MxWeiXinPF.Common.log { public stat ...
- Unity2018安装
1. 直接安装 2. 先安装Unity 2018.2.14f1 若有C:\ProgramData\Unity文件夹,删除这个文件夹下的所有文件 3. 解压 unity2018破解.zip 将最后一个 ...