文章大纲

一、课程介绍
二、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. shell入门-tr替换字符和split切割大文件

    命令:tr 说明:替换字符 格式tr ‘原字符’ ‘新字符’ 可以是范围字符,指定字符 命令:split 选项:-b 50m 1.txt  根据大小分割 单位是b不用单位,单位是兆加m -l 100 ...

  2. HDU 5862 Counting Intersections (离散化+扫描线+树状数组)

    题意:给你若干个平行于坐标轴的,长度大于0的线段,且任意两个线段没有公共点,不会重合覆盖.问有多少个交点. 析:题意很明确,可是并不好做,可以先把平行与x轴和y轴的分开,然后把平行y轴的按y坐标从小到 ...

  3. Codeforces Round #179 (Div. 2) B. Yaroslav and Two Strings (容斥原理)

    题目链接 Description Yaroslav thinks that two strings s and w, consisting of digits and having length n  ...

  4. matlab 矩阵运算技巧

    1.a=a(:) 作用:将矩阵转化成列向量 a=[a11 a12 a13                      a=[a11 a21 a12 a22 a13 a23]^T a21 a22 a23] ...

  5. SAS笔记(8) 利用数组重构SAS数据集

    在实际应用中,我们经常会把宽数据(一个患者一条观测)转化为长数据(一个患者多条观测)或者将长数据(一个患者多条观测)转换为宽数据(一个患者一条观测),在R中我们可以利用Reshape2包来实现.在SA ...

  6. java整理(一)

    1.方法重载:方法名称相同,参数的类型或个数不同.但是返回值类型不同,不是方法重载. 2.引用数据类型:数组,类,接口.内存地址分为两类,栈内存和对内存.栈内存保存的是对内存的地址,简单理解就是保存了 ...

  7. Ruby中如何识别13位的时间戳

    由于13位的时间戳在Ruby中是比较另类的,以为Ruby中默认的时间戳都是10位的.而Time和Date是Ruby中常用的处理时间的模块. 由于最初遇到问题的时候网上搜了好久都没找到合适的,因此就自己 ...

  8. 帝都Day6——图论

    //P2O5呢? 一.图的存储: 邻接矩阵:邻接表. 邻接矩阵:n*n的[][],[i][j]节点有边记1没边0 缺点 空间复杂度O(n^2) 占用内存较大(我为什么要把这些东西写到这里呢???) 邻 ...

  9. kubectl 命令

    Kubectl 命令表 kubectl run kubectl expose kubectl annotate kubectl autoscale kubectl convert kubectl cr ...

  10. day04 ---Linux安装Python3

    如何linux上安装python3 1.下载源代码,方式有2个, 1.在windows上下载,下载完成后,通过lrzsz工具,或者xftp工具,传输到linux服务器中 2.在linux中直接下载 c ...