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实现的更多相关文章

  1. (转)淘淘商城系列——商品搜索功能Dao实现

    http://blog.csdn.net/yerenyuan_pku/article/details/72909286 终于进入商品搜索功能的开发中了,本文我来教大家编写实现商品搜索功能的Dao层代码 ...

  2. 后台商品搜索功能开发SQL

    在做后台的商品搜索功能开发时遇到了一些问题记录下来 版本一 <select id="SelectByNameAndParentId resultMap="Base_resul ...

  3. PHPCMS快速建站系列之搜索功能

    默认模板的搜索功能代码 <div class="bd"> <form action="{APP_PATH}index.php" method= ...

  4. 分布式网上商城项目-solr搜索功能错误

    1.RuntimeException错误 java.lang.RuntimeException: org.apache.ibatis.binding.BindingException: Invalid ...

  5. (转)淘淘商城系列——中文分析器IK-Analyzer的使用

    在Solr中默认是没有中文分析器的,需要手工配置,配置一个FieldType,在FieldType中指定使用的中文分析器.另外,Solr中的字段(即业务域)必须先定义后使用.下面我们先把中文分析器配好 ...

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

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

  7. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis)【第九天】(商品详情页面实现)

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

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

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

  9. 【PHP开源产品】Ecshop的商品筛选功能实现分析之一

    一.首先,说明一下为什么要对category.php文件进行分析. 原因如下: ①个人对商城类商品筛选功能的实现比较好奇: ②对商城中关于商品的数据表设计比较感兴趣.(该功能涉及到与数据库的交互,而且 ...

随机推荐

  1. C# .NET 如何修改代码字体

    工具-选项-字体和颜色

  2. Objective-C 2.0 基础要点归纳

    本文的阅读基本条件: 具备C/C++基础知识,了解面向对象特征 阅读过<Objective-C 2.0 程序设计(第二版)>.<Objective-C 程序设计 第6版>或相关 ...

  3. ie 代理设置中地址和端口置灰的解决办法

    @echo offecho 代理设置reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings&quo ...

  4. 开源 免费 java CMS - FreeCMS2.1 会员3.9我的收藏

    项目地址:http://www.freeteam.cn/ 我的收藏 从左側管理菜单点击我的收藏进入.在这里能够查看当前登录会员的全部收藏记录. 查看收藏 点击标题能够查看收藏内容. 删除收藏 选择收藏 ...

  5. Angular Scope解析与应用

    Scope层级结构 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA== ...

  6. nginx进程和实时控制

    原文地址:http://nginx.com/resources/admin-guide/processes-and-runtime-control/ Processes and Runtime Con ...

  7. Python 不同对象比較大小

    万恶的源泉: Fireboo的疑问(当然 lambda 本身写的就有问题): >>> filter( lambda x: x > 2, [ 1, [ 1, 2, 3 ], 2, ...

  8. lua-5.2.3编译问题记录&quot;libreadline.so: undefined reference to `PC&#39;&quot;

    作者:zhanhailiang 日期:2014-10-21 [root@~/software]# cd lua-5.2.3 [root@~/software/lua-5.2.3]# make linu ...

  9. 第一次PHP面试题

    1.标记符title是放在标记符什么之间:head 2.级联样式表文件的扩展名:CSS 3.html中<pre>的作用:pre 元素可定义预格式化的文本 4.mysql_affected_ ...

  10. MATLAB——matlab特殊符号表【转载】

    链接来源: matlab特殊符号表 http://blog.sina.com.cn/s/blog_4a09187801014xg9.html Character Sequence Symbol Cha ...