http://blog.csdn.net/yerenyuan_pku/article/details/72894187

通过上文的学习,我相信大家已经学会了如何使用Solrj来操作索引库。本文我们将把商品数据导入到索引库中的Service层代码编写完毕! 
首先在taotao-search-interface工程中新建一个接口,如下图所示。 

可以看到importAllItemToIndex方法的返回值类型是TaotaoResult,当你纠结返回值是什么的时候,你就可以使用TaotaoResult。 
接着在taotao-search-service工程中新建以上接口的SearchItemServiceImpl实现类,为了方便大家复制,现将SearchItemServiceImpl实现类的代码贴出。

/**
* 导入商品数据到索引库
* <p>Title: SearchItemServiceImpl</p>
* <p>Description: </p>
* <p>Company: www.itcast.cn</p>
* @version 1.0
*/
@Service
public class SearchItemServiceImpl implements SearchItemService { @Autowired
private SolrServer solrServer; @Autowired
private ItemMapper itemMapper; @Override
public TaotaoResult importAllItemToIndex() throws Exception {
// 1、查询所有商品数据。
List<SearchItem> itemList = itemMapper.getItemList();
// 2、创建一个SolrServer对象。
// 3、为每个商品创建一个SolrInputDocument对象。
SolrInputDocument document = new SolrInputDocument();
for (SearchItem searchItem : itemList) {
// 4、为文档添加域
document.addField("id", searchItem.getId());
document.addField("item_title", searchItem.getTitle());
document.addField("item_sell_point", searchItem.getSell_point());
document.addField("item_price", searchItem.getPrice());
document.addField("item_image", searchItem.getImage());
document.addField("item_category_name", searchItem.getCategory_name());
document.addField("item_desc", searchItem.getItem_desc());
// 5、向索引库中添加文档。
solrServer.add(document);
}
// 提交
solrServer.commit();
// 6、返回TaotaoResult,当你纠结返回值是什么的时候,你就可以使用TaotaoResult。
return TaotaoResult.ok();
} }

以上代码中要使用ItemMapper,故Spring容器需要能够管理它才行,我们打开applicationContext-dao.xml文件,可以看到扫描包的范围是com.taotao.mapper和com.taotao.search.mapper,这说明之前我们已经配置好了,因此这里不用做任何修改。 

这里面还有一个问题,Service层要用到一个SolrServer对象,而Spring默认是没有管理这个对象的,我们再单独建一个applicationContext-solr.xml文件来管理,如下图所示。 

服务编写完之后,下面要做的便是发布服务了,即在applicationContext-service.xml文件中添加如下配置。

<dubbo:service interface="com.taotao.search.service.SearchItemService" ref="searchItemServiceImpl" timeout="300000" />


这样,把商品数据导入到索引库中的Service层代码编写完毕!

(转)淘淘商城系列——导入商品数据到索引库——Service层的更多相关文章

  1. (转)淘淘商城系列——使用solrj来测试索引库

    http://blog.csdn.net/yerenyuan_pku/article/details/72892280 我们使用solrj来操作索引库,一般习惯先建一个单元测试类测试下增删改查方法是否 ...

  2. 淘淘商城项目_同步索引库问题分析 + ActiveMQ介绍/安装/使用 + ActiveMQ整合spring + 使用ActiveMQ实现添加商品后同步索引库_匠心笔记

    文章目录 1.同步索引库问题分析 2.ActiveM的介绍 2.1.什么是ActiveMQ 2.2.ActiveMQ的消息形式 3.ActiveMQ的安装 3.1.安装环境 3.2.安装步骤 4.Ac ...

  3. 010商城项目:商品类目的选择——Dao,Service.Action层的分析

    我们现在开始写商品类选择这个功能: 先看效果: 当我们点击"新增商品"---->"选择目录"然后从数据库中查出来数据并显示了. 我们分析数据库的那张表: ...

  4. elasticsearch 导入基础数据并索引之 geo_point

    elasticsearch 中的地理信息存储, 有geo_point形式和geo_shape两种形式 此篇只叙述geo_point, 地理位置需要声明为特殊的类型, 不显示在mapping中定义的话, ...

  5. elasticsearch 导入基础数据并索引之 geo_shape

    我们看到的图形, 实际是由点来完成的, 有2种类型的格子模型可用于地理星座, 默认使用的是geoHash, 还有一种4叉树(quad trees), 也可用于 判断形状与索引的形状关系 1), int ...

  6. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis)【第八天】(solr服务器搭建、搜索功能实现)

    https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...

  7. 商城06——solr索引库搭建&solr搜索功能实现&图片显示问题解决

    1.   课程计划 1.搜索工程的搭建 2.linux下solr服务的搭建 3.Solrj使用测试 4.把数据库中的数据导入索引库 5.搜索功能的实现 2.   搜索工程搭建 要实现搜索功能,需要搭建 ...

  8. 商城08——activeMQ 使用消息队列同步索引库

    1.  课程计划 1.什么是MQ 2.MQ的应用场景 3.ActiveMQ的使用方法. 4.使用消息队列实现商品同步. 2.  同步索引库分析 方案一:在taotao-manager中,添加商品的业务 ...

  9. 淘淘商城_day07_课堂笔记

    今日大纲 讲解订单系统 基于订单系统完成下单功能的开发 使用Solr完成商品的搜索功能 订单系统 说明:订单系统只是做讲解,不做开发. 导入taotao-order 表结构 订单表: 订单商品表: 疑 ...

随机推荐

  1. Python中flatten用法

    Python中flatten用法 原创 2014年04月16日 10:20:02 标签: Python / flatten 22667 一.用在数组 >>> a = [[1,3],[ ...

  2. USRP内部的寄存器

    usrp_regs.hpp #ifndef INCLUDED_USRP2_REGS_HPP #define INCLUDED_USRP2_REGS_HPP ////////////////////// ...

  3. web metrics dashboard 数据分析工具 看板 从可视化发现问题 避免sql重复写 调高效率

    <?php$todo = array();$done = array();$h = array();$v = $all['v'];$l = count($v);#19700101 08for ( ...

  4. CSP 201703-4 地铁修建【最小生成树+并查集】

    问题描述 试题编号: 201703-4 试题名称: 地铁修建 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 A市有n个交通枢纽,其中1号和n号非常重要,为了加强运输能力,A市 ...

  5. CMake使用总结

    总结CMake的常用命令,并介绍有用的CMake资源. CMake意为cross-platform make,可用于管理c/c++工程.CMake解析配置文件CMakeLists.txt生成Makef ...

  6. 杂项:MySQL

    ylbtech-杂项:MySQL 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部 0. https://www.mysql.com/ 1. https://bai ...

  7. vs code 快速生成vue 模板

    vs code 快速生成vue 模板 1.使用快捷Ctrl + Shift + P唤出控制台,然后输入snippets并选择.(或 文件>首选项>用户代码片断里面,输入 vue.json ...

  8. UVaLive 6834 Shopping (贪心)

    题意:给定 n 个商店,然后有 m个限制,去 c 之前必须先去d,问你从0到n+1,最短路程是多少. 析:我们我们要到c,必须要先到d,那么举个例子,2 5, 3 7,如果我们先到5再到2,再到7再到 ...

  9. java io流读取 和commons.io的使用

    前提:记事本里面一共有605个字 1.使用BufferedReader和FileReader来读取txt里面的内容,用时相对短.读完记得关闭流br.close() 2.指定UTF-8输出格式,使用Fi ...

  10. bzoj 1492: [NOI2007]货币兑换Cash【贪心+斜率优化dp+cdq】

    参考:http://www.cnblogs.com/lidaxin/p/5240220.html 虽然splay会方便很多,但是懒得写,于是写了cdq 首先要想到贪心的思路,因为如果在某天买入是能得到 ...