学习淘淘商城第四十二课(导入商品数据-service层)

       上节课我们一起学习了使用Solrj来操作索引库。这节我们一起来学习下Service层代码编写。

       首先,在taotao-search-interface工程新建一个接口,如下图所示。


      接着在taotao-search-service工程新建实现类SearchItemServiceImpl,实现SearchItemService接口。如下图所示。


       代码如下

package com.taotao.search.service.impl;

import java.io.IOException;
import java.util.List;

import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.taotao.common.pojo.SearchItem;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.search.mapper.SearchItemMapper;
import com.taotao.search.service.SearchItemService;

@Service
public class SearchItemServiceImpl implements SearchItemService {
	@Autowired
	private SearchItemMapper searchItemMapper;
	@Autowired
	private SolrServer solrServer;

	@Override
	public TaotaoResult importItemsToIndex() {
		//1、先查询所有商品数据
		try {
			List<SearchItem> itemList= searchItemMapper.getSearchItemList();
			//2、遍历商品数据添加到索引库
			for(SearchItem searchItem : itemList){
				//创建文档对象
				SolrInputDocument document = new SolrInputDocument();
				document.setField("id", searchItem.getId());
				document.setField("item_title", searchItem.getTitle());
				document.setField("item_sell_point", searchItem.getSell_point());
				document.setField("item_price", searchItem.getPrice());
				document.setField("item_image", searchItem.getImage());
				document.setField("item_category_name", searchItem.getItem_category_name());
				document.setField("item_desc", searchItem.getItem_desc());
				solrServer.add(document);
			}
			solrServer.commit();
			return TaotaoResult.ok();
		}  catch (Exception e) {
			e.printStackTrace();
			return TaotaoResult.build(500, "导入数据失败!");
		}
	}

}

        在代码中要使用SearchItemMapper,Spring容器需要能够管理它才行,我们到applicationContext-dao.xml文件当中,原来的扫描范围是com.taotao.mapper,而我们的Mapper文件所在的包是com.taotao.search.mapper,因此需要增加一个扫描的范围,添加方式是以","分隔,如下图所示。


        在实现类中还用到了SolrServer,而默认Spring是没有管理这个类的,因此我们需要配置一下。我们单独建个applicationContext-solr.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">
    
   <!-- 配置单机版Solr -->
   <bean id="httpSolrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer">
   	   <constructor-arg name="baseURL" value="http://192.168.156.22:8080/solr/collection1"/>
   </bean>
</beans>
     

           写完服务,下面要做的便是发布服务,发布配置:<dubbo:service interface="com.taotao.search.service.SearchItemService" ref="searchItemServiceImpl" timeout="300000"/>,如下图所示。


           这样,Service层我们就写完了。




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值