淘淘商城23_在Linux上的操作_solrJ客户端_01数据导入索引库

一、需求

创建一个taotao-search工程, 做为一个服务的工程,     taotao-search-web 是一个前端访问的工程.前台进行访问的时候,直接访问的是taotao-search-web这个工程.

导入数据库数据的时候,使用的是后台操作的方式导入的数据,今天使用solrJ的方式把数据库里的数据导入到solr索引库中,查询的时候,直接从solr索引库中查询出数据.

二、步骤

1. 创建taotao-search工程

修改pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.taotao</groupId>
		<artifactId>taotao-parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<groupId>com.taotao</groupId>
	<artifactId>taotao-search</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>
	<dependencies>
		<dependency>
			<groupId>com.taotao</groupId>
			<artifactId>taotao-manager-dao</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		<dependency>
			<groupId>com.taotao</groupId>
			<artifactId>taotao-common</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
	</dependencies>
	<modules>
		<module>taotao-search-interface</module>
		<module>taotao-search-service</module>
	</modules>
	
	<build>
		<!-- 配置插件 -->
		<plugins>
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<configuration>
					<port>8084</port>
					<path>/</path>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

2. 创建taotao-search-interface工程

3. 创建taotao-search-service

3.1 修改pom文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.taotao</groupId>
    <artifactId>taotao-search</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>taotao-search-service</artifactId>
  <packaging>war</packaging>
  <dependencies>
  
  	<dependency>
  		<groupId>com.taotao</groupId>
	    <artifactId>taotao-search-interface</artifactId>
	    <version>0.0.1-SNAPSHOT</version>
  	</dependency>
  
  <!-- Spring -->
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-context</artifactId>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-beans</artifactId>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-webmvc</artifactId>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-jdbc</artifactId>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-aspects</artifactId>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-jms</artifactId>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-context-support</artifactId>
			</dependency>
  		<!-- dubbo相关 -->
			<dependency>
				<groupId>com.alibaba</groupId>
				<artifactId>dubbo</artifactId>
				<exclusions>
					<exclusion>
						<artifactId>spring</artifactId>
						<groupId>org.springframework</groupId>
					</exclusion>
					<exclusion>
						<artifactId>netty</artifactId>
						<groupId>org.jboss.netty</groupId>
					</exclusion>
				</exclusions>
			</dependency>
			<dependency>
				<groupId>org.apache.zookeeper</groupId>
				<artifactId>zookeeper</artifactId>
			</dependency>
			<dependency>
				<groupId>com.github.sgroschupf</groupId>
				<artifactId>zkclient</artifactId>
			</dependency>
			<!-- solr客户端 -->
			<dependency>
				<groupId>org.apache.solr</groupId>
				<artifactId>solr-solrj</artifactId>
			</dependency>
  </dependencies>
</project>

3.2 修改web.xml文件

3.2.1 创建WEB-INF

3.2.2 创建web.xml

3.2.3 修改web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="taotao" version="2.5">
	<display-name>taotao-search</display-name>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	<!-- 初始化spring容器 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring/applicationContext-*.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
</web-app>

4. 参考spring的配置

4.1 修改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:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.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://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.service"/>
	
	<!-- 生产者,在这发布一个dubbo服务,相当于把service发布到注册中心去. -->	
	<!-- 使用dubbo发布服务 -->
	<!-- 提供方应用信息,用于计算依赖关系 -->
	<dubbo:application name="taotao-search" />
	<dubbo:registry protocol="zookeeper" address="172.18.34.94:2181" />
	<!-- 用dubbo协议在20880端口暴露服务 -->
	<dubbo:protocol name="dubbo" port="20882" />
	
	<!-- 声明需要暴露的服务接口 -->
    <!-- 留下一个做参考 -->                
	<dubbo:service interface="com.taotao.service.ItemService" ref="itemServiceImpl" />
	
</beans>

4.2 修改applicationContext-tranc.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: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://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
	
	<!-- 事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 数据源 -->
		<property name="dataSource" ref="dataSource" />
	</bean>
	<!-- 通知 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<!-- 传播行为 -->
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="insert*" propagation="REQUIRED" />
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="create*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
		</tx:attributes>
	</tx:advice>
	<!-- 切面 -->
	<aop:config>
		<aop:advisor advice-ref="txAdvice"
			pointcut="execution(* com.taotao.search.service.*.*(..))" />
	</aop:config>
	
</beans>

扫描二维码关注公众号,回复: 4752841 查看本文章

4.3 applicationContext-dao.xml 暂时不做修改

三、代码的编写

1. 需求分析

页面查询的时候,有可能根据哪些字段查询?

页面上显示哪些内容?哪些字段

商品id,名称,卖点,价格,详情,图片,分类信息

 

数据来源几张表:

三张表: 商品表,商品分类表,商品详情表

SELECT

a.id,

  a.title,

  a.sell_point,

  a.price,

  a.image,

  t.`name`,

  c.item_desc

FROM

tb_item a

LEFT JOIN tb_item_cat t ON a.cid = t.id

LEFT JOIN tb_item_desc c ON a.id = c.item_id where a.`status`=1

2. 创建一个封装类SearchItem.java

package com.taotao.utils;

import java.io.Serializable;
/**
 * 封装的要查询的字段
 * @author fengjinzhu
 *
 */
public class SearchItem implements Serializable{
	private String id;
	private String title;
	private String sell_point;
	private long price;
	private String image;
	private String catName;
	private String item_desc;
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getSell_point() {
		return sell_point;
	}
	public void setSell_point(String sell_point) {
		this.sell_point = sell_point;
	}
	public long getPrice() {
		return price;
	}
	public void setPrice(long price) {
		this.price = price;
	}
	public String getImage() {
		return image;
	}
	public void setImage(String image) {
		this.image = image;
	}
	public String getCatName() {
		return catName;
	}
	public void setCatName(String catName) {
		this.catName = catName;
	}
	public String getItem_desc() {
		return item_desc;
	}
	public void setItem_desc(String item_desc) {
		this.item_desc = item_desc;
	}
	
	
	
}

3. 创建dao层:因为这个dao只是自己用,所以单独创建

SearchItemMapper.java

package com.taotao.search.mapper;

import java.util.List;

import com.taotao.utils.SearchItem;

public interface SearchItemMapper {
	//查询出的是多条数据
	public List<SearchItem> getSearchItemList();
}

SearchItemMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.taotao.search.mapper.SearchItemMapper" >
  
  <!-- 查询所有 -->
  <select id="getSearchItemList" resultType="com.taotao.utils.SearchItem">
		SELECT
		  a.id,
		  a.title,
		  a.sell_point,
		  a.price,
		  a.image,
		  t.name as catName,
		  c.item_desc
	  FROM
			tb_item a
		LEFT JOIN tb_item_cat t ON a.cid = t.id
		LEFT JOIN tb_item_desc c ON a.id = c.item_id where a.status=1  	
  </select>
</mapper>



4. 创建applicationContext-solr.xml,加载solrserver

<?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:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.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://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
	
	<!-- 配置SolrServer对象 -->
	<!-- 单机版 -->
	<bean id="httpSolrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer">
		<constructor-arg name="baseURL" value="${SOLR.SERVER.URL}"></constructor-arg>
	</bean>

</beans>

5. resource.properties

#solr服务地址
SOLR.SERVER.URL=http://172.18.34.111:8080/solr

6. 加载resource.properties

7. 发布服务

SearchItemService.java

package com.taotao.search.service;

import com.taotao.utils.TaotaoResult;

public interface SearchItemService {

	public TaotaoResult importSearchItem();
}

SearchItemServiceImpl.java

package com.taotao.search.service.impl;

import java.util.List;

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

import com.taotao.search.mapper.SearchItemMapper;
import com.taotao.search.service.SearchItemService;
import com.taotao.utils.ExceptionUtil;
import com.taotao.utils.SearchItem;
import com.taotao.utils.TaotaoResult;

@Service
public class SearchItemServiceImpl implements SearchItemService {

	private static final Logger LOGGER = LoggerFactory.getLogger(SearchItemServiceImpl.class);
	
	@Autowired
	private SearchItemMapper searchItemMapper;
	@Autowired
	private SolrServer solrServer;
	
	@Override
	public TaotaoResult importSearchItem() {
		try {
			//查询到数据
			List<SearchItem> list = searchItemMapper.getSearchItemList();
			//启动Tomcat的时候,让spring去创建solrserver实例
			//把数据导入到索引库
			SolrInputDocument document = new SolrInputDocument();
			for (SearchItem searchItem : list) {
				//根据索引库域的定义,将字段写入到索引库
				document.setField("id", searchItem.getId());
				document.setField("item_title", searchItem.getTitle());
				document.setField("item_category_name", searchItem.getCatName());
				document.setField("item_image", searchItem.getImage());
				document.setField("item_desc", searchItem.getItem_desc());
				document.setField("item_price", searchItem.getPrice());
				document.setField("item_sell_point", searchItem.getSell_point());
				
				solrServer.add(document);
			}
			solrServer.commit();
			return TaotaoResult.ok();
		} catch (Exception e) {
			e.printStackTrace();
			LOGGER.error("错误信息展示:", e);
			return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
		}
	
	}

}

8. 为了方便查看,所以我们在taotao-manager-web自定义一个页面

item-importIndex.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<link href="/js/kindeditor-4.1.10/themes/default/default.css" type="text/css" rel="stylesheet">
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<script type="text/javascript">
	function searchImport(){
		$.getJSON("searchItemImportAll" ,function(data){
			  if(data.status == 200){
				  $.messager.alert("提示", "数据导入成功")
			  }
		});
	}
</script>
<body>
	<a href="#" class="easyui-linkbutton" onclick="searchImport()">导入到索引库</a>
</body>
</html>

在index.jsp中添加(方便在首页展示)

<li>
         		<span>索引库管理</span>
         		<ul>
	         		<li data-options="attributes:{'url':'item-importIndex'}">导入索引库</li>
	         	</ul>
         	</li>

9. 创建controller

package com.taotao.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.taotao.search.service.SearchItemService;
import com.taotao.utils.TaotaoResult;

@Controller
public class SearchItemController {

	@Autowired
	private SearchItemService searchItemService;
	
	/**
	 * 导入数据到索引库
	 * @return
	 */
	@RequestMapping("/searchItemImportAll")
	@ResponseBody
	public TaotaoResult searchItemImport(){
		TaotaoResult result = searchItemService.importSearchItem();
		return result;
	}
}

10. 修改springmvc.xml的配置,调用服务

11. 错误更改

因为我们在applicationContenx-dao.xml中 没有配置,所以我们在taotao-search-service中的dao扫描不到,所以如图修改

12. 启动项目

报错了:

 解决:在taotao-search-service工程的pom中添加:

<!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉。 -->
	<build>
		<resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
	</build>

测试:

猜你喜欢

转载自blog.csdn.net/fjz_lihuapiaoxiang/article/details/85230643