(转)淘淘商城系列——导入商品数据到索引库——Service层
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层的更多相关文章
- (转)淘淘商城系列——使用solrj来测试索引库
http://blog.csdn.net/yerenyuan_pku/article/details/72892280 我们使用solrj来操作索引库,一般习惯先建一个单元测试类测试下增删改查方法是否 ...
- 淘淘商城项目_同步索引库问题分析 + ActiveMQ介绍/安装/使用 + ActiveMQ整合spring + 使用ActiveMQ实现添加商品后同步索引库_匠心笔记
文章目录 1.同步索引库问题分析 2.ActiveM的介绍 2.1.什么是ActiveMQ 2.2.ActiveMQ的消息形式 3.ActiveMQ的安装 3.1.安装环境 3.2.安装步骤 4.Ac ...
- 010商城项目:商品类目的选择——Dao,Service.Action层的分析
我们现在开始写商品类选择这个功能: 先看效果: 当我们点击"新增商品"---->"选择目录"然后从数据库中查出来数据并显示了. 我们分析数据库的那张表: ...
- elasticsearch 导入基础数据并索引之 geo_point
elasticsearch 中的地理信息存储, 有geo_point形式和geo_shape两种形式 此篇只叙述geo_point, 地理位置需要声明为特殊的类型, 不显示在mapping中定义的话, ...
- elasticsearch 导入基础数据并索引之 geo_shape
我们看到的图形, 实际是由点来完成的, 有2种类型的格子模型可用于地理星座, 默认使用的是geoHash, 还有一种4叉树(quad trees), 也可用于 判断形状与索引的形状关系 1), int ...
- 第04项目:淘淘商城(SpringMVC+Spring+Mybatis)【第八天】(solr服务器搭建、搜索功能实现)
https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...
- 商城06——solr索引库搭建&solr搜索功能实现&图片显示问题解决
1. 课程计划 1.搜索工程的搭建 2.linux下solr服务的搭建 3.Solrj使用测试 4.把数据库中的数据导入索引库 5.搜索功能的实现 2. 搜索工程搭建 要实现搜索功能,需要搭建 ...
- 商城08——activeMQ 使用消息队列同步索引库
1. 课程计划 1.什么是MQ 2.MQ的应用场景 3.ActiveMQ的使用方法. 4.使用消息队列实现商品同步. 2. 同步索引库分析 方案一:在taotao-manager中,添加商品的业务 ...
- 淘淘商城_day07_课堂笔记
今日大纲 讲解订单系统 基于订单系统完成下单功能的开发 使用Solr完成商品的搜索功能 订单系统 说明:订单系统只是做讲解,不做开发. 导入taotao-order 表结构 订单表: 订单商品表: 疑 ...
随机推荐
- BZOJ 2208 JSOI2010 连通数 Tarjan+拓扑排序
题目大意:给定一个n个点的有向图,求有多少点对(x,y),使x沿边可到达y 设f[i][j]为从i到j是否可达 首先强联通分量中的随意两个点均可达 于是我们利用Tarjan缩点 缩点之后是一个拓扑图. ...
- POJ 1159 Palindrome(字符串变回文:LCS)
POJ 1159 Palindrome(字符串变回文:LCS) id=1159">http://poj.org/problem? id=1159 题意: 给你一个字符串, 问你做少须要 ...
- ADB无法启动
一般就是5037端口被占用,各种手机助手鼓捣的 1.CMD下查看端口是否被占用 netstat -ano|findstr "5037" 截图不上了,我的是820进程,360手机助手 ...
- USACO Section 1.2PROB Miking Cows
贪心做过去,先对每个时间的左边点进行排序,然后乱搞,当然线段树也可以做 /* ID: jusonal1 PROG: milk2 LANG: C++ */ #include <iostream&g ...
- [Codeforces 425A] Sereja and Swaps
[题目链接] https://codeforces.com/contest/425/problem/A [算法] 枚举最终序列的左端点和右端点 , 尝试用这段区间中小的数与区间外大的数交换 时间复杂度 ...
- Redis设置认证密码
1.找到Redis里的redis.conf配置文件:搜素requirepass所在的行,格式为:requirepass password 2.redis-cli客户端登陆格式:redis-cli -a ...
- bzoj 3534: [Sdoi2014]重建【矩阵树定理】
啊啊啊无脑背过果然不可取 比如这道题就不会写 参考:https://blog.csdn.net/iamzky/article/details/41317333 #include<iostream ...
- bzoj 3498: PA2009 Cakes【瞎搞】
参考:https://www.cnblogs.com/spfa/p/7495438.html 为什么邻接表会TTTTTTTLE啊...只能用vector? 把点按照点权从大到小排序,把无向边变成排名靠 ...
- [Swift通天遁地]一、超级工具-(14)使用SweetAlert制作漂亮的自定义Alert窗口
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- O - Combinations (组合数学)
Description Computing the exact number of ways that N things can be taken M at a time can be a great ...