学习淘淘商城第四十七课(搜索功能Service实现)

        首先我们在taotao-search-interface工程新建一个接口类SearchService,并在接口类中添加一个接口,如下图所示。


        接着,我们到taotao-search-service工程添加一个实现类SearchServiceImpl,并实现SearchService接口,如下图所示。


         实现类代码如下:

package com.taotao.search.service.impl;

import org.apache.solr.client.solrj.SolrQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.taotao.common.pojo.SearchResult;
import com.taotao.search.dao.SearchDao;
import com.taotao.search.service.SearchService;

@Service
public class SearchServiceImpl implements SearchService {
	@Autowired
	private SearchDao searchDao;

	@Override
	public SearchResult search(String queryString, int page, int rows) throws Exception {
		//根据查询条件拼装查询对象
		//创建一个SolrQuery对象
		SolrQuery query = new SolrQuery();
		//设置查询条件
		query.setQuery(queryString);
		//设置分页条件
		if (page < 1) page = 1;
		query.setStart((page-1)*rows);
		if (rows < 1) rows = 10;
		query.setRows(rows);
		//设置默认搜索域,由于复制域查询不太准确,因此建议直接使用item_title域
		query.set("df", "item_title");
		//设置高亮显示
		query.setHighlight(true);
		query.addHighlightField("item_title");
		query.setHighlightSimplePre("<em>");
		query.setHighlightSimplePost("</em>");
		//调用Dao执行查询
		SearchResult searchResult = searchDao.search(query);
		//计算查询结果的总页数
		long totalNumber = searchResult.getTotalNumber();
		long pages = totalNumber / rows;
		if(totalNumber % rows > 0){
			pages++;
		}
		searchResult.setTotalPages(pages);
		//返回结果
		return searchResult;
	}

}

         在上面的实现类中使用注解的方式注入SearchDao,能不能注入成功呢?答案是不行的,因为我们在taotao-search-service的spring配置文件中配置的扫描包的范围是com.taotao.search.service,而com.taotao.search.dao不在这个范围内,从而无法将dao这个包下的接口类扫描到spring容器当中,我们也无法注入成功,解决方法有两种,第一种是把扫描包的范围扩大,由原来的com.taotao.search.service改为com.taotao.search,这样com.taotao.search.dao和com.taotao.search.service两个包就都可以被扫描进spring容器当中了。第二种方法是在已有的扫描包后面再加一个扫描范围com.taotao.search.dao,中间以","分隔(base-package="com.taotao.search.service,com.taotao.search.dao")。


       写完了Service,下面我们便要发布服务,我们在taotao-search-service工程的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">
    
    <!-- 配置包扫描器,扫描所有带@Service注解的类 -->
	<context:component-scan base-package="com.taotao.search"/>
	
	<!-- 发布dubbo服务 -->
	<!-- 提供方应用信息,用于计算依赖关系 -->
	<dubbo:application name="taotao-search" />
	<!-- 注册中心的地址 -->
	<dubbo:registry protocol="zookeeper" address="192.168.156.14: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便实现了。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值