ssm(Spring、Springmvc、Mybatis)实战之淘淘商城-第八天(非原创)

文章大纲

1、课程介绍
2、Solr基本介绍
3、ssm整合Solr
4、项目源码与资料下载
5、参考文章php

 

1、课程介绍

一共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)项目总结。html

2、Solr基本介绍

1. 什么是Solr

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

2. 全文搜索框架介绍

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

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并解压:web

 

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相关的一些许可信息redis

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.53spring

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目录下apache

 
 

(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 homewindows

 

5. Solr界面功能

 
 
 
image.png
 
 

3、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)); } } } 

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

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