(转)淘淘商城系列——商品搜索功能Service实现
2017-06-08 14:00
651 查看
http://blog.csdn.net/column/details/15737.html
首先我们在taotao-search-interface工程中新建一个SearchService接口,并在接口中添加一个方法,如下图所示。
![](https://images2015.cnblogs.com/blog/48590/201706/48590-20170608135903325-298085763.png)
接着,我们到taotao-search-service工程中添加一个SearchServiceImpl实现类,并实现SearchService接口,如下图所示。
![](https://images2015.cnblogs.com/blog/48590/201706/48590-20170608135914372-286844265.png)
为方便大家复制,现将SearchServiceImpl实现类的代码贴出。
1
2
写完了Service,下面我们便要发布服务了,我们在taotao-search-service工程的applicationContext-service.xml文件中暴露搜索接口,如下图所示。
![](https://images2015.cnblogs.com/blog/48590/201706/48590-20170608135926731-401154182.png)
为方便大家复制,现将applicationContext-service.xml文件的内容贴出。
这样,实现商品搜索功能的Service层代码便写完了。
首先我们在taotao-search-interface工程中新建一个SearchService接口,并在接口中添加一个方法,如下图所示。
![](https://images2015.cnblogs.com/blog/48590/201706/48590-20170608135903325-298085763.png)
接着,我们到taotao-search-service工程中添加一个SearchServiceImpl实现类,并实现SearchService接口,如下图所示。
![](https://images2015.cnblogs.com/blog/48590/201706/48590-20170608135914372-286844265.png)
为方便大家复制,现将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文件中暴露搜索接口,如下图所示。
![](https://images2015.cnblogs.com/blog/48590/201706/48590-20170608135926731-401154182.png)
为方便大家复制,现将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实现
- 淘淘商城系列——商品搜索功能表现层实现
- (转)淘淘商城系列——商品搜索功能表现层实现
- 淘淘商城系列——商品搜索功能Dao实现
- (转)淘淘商城系列——实现添加商品功能
- 淘淘商城系列——实现添加商品功能
- 学习淘淘商城第四十八课(商品搜索功能Controller实现)
- 学习淘淘商城第四十七课(搜索功能Service实现)
- (转)淘淘商城系列——商品搜索功能测试
- 淘淘商城系列——商品搜索功能测试
- 利用solr实现商品的搜索功能
- Android开发实现仿京东商品搜索选项卡弹窗功能
- 淘淘商城系列——利用拦截器实现登录功能及订单确认页面展示
- 利用solr实现商品的搜索功能
- 利用solr实现商品的搜索功能(实例讲解)
- (转)淘淘商城系列——实现图片上传功能
- 淘淘商城系列——商品类目选择的实现
- 【引用】类似于京东等的搜索筛选查询功能实现,商品分类筛选
- 淘淘商城系列——搜索功能切换到集群