文章大纲

一、课程介绍
二、Solr基本介绍
三、ssm整合Solr
四、项目源码与资料下载
五、参考文章

 

一、课程介绍

一共14天课程
(1)第一天:电商行业的背景。淘淘商城的介绍。搭建项目工程。Svn的使用。
(2)第二天:框架的整合。后台管理商品列表的实现。分页插件。
(3)第三天:后台管理。商品添加。商品类目的选择、图片上传、富文本编辑器的使用。
(4)第四天:商品规格的实现。
(5)第五天:商城前台系统的搭建。首页商品分类的展示。Jsonp。
(6)第六天:cms系统的实现。前台大广告位的展示。
(7)第七天:cms系统添加缓存。Redis。缓存同步。
(8)第八天:搜索功能的实现。使用solr实现搜索。
(9)第九天:商品详情页面的展示。
(10)第十天:单点登录系统。Session共享。
(11)第十一天:购物车订单系统的实现。
(12)第十二天:nginx。反向代理工具。
(13)第十三天:redis集群的搭建、solr集群的搭建。系统的部署。
(14)项目总结。

二、Solr基本介绍

1. 什么是Solr

Solr 是Apache下的一个顶级开源项目,采用Java开发,它是基于Lucene的全文搜索服务器。Solr提供了比Lucene更为丰富的查询语言,同时实现了可配置、可扩展,并对索引、搜索性能进行了优化。
Solr是一个全文检索服务器,只需要进行配置就可以实现全文检索服务。

2. 全文搜索框架介绍

https://www.cnblogs.com/WUXIAOCHANG/p/10855506.html

3. 下载

从Solr官方网站(http://lucene.apache.org/solr/ )下载Solr4.10.3,根据Solr的运行环境,Linux下需要下载lucene-4.10.3.tgz,windows下需要下载lucene-4.10.3.zip。
下载lucene-4.10.3.zip并解压:

 

bin:solr的运行脚本
contrib:solr的一些贡献软件/插件,用于增强solr的功能。
dist:该目录包含build过程中产生的war和jar文件,以及相关的依赖文件。
docs:solr的API文档
example:solr工程的例子目录:
example/solr:
该目录是一个包含了默认配置信息的Solr的Core目录。
example/multicore:
该目录包含了在Solr的multicore中设置的多个Core目录。
example/webapps:
该目录中包括一个solr.war,该war可作为solr的运行实例工程。
licenses:solr相关的一些许可信息

4. Solr的安装及配置

4.1 运行环境
solr 需要运行在一个Servlet容器中,Solr4.10.3要求jdk使用1.7以上,Solr默认提供Jetty(java写的Servlet容器),本教程使用Tocmat作为Servlet容器,环境如下:
Solr:Solr4.10.3
Jdk:jdk1.7.0_72
Tomcat:apache-tomcat-7.0.53

4.2 Solr整合tomcat
(1)将dist\solr-4.10.3.war拷贝到Tomcat的webapp目录下改名为solr.war
(2)启动tomcat后,solr.war自动解压,将原来的solr.war删除。
(3)拷贝example\lib\ext 目录下所有jar包到Tomcat的webapp\solr\WEB-INF\lib目录下

 
 

(4)拷贝log4j.properties文件
在 Tomcat下webapps\solr\WEB-INF目录中创建文件 classes文件夹,
复制Solr目录下example\resources\log4j.properties至Tomcat下webapps\solr\WEB-INF\classes目录
(5)创建solrhome及配置solrcore的solrconfig.xml文件
(6)修改Tomcat目录 下webapp\solr\WEB-INF\web.xml文件,如下所示:
设置Solr home

 

5. Solr界面功能

 
 
 
image.png
 
 

三、ssm整合Solr

1. 搭建ssm项目

具体搭建过程可以参考taotao-rest项目,搭建后代码可以在项目源码与资料下载中进行学习。

2. Solr连接测试

package com.taotao.search.dao.impl;

import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Test; /**
* Solr连接测试
*
* @author Administrator
*
*/
public class SolrJTest { @Test
public void addDocument() throws Exception{ //1.创建链接
SolrServer solr = new HttpSolrServer("http://localhost:8983/solr/"); //2.创建一文档对象
SolrInputDocument document = new SolrInputDocument(); //3.向文档对象中添加域 (先定义后使用)
document.addField("id", "001");
document.addField("title", "这是新的域"); //4.提交文档到索引库
solr.add(document); //5.提交
solr.commit();
} // @Test
// public void deleteDocument() throws Exception {
// //创建一连接
// SolrServer solrServer = new HttpSolrServer("http://192.168.3.153:8983/solr");
// //solrServer.deleteById("test001");
// solrServer.deleteByQuery("*:*");
// solrServer.commit();
// }
}

3. resources文件夹中新建Solr配置文件

 
<?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.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 配置solrServer对象 -->
<!-- 单机版 -->
<!-- <bean id="solrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer">
<constructor-arg name="baseURL" value="http://192.168.25.154:8080/solr"></constructor-arg>
</bean> -->
<!-- 集群版配置 -->
<bean id="solrServer" class="org.apache.solr.client.solrj.impl.CloudSolrServer">
<constructor-arg name="zkHost" value="192.168.25.154:2181,192.168.25.154:2182,192.168.25.154:2183"></constructor-arg>
<property name="defaultCollection" value="collection2"/>
</bean> </beans>

4. 新建数据添加功能

在com.taotao.service包下新建ItemService.java

package com.taotao.search.service;

import com.taotao.common.pojo.TaotaoResult;

public interface ItemService {

    TaotaoResult importAllItems()  throws Exception ;
}

在com.taotao.service.impl包下新建ItemServiceImpl.java

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.SolrItem;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.search.mapper.ItemMapper;
import com.taotao.search.service.ItemService; /**
* 向索引库中导入商品信息
* <p>Title: ItemServiceImpl</p>
* <p>Description: </p>
* <p>Company: www.itcast.com</p>
* @author 入云龙
* @date 2015年8月22日上午11:42:32
* @version 1.0
*/
@Service
public class ItemServiceImpl implements ItemService { @Autowired
private ItemMapper itemMapper;
@Autowired
private SolrServer solrServer; @Override
public TaotaoResult importAllItems() throws Exception {
//查询商品列表
List<SolrItem> list = itemMapper.getItemList();
//向索引库中添加文档
for (SolrItem solrItem : list) {
//创建文档对象
SolrInputDocument document = new SolrInputDocument();
document.setField("id", solrItem.getId());
document.setField("item_title", solrItem.getTitle());
document.setField("item_sell_point", solrItem.getSell_point());
document.setField("item_price", solrItem.getPrice());
document.setField("item_image", solrItem.getImage());
document.setField("item_category_name", solrItem.getItem_cat_name());
document.setField("item_desc", solrItem.getItem_desc());
//向索引库中添加文档
solrServer.add(document);
}
//提交修改
solrServer.commit(); return TaotaoResult.ok();
} }

com.taotao.controller包下新建ItemController.java

package com.taotao.search.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.common.pojo.TaotaoResult;
import com.taotao.common.utils.ExceptionUtil;
import com.taotao.search.service.ItemService; @Controller
@RequestMapping("/manage")
public class ItemController {
@Autowired
private ItemService itemService; @RequestMapping("/importall")
@ResponseBody
public TaotaoResult importAll() {
try {
TaotaoResult result = itemService.importAllItems();
return result;
} catch (Exception e) {
e.printStackTrace();
return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
}
}
}

5. 新建数据查询功能

在com.taotao.service包下新建SearchService.java

package com.taotao.search.service;

import com.taotao.common.pojo.SearchResult;

public interface SearchService {

    SearchResult search(String queryString, Long page, Long pageSize) throws Exception;
}

在com.taotao.service.impl包下新建SearchServiceImpl.java

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.ItemDao;
import com.taotao.search.service.SearchService; @Service
public class SearchServiceImpl implements SearchService { @Autowired
private ItemDao itemDao; @Override
public SearchResult search(String queryString, Long page, Long pageSize) throws Exception {
//创建查询对象
SolrQuery solrQuery = new SolrQuery();
//设置查询条件
//solrQuery.set("q","");
solrQuery.setQuery(queryString);
//设置分页
solrQuery.setStart((int) ((page - 1) * pageSize));
solrQuery.setRows(pageSize.intValue());
//高亮设置
solrQuery.setHighlight(true);
//设置高亮显示的域
solrQuery.addHighlightField("item_title");
//高亮显示的前缀
solrQuery.setHighlightSimplePre("<span style='color:red'>");
//高亮显示的后缀
solrQuery.setHighlightSimplePost("</span>");
//设置默认搜素域
solrQuery.set("df", "item_keywords");
//调用dao查询商品列表
SearchResult searchResult = itemDao.searchItem(solrQuery);
//计算总页数
long total = searchResult.getTotal();
long pageCount = total / pageSize;
if (total % pageSize > 0) {
pageCount++;
}
searchResult.setPageCount(pageCount);
searchResult.setPage(page);
//返回结果
return searchResult;
} }

com.taotao.controller包下新建SearchController.java

package com.taotao.search.controller;

import org.apache.commons.lang3.StringUtils;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; import com.taotao.common.pojo.SearchResult;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.utils.ExceptionUtil;
import com.taotao.search.service.SearchService; @Controller
public class SearchController { @Autowired
private SearchService searchService; @RequestMapping("/q")
@ResponseBody
public TaotaoResult search(@RequestParam("kw")String queryString, @RequestParam(defaultValue="1")Long page, @RequestParam(defaultValue="60")Long pageSize) {
if (StringUtils.isBlank(queryString)) {
return TaotaoResult.build(400, "查询条件不能为空");
}
try {
//解决get乱码问题
queryString = new String(queryString.getBytes("iso8859-1"), "utf-8");
SearchResult searchResult = searchService.search(queryString, page, pageSize);
return TaotaoResult.ok(searchResult);
} catch (Exception e) {
e.printStackTrace();
return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
} }
}

四、项目源码与资料下载
链接:https://pan.baidu.com/s/1V-SdXaAFwmCszHRHWEK6Yg
提取码:ydka

五、参考文章
http://yun.itheima.com/course?hm

ssm(Spring、Springmvc、Mybatis)实战之淘淘商城-第八天(非原创)的更多相关文章

  1. SSM Spring+SpringMVC+mybatis+maven+mysql环境搭建

    SSM Spring+SpringMVC+mybatis+maven环境搭建 1.首先右键点击项目区空白处,选择new->other..在弹出框中输入maven,选择Maven Project. ...

  2. SSM(Spring + Springmvc + Mybatis)框架面试题

    JAVA SSM框架基础面试题https://blog.csdn.net/qq_39031310/article/details/83050192 SSM(Spring + Springmvc + M ...

  3. SSM(Spring +SpringMVC + Mybatis)框架搭建

    SSM(Spring +SpringMVC + Mybatis)框架的搭建 最近通过学习别人博客发表的SSM搭建Demo,尝试去搭建一个简单的SSMDemo---实现的功能是对用户增删改查的操作 参考 ...

  4. SSM(Spring+SpringMVC+Mybatis)框架环境搭建(整合步骤)(一)

    1. 前言 最近在写毕设过程中,重新梳理了一遍SSM框架,特此记录一下. 附上源码:https://gitee.com/niceyoo/jeenotes-ssm 2. 概述 在写代码之前我们先了解一下 ...

  5. SSM Spring +SpringMVC+Mybatis 整合配置 及pom.xml

    SSM Spring +SpringMVC+Mybatis 配置 及pom.xml SSM框架(spring+springMVC+Mybatis) pom.xml文件 maven下的ssm整合配置步骤

  6. SSM(Spring,SpringMVC,Mybatis)框架整合项目

    快速上手SSM(Spring,SpringMVC,Mybatis)框架整合项目 环境要求: IDEA MySQL 8.0.25 Tomcat 9 Maven 3.6 数据库环境: 创建一个存放书籍数据 ...

  7. SSM:spring+springmvc+mybatis框架中的XML配置文件功能详细解释(转)

    原文:https://blog.csdn.net/yijiemamin/article/details/51156189# 这几天一直在整合SSM框架,虽然网上有很多已经整合好的,但是对于里面的配置文 ...

  8. 0927-转载:SSM:spring+springmvc+mybatis框架中的XML配置文件功能详细解释

    这篇文章暂时只对框架中所要用到的配置文件进行解释说明,而且是针对注解形式的,框架运转的具体流程过两天再进行总结. spring+springmvc+mybatis框架中用到了三个XML配置文件:web ...

  9. SSM:spring+springmvc+mybatis框架中的XML配置文件功能详细解释

    这几天一直在整合SSM框架,虽然网上有很多已经整合好的,但是对于里面的配置文件并没有进行过多的说明,很多人知其然不知其所以然,经过几天的搜索和整理,今天总算对其中的XML配置文件有了一定的了解,所以拿 ...

随机推荐

  1. NumberFormatException: For input string: "null"

    日志: [INFO-2016/08/04/16/:21/:25]ProjectCommonFormController.(78) - 审批[同意]入参-[string]commonFormDtoStr ...

  2. ANDROID开发中资源文件和资源ID是如何映射的

    http://tweetyf.org/2013/02/mapping_between_res_resid_android.html

  3. RN控件之TextInput

    /** * Sample React Native App * https://github.com/facebook/react-native */ 'use strict'; import Rea ...

  4. 我的笔记文档版本控制系统-MediaWiki-回到顶部/链接放大/升级

    为了练习自己的JS.CSS基本功,这些天和MediaWiki干上了!^_^ 下面是我的MediaWiki新添加的功能: 回到顶部 链接放大 MediaWiki升级 回到顶部 回到顶部是很多网站的基本功 ...

  5. 发现fork容易出错的一个地方

    今天在看代码时发现一段有意思的代码 #include<stdio.h> #include<stdlib.h> #include<unistd.h> #include ...

  6. FreeSql 新功能介绍:贪婪加载五种方法

    前言 FreeSql 在经过6个月的开发和朋友们的工作实践,不断的改进创新,目前拥有1500个左右单元测试方法,且每个方法内又复盖不同的测试面. 今天介绍 FreeSql 各种贪婪加载的姿势,作下总结 ...

  7. 浅谈JavaScript--事件委托与事件监听

    事件监听 该方法用于向指定元素添加事件句柄(代码块),且不会覆盖已存在的事件句柄. 即可以向同一个元素添加同一个事件多次. 添加事件 语法: element.addEventListener(even ...

  8. KONG 安装 (在 CentOS 7 中)

    1. 下载安装包:  https://bintray.com/kong/kong-community-edition-rpm/download_file?file_path=centos/7/kong ...

  9. cogs 1685 魔法森林

    /* 写了个傻逼二分套二分,真的傻逼了,我这tmd是在贪心呐,70分满满的人品 */ #include<iostream> #include<cstdio> #include& ...

  10. SKlearn中分类决策树的重要参数详解

    学习机器学习童鞋们应该都知道决策树是一个非常好用的算法,因为它的运算速度快,准确性高,方便理解,可以处理连续或种类的字段,并且适合高维的数据而被人们喜爱,而Sklearn也是学习Python实现机器学 ...