黑马程序员技术交流社区
标题: 【郑州校区】Java之品优购课程讲义_day09(2) [打印本页]
作者: 谷粒姐姐 时间: 2018-9-12 15:02
标题: 【郑州校区】Java之品优购课程讲义_day09(2)
1. SpringDataSolr入门
1.1 Spring Data Solr 简介虽然支持任何编程语言的能力具有很大的市场价值,你可能感兴趣的问题是:我如何将
Solr 的应用集成到 Spring 中?可以,Spring Data Solr 就是为了方便 Solr 的开发所研制的一个框架,其底层是对 SolrJ(官方 API)的封装。
1.2 Spring Data Solr 入门小 Demo1.2.1 搭建工程(1)创建 maven 工程,pom.xml 中引入依赖
[AppleScript] 纯文本查看 复制代码
<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
1.1.1 @Field注 解
创建 cn.itcast.pojo 包,将品优购的 TbItem 实体类拷入本工程 ,属性使用@Field 注解标识 。如果属性与配置文件定义的域名称不一致,需要在注解中指定域名称。
[AppleScript] 纯文本查看 复制代码
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;
.......
}
1.1.1 增加(修改)创建测试类 TestTemplate.java
[AppleScript] 纯文本查看 复制代码
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext-solr.xml")
public class TestTemplate {
@Autowired
private SolrTemplate solrTemplate;
@Test
public void testAdd(){
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();
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) |
黑马程序员IT技术论坛 X3.2 |