
虽然支持任何编程语言的能力具有很大的市场价值,你可能感兴趣的问题是:我如何将
Solr 的应用集成到 Spring 中?可以,Spring Data Solr 就是为了方便 Solr 的开发所研制的一个
框架,其底层是对 SolrJ(官方 API)的封装。
1 搭建工程
(1)创建 maven 工程,pom.xml 中引入依赖
<dependencies>
org.springframework.data
spring-data-solr
1.5.5.RELEASE
<dependency>
<groupId>org.springframework
<artifactId>spring-test
<version>4.2.4.RELEASE
<dependency>
<groupId>junit
<artifactId>junit
<version>4.9
(2)在 src/main/resources 下创建 applicationContext-solr.xml
?
<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-server id="solrServer" url="http://127.0.0.1:8080/solr" />
<bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
<constructor-arg ref="solrServer" />
2 @Field 注解
创建 cn.itcast.pojo 包,将 TbItem 实体类拷入本工程 ,属性使用@Field 注解标识 。
如果属性与配置文件定义的域名称不一致,需要在注解中指定域名称。
public class TbItem 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;
.......
}
创建测试类 TestTemplate.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext-solr.xml")
public class TestTemplate {
@Autowired
private SolrTemplate solrTemplate;







网友评论