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. JAVA获取操作系统的信息

    列出全部信息: Properties prop = System.getProperties(); prop.list(System.out); 获取某个信息: String os = prop.ge ...

  2. 基于MFC的一个简单计算器

    写一个简单的计算器并不是什么很难的事,主要目的是要通过这个程序来学习和分析其中的核心算法.这个简易计算器的核心部分就是对输入的表达式的正确性判断与求值,其中包括对表达式的解析.中缀表达式转后缀表达式. ...

  3. xib或者storyboard设置颜色偏问题(与代码设置颜色不一致)

    选中xib中的label,在右边栏的第三个标签页中第三项是User Defined Runtime Attributes 添加一个keyPath,keyPath值为layer.borderWidth, ...

  4. c#实例化继承类,必须对被继承类的程序集做引用 .net core Redis分布式缓存客户端实现逻辑分析及示例demo 数据库笔记之索引和事务 centos 7下安装python 3.6笔记 你大波哥~ C#开源框架(转载) JSON C# Class Generator ---由json字符串生成C#实体类的工具

    c#实例化继承类,必须对被继承类的程序集做引用   0x00 问题 类型“Model.NewModel”在未被引用的程序集中定义.必须添加对程序集“Model, Version=1.0.0.0, Cu ...

  5. BZOJ1179 : [Apio2009]Atm 缩点+spfa

    1179: [Apio2009]Atm Time Limit: 15 Sec  Memory Limit: 162 MBSubmit: 2069  Solved: 826[Submit][Status ...

  6. Android实战技巧之八:Ubuntu下切换JDK版本【转】

    本文转载自:http://blog.csdn.net/lincyang/article/details/42024565 Android L之后推荐使用JDK7编译程序,这是自然发展规律,就像是4年前 ...

  7. 利用JFreeChart生成组合图表 (8) (转自 JSP开发技术大全)

    利用JFreeChart生成组合图表 (8) (转自 JSP开发技术大全) 14.8 利用JFreeChart生成组合图表  实例位置:光盘\mingrisoft\14\dxyy\02 通过JFree ...

  8. golang中管道热替换

    golang中管道替换问题 https://blog.csdn.net/cyk2396/article/details/78875347 1.运行以下代码: var chan1 chan int va ...

  9. Python---scikit-learn(sklearn)模块

    Python在机器学习方面一个非常强力的模块---scikit-learn模块,它作为数据挖掘和数据分析方面的一个简单而有效的工具,主要包括6大功能:分类(Classification),回归(Reg ...

  10. springboot根据yml配置文件选择性加载bean

    @Slf4j @Aspect @Component @ConditionalOnProperty(value = "localCache.apiCache", havingValu ...