(转)淘淘商城系列——商品搜索功能Service实现
http://blog.csdn.net/column/details/15737.html
首先我们在taotao-search-interface工程中新建一个SearchService接口,并在接口中添加一个方法,如下图所示。 
接着,我们到taotao-search-service工程中添加一个SearchServiceImpl实现类,并实现SearchService接口,如下图所示。 
为方便大家复制,现将SearchServiceImpl实现类的代码贴出。
/**
* 商品搜索服务实现类
* <p>Title: SearchServiceImpl</p>
* <p>Description: </p>
* <p>Company: www.itcast.cn</p>
* @version 1.0
*/
@Service
public class SearchServiceImpl implements SearchService {
@Autowired
private ItemSearchDao itemSearchDao;
@Override
public SearchResult search(String queryString, int page, int rows) throws Exception {
// 1、创建一个SolrQuery对象。
SolrQuery query = new SolrQuery();
// 2、设置查询条件
query.setQuery(queryString);
// 3、设置分页条件
if (page < 1) { // page为当前页
page = 1;
}
query.setStart((page - 1) * rows);
if (rows < 1) {
rows = 10;
}
query.setRows(rows);
// 4、需要指定默认搜索域。由于复制域查询不太准确,因此建议直接使用item_title域
query.set("df", "item_title");
// 5、设置高亮
query.setHighlight(true);
query.addHighlightField("item_title"); // 设置高亮显示的域
query.setHighlightSimplePre("<em style=\"color:red\">"); // 设置高亮显示的前缀
query.setHighlightSimplePost("</em>"); // 设置高亮显示的后缀
// 6、执行查询,调用SearchDao。得到SearchResult
SearchResult searchResult = itemSearchDao.search(query);
// 7、需要计算总页数。
long totalNumber = searchResult.getTotalNumber();
long totalPage = totalNumber / rows;
if (totalNumber % rows > 0) {
totalPage++;
}
searchResult.setTotalPage(totalPage);
// 8、返回SearchResult
return searchResult;
}
}
- 1
- 2
写完了Service,下面我们便要发布服务了,我们在taotao-search-service工程的applicationContext-service.xml文件中暴露搜索接口,如下图所示。 
为方便大家复制,现将applicationContext-service.xml文件的内容贴出。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<context:component-scan base-package="com.taotao.search"></context:component-scan>
<!-- 使用dubbo发布服务 -->
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="taotao-search" />
<dubbo:registry protocol="zookeeper" address="192.168.25.128:2181" />
<!-- 用dubbo协议在20882端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20882" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.taotao.search.service.SearchItemService" ref="searchItemServiceImpl" timeout="300000" />
<dubbo:service interface="com.taotao.search.service.SearchService" ref="searchServiceImpl" timeout="300000" />
</beans>
这样,实现商品搜索功能的Service层代码便写完了。
(转)淘淘商城系列——商品搜索功能Service实现的更多相关文章
- (转)淘淘商城系列——商品搜索功能Dao实现
http://blog.csdn.net/yerenyuan_pku/article/details/72909286 终于进入商品搜索功能的开发中了,本文我来教大家编写实现商品搜索功能的Dao层代码 ...
- 后台商品搜索功能开发SQL
在做后台的商品搜索功能开发时遇到了一些问题记录下来 版本一 <select id="SelectByNameAndParentId resultMap="Base_resul ...
- PHPCMS快速建站系列之搜索功能
默认模板的搜索功能代码 <div class="bd"> <form action="{APP_PATH}index.php" method= ...
- 分布式网上商城项目-solr搜索功能错误
1.RuntimeException错误 java.lang.RuntimeException: org.apache.ibatis.binding.BindingException: Invalid ...
- (转)淘淘商城系列——中文分析器IK-Analyzer的使用
在Solr中默认是没有中文分析器的,需要手工配置,配置一个FieldType,在FieldType中指定使用的中文分析器.另外,Solr中的字段(即业务域)必须先定义后使用.下面我们先把中文分析器配好 ...
- 第04项目:淘淘商城(SpringMVC+Spring+Mybatis)【第八天】(solr服务器搭建、搜索功能实现)
https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...
- 第04项目:淘淘商城(SpringMVC+Spring+Mybatis)【第九天】(商品详情页面实现)
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. 搜索工程搭建 要实现搜索功能,需要搭建 ...
- 【PHP开源产品】Ecshop的商品筛选功能实现分析之一
一.首先,说明一下为什么要对category.php文件进行分析. 原因如下: ①个人对商城类商品筛选功能的实现比较好奇: ②对商城中关于商品的数据表设计比较感兴趣.(该功能涉及到与数据库的交互,而且 ...
随机推荐
- hdu 1009 FatMouse' Trade
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 水晶报表 Crystal Report 调用存储过程时出错 找不到表 ,解决方法。
用 CrystalReportViewer1 控件在asp.net的网页上显示报表,假设做报表时调用数据表数据的方式调用是能够成功的.但报表是用存储过程获取数据方式会出现下面错误: 找不到表'RptO ...
- 量化分析师的Python日记【第1天:谁来给我讲讲Python?】
量化分析师的Python日记[第1天:谁来给我讲讲Python?]薛昆Kelvin优矿 001 号员工2015-01-28 15:48 58 144克隆 ###“谁来给我讲讲Python?” 作为无基 ...
- MySQL-插入数据(INSERT)
Insert语句可将一行或多行插入到表中. INSERT语法: INSERT INTO table(column1,column2...) VALUES (value1,value2,...); 首先 ...
- 【HNOI模拟By lyp】Day1
1 xlk1.1 题目描述 给定一棵大小为 n 的无根树,求满足以下条件的四元组 (a, b, c, d) 的个数: 1. 1 ≤ a < b ≤ n 2. 1 ≤ c < d ≤ n 3 ...
- mongodb配置复制集replset
Mongodb的replication主要有两种:主从和副本集(replica set).主从的原理和mysql类似,主节点记录在其上的所有操作oplog,从节点定期轮询主节点获取这些操作,然后对自己 ...
- ABAP OLE
OLE DATA: excel TYPE ole2_object, workbook TYPE ole2_object, sheet TYPE ole2_object, cell TYPE ole2_ ...
- 美国诚实签经验——IMG全球医疗险,TODO
那么,诚实签最关键的4个要点 是什么呢? 第一,证明你有一定的经济实力. 可能需要房产.存款等证明,也需要银行信用卡或借记卡半年流水证明(让人信服的每月进帐和消费能力). 这些是为了证明,你可以支付在 ...
- 22 WPF列表,树,网格
ListView ListView从ListBox派生,只增加了View属性.如果你没有设置View属性,ListView行为正如ListBox. 从技术上,View属性指向任何ViewBase派生类 ...
- shiro vue 前后端分离中模拟登录遇到的坑
系统采用jeeplus框架(ssm+redis+shiro+mongodb+redis),默认是了JSP未做前后端分离,由于业务需要已经多终端使用的需求(H5.小程序等),需要实现前后端分离.但是由于 ...