• (转)淘淘商城系列——商品搜索功能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层代码便写完了。

     
  • 相关阅读:
    【Linux基础总结】Linux基本环境
    mysql 源码安装
    windows内存映射文件
    TCHAR和CHAR类型的互转
    删除链表中重复的结点
    iptables防火墙
    两个链表的第一个公共结点
    无人值守安装linux系统
    dns服务 很多问题,后续再研究
    string 类型转换
  • 原文地址:https://www.cnblogs.com/telwanggs/p/6962452.html
Copyright © 2020-2023  润新知